WooCommerce - 检查属性是否存在,然后给出变量属性值


WooCommerce - check if attribute exists and then give variable attribute value

我为我的 wooCommerce 产品创建了一个带有 slug 'short-title ' 的新属性。这个想法是我想用短标题覆盖商店页面上的标题。我已经设置了一个循环,如下所示:

if (get_post_meta($product->id, 'short-code', true)) {
$product_name =  (get_post_meta($product->id, 'short-code', true));
} else {
$product_name =  get_the_title();
}
echo '<div class="product-below"><h4 class="product-name">'.$product_name.'</h4>'; 

但是,当我在产品的短标题字段中输入值时,这不会覆盖标题。我想我需要与get_post_meta不同的功能

你可以试试:

global $product;
$product_name = $product->get_attribute( 'short-code' );

而不是

$product_name = get_post_meta($product->id, 'short-code', true)

如果它不适合您,那么也请确认添加属性的 slug 是short-codeshort_code