1

I want to display the entire shopping cart in a customized way, therefore I have to get access to the:

  • Product Image
  • Product Title
  • Product Price
  • Product Attributes

So far I managed to display everything but the attributes. This is my working code so far.


<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    

        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id() );
           
            $getProductDetail = wc_get_product( $values['product_id'] );
            echo $getProductDetail->get_image(); 

            echo "<b>".$_product->get_title() .'</b>  <br> Anzahl: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
            /*attributes*/
            /* $available_variations = $_product->get_available_variations();
                foreach ($available_variations as $key => $value) {
                    echo "$key $value";
                }*/
                    
        }
?>

The last part about the attributes is not working can someone assist me and tell me how to access the attributes of each product in the cart and display them underneath?

5
  • 1
    Either use $_product->get_attribute( 'attributeid' ); //get specific attribute value or $_product->get_attributes(); // get all attributes . Read this - businessbloomer.com/… Commented Mar 8, 2022 at 8:32
  • Thanks for the reply. I've come so far that I don't get an error anymore. But I still can't get the color or size variation to be displayed. Commented Mar 8, 2022 at 20:32
  • /*attributes*/ $available_variations = $_product->get_attribute('pa_groese'); echo "$available_variations"; Commented Mar 8, 2022 at 20:33
  • This is what I changed down at the part for the attributes Commented Mar 8, 2022 at 20:34
  • Attribute and variation are totaly different thing. Clarify what you want to output. And $available_variations should be array ? Commented Mar 9, 2022 at 8:08

1 Answer 1

1

Try this code

global $product;
echo wc_display_product_attributes( $product );
Sign up to request clarification or add additional context in comments.

2 Comments

I suggest using the $product->get_attributes(); instead.
When I do this I get a fatal error. Is it possible to only take certain attributes instead of all?

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.