Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have created a new attribute for my woocommerce products with the slug 'short-title'. The idea being that I want to override the title on the shop pages with the short title. I have set up 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 is not overriding the title when I enter a value in the short title field for a product. I think I need a different function from get_post_meta

like image 560
rhysclay Avatar asked Mar 10 '15 03:03

rhysclay


1 Answers

You can try :

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

instead of

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

And if it not works for you then please also once confirm the slug for the added attribute that it is short-code or short_code

like image 78
Jenis Patel Avatar answered Sep 19 '22 09:09

Jenis Patel