0

I am trying to show attribute type and variation on my custom made minicart in woocommerce. I use WC()->cart->cart_contents[ $item ]; to get cart item object. and using wc_implode_text_attributes() to extract value from array where $variation_name return name but not attribute.

Attribute = color variation = green , red , yellow

So I want output should be like

color:green

but the current output is only showing

green

$cart_item = WC()->cart->cart_contents[ $item ];
if( $cart_item['data']->is_type( 'variation' ) ){
    $attributes = $cart_item['data']->get_attributes();
    $variation_name = wc_implode_text_attributes( $attributes );
    var_dump( $attributes);
}

The dump result is

array(1) { ["color"]=> string(5) "green" }

So how Can I extract this dump like color:green ?

2 Answers 2

1

Just replace your codes with follows -

$cart_item = WC()->cart->cart_contents[ $item ];
if( $cart_item['data']->is_type( 'variation' ) ){
    $attributes = $cart_item['data']->get_attributes();
    $variation_names = array();
    if( $attributes ){
        foreach ( $attributes as $key => $value) {
            $variation_names[] = $key .': '. $value;
        }
    }
    echo implode( '<br>', $variation_names );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes Thanks for the answer. But I already solved that array issue actually because of caching issue its not showing in front end.
@Firefog, you can contact me via my profile
0
$cart_item = WC()->cart->cart_contents[ $item ];
    if( $cart_item['data']->is_type( 'variation' ) ){
    $attributes = $cart_item['data']->get_attributes();
    foreach($attributes as $attname => $variant) {
    $attribute_variation = $attname .  " : " . $variant;
    print($attribute_variation );
    }
}

1 Comment

please consider adding more explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.