WooCommerce - check if an attribute exists, and then specify the value of the variable attribute

I created a new attribute for my woocommerce products using slug 'short-title'. The idea is that I want to redefine the name on the pages of the store with a short name. I installed a loop with the following:

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>'; 

But this does not override the title when I enter a value in the short title field for the product. I think I need another function from get_post_meta p>

+5
source share
1 answer

You may try:

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

instead

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

And if this does not work for you, please confirm again that the slug for the added attribute is short-code or short_code

+5
source

Source: https://habr.com/ru/post/1215024/


All Articles