php - Unable to return 'attribute' data in Woocomerce -
pa_i trying dog products' attribute data in order compare attribute between items in cart. however, cannot access data. following code returns following on var_dump command:
c:\wamp\www\wizstaginglocal\wp-content\plugins\code-snippets\php\snippet-ops.php(426) : eval()'d code:17:boolean false
in other words nothing
code:
add_action('woocommerce_before_cart', 'wiz_scale_woocommerce_before_cart'); function wiz_scale_woocommerce_before_cart() { foreach(wc() -> cart -> get_cart() $cart_item_key => $cart_item) { // here wc_product object $product = $cart_item['data']; echo "my product id {$cart_item['product_id']} \n"; $attributes = $product -> get_attributes(); foreach($attributes $taxonomy => $value) { // wp_term object $term_obj = get_term_by('pa_1_scale', $value, $taxonomy); $term_name = $term_obj -> name; } echo '<pre>'; var_dump($term_obj); echo '</pre>'; } }
at var_dump $attributes shows, relevant slug:
array (size=3).... 'pa_1_scale' => object(wc_product_attribute)[1383] 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
i became aware of this post: reason?
the attribute data in serialized, cannot find way extract it. work around successful write sql query wp database.
function mycustomfunc_retrieve_attribute_for product() { foreach(wc() -> cart -> get_cart() $cart_item_key => $cart_item) { $product = $cart_item['product_id']; // perform wpdb search global $wpdb; $myatt = $wpdb->get_var( $wpdb->prepare( "select distinct t.name 'term name' wiz_posts p inner join wiz_term_relationships tr on p.id = tr.object_id inner join wiz_term_taxonomy tt on tt.term_taxonomy_id = tr.term_taxonomy_id inner join wiz_terms t on t.term_id = tt.term_id p.post_type = 'product' , p.id = %d , tt.taxonomy = 'myslugname' ", $product ) ); echo "my product id {$product} , attribute {$myatt} \n"; }
Comments
Post a Comment