php - Product Attributes in Short Description in WooCommerce 3.0+ -
this question has answer here:
i’ve been trying use code found here this:
// woocommerce version 3.0+ (only) add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 ); function custom_attributes_display(){ // product category archives pages if(is_product_category()){ global $product; // array of attributes names $attribute_names = array('pa_guaranteed-ram', 'pa_cpu', 'pa_disk', 'pa_traffic'); foreach( $attribute_names $key => $attribute_name ) { // woocommerce version 3.0+ $product_id = $product->get_id(); // wc 3.0+ // getting value of attribute (ok wc 3.0+) $attribute_value = array_shift(wc_get_product_terms( $product_id, $attribute_name)); // displays if attribute exist product if(!empty($attribute_value)){ // updated echo $attribute_value; // separating each number " / " if($key < 3) echo ' / '; } } } }
but end error: catchable fatal error: object of class wp_term not converted string in ../functions.php on line 51 line:
echo $attribute_value;
each of 4 attributes i’m trying pull terms have 1 value i’m not sure if array_shift part of problem, still doesn’t work when removed.
i checked out many other posts , guides , still couldn't figured out.. i'm php novice , appreciated, , sorry if i've posted incorrectly.
this post not duplicate of other post because other post has bunch of people saying works , "thanks!" when not work me, situation different.... also, couldn't post comments on other post due being new how supposed help?? lol
your exec code should using $product->id
instead of $product->get_id()
.
// woocommerce version 3.0+ (only) add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 ); function custom_attributes_display(){ // product category archives pages if(is_product_category()){ global $product; // array of attributes names $attribute_names = array('pa_guaranteed-ram', 'pa_cpu', 'pa_disk', 'pa_traffic'); foreach( $attribute_names $key => $attribute_name ) { // getting value of attribute (ok wc 3.0+) $attribute_value = array_shift(wc_get_product_terms( $product->id, $attribute_name)); // displays if attribute exist product if(!empty($attribute_value)){ // updated echo $attribute_value; // separating each number " / " if($key < 3) echo ' / '; } } } }
Comments
Post a Comment