php - Looking to recover the product attribute for a cart item -
i trying recover array of attributes products in woocomerce cart
this code have written (i beginner)
foreach ( wc()->cart->get_cart() $cart_item_key => $cart_item ) { global $product; $my_product_id = $cart_item['product_id']; echo "id {$my_product_id} \n"; $my_attribute = wc()->product->attribute->get_name($value); }
however "call member function get_name() on null"
i think getting items product_id 's ok (using echo view). not sure how use function get_name() or indeed of methods in object wc_product_attribute
i guess working outside loop?
i writing code wordpress 'page' in order develop it, using plugin wraps code in tags - [insert_php] & [/insert_php]
help appreciated, need understand doing wrong conceptually.
correct code loictheaztec produces third element of array (the attribute want retrieve)
'pa_1_scale' => object(wc_product_attribute)[1410] protected 'data' => array (size=6) 'id' => int 1 'name' => string 'pa_1_scale' (length=10) 'options' => array (size=1) ... 'position' => int 2 'visible' => boolean true 'variation' => boolean false
the correct code is:
foreach ( wc()->cart->get_cart() $cart_item_key => $cart_item ) { // here wc_product object $product = $cart_item['data']; $product_id = $cart_item['product_id']; $attributes = $product->get_attributes(); foreach($attributes $taxonomy => $value){ // wp_term object $term_obj = get_term_by( 'slug', $value, $taxonomy ); $term_name = $term_obj->name; } }
tested , works
Comments
Post a Comment