简体   繁体   中英

Displaying WooCommerce Cart with Variations

I am building a custom theme and trying to create a "Cart" link which, when hovered over, will display a preview of the WooCommerce cart.

Using the following code from this post: Get cart item name, quantity all details woocommerce

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

        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id() );
            //product image
            $getProductDetail = wc_get_product( $values['product_id'] );
            echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )

            echo "<b>".$_product->get_title() .'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
            /*Regular Price and Sale Price*/
            echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>";
            echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>";
        }
?>

I have successfully managed to create a nice little display of the cart.

BUT - It is not showing the correct variation of the product which has been added to the cart. It is only showing the main featured image of the product, and not the image of the variation that has been added.

Can anyone tell me how to get and display the details of the specific product variations which have been added to the cart?

Try this. It will work for both normal and variation product details with image:

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

foreach ($items as $item => $values) {
    //get variation product id 
    $variation_id = $values['variation_id'];

    /* check if cart added item is variation product or not (checked by using variation id ) 
     if yes then variation product related information set to variables 
     Else normal product information will set to variables 
     i.e product_id common , product title , product image etc. 
     */
    if ($variation_id) {
        $product_id = $variation_id; // set commmon id ,later will use in get_post_meta() 
        $_product = new WC_Product_Variation($values['variation_id']);
        $product_image = $_product->get_image();
        $product_title = $_product->get_name();
    } else {

        $product_id = $values['product_id']; // set commmon id ,later will use in get_post_meta() 
        $_product = wc_get_product($values['data']->get_id());
        $getProductDetail = wc_get_product($values['product_id']);
        $product_image = $getProductDetail->get_image();
        $product_title = $_product->get_title();
    }

    echo $product_image; // product image depends on if else condition i.e variation or normal product
    echo "<b>" . $product_title . '</b>  <br> Quantity: ' . $values['quantity'] . '<br>';
    $price = get_post_meta($product_id, '_price', true);
    echo "  Price: " . $price . "<br>";
    /* Regular Price and Sale Price */
    echo "Regular Price: " . get_post_meta($product_id, '_regular_price', true) . "<br>";
    echo "Sale Price: " . get_post_meta($product_id, '_sale_price', true) . "<br>";
}

here is image cart details display image...

$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

if ( ! $_product->is_visible() ) {
            echo $thumbnail;
        } else {
                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($_product->id), 'thumbnail' );
                        $cartimgsrc=UNAVAILABLEIMG;
                    if($thumb[0]){
                        $cartimgsrc=$thumb[0];

                   $product = wc_get_product($cart_item['product_id']);
                   if ( $product && $product->is_type( 'variable' ) ) {


                            $product = new WC_Product_Variable( $product_id );
                            $variations = $product->get_available_variations();
                            foreach ( $variations as $variation ) {
                                if($variation['variation_id'] == $cart_item['variation_id'] ){
                                    $cartimgsrc=$variation['image']['thumb_src'];
                                }   
                            }
                         }

                    }

                    echo "<a href='".$_product->get_permalink( $cart_item )."'> <img class='cartthumbimage' height='180px' width='180px' src='".$cartimgsrc."' /></a>";
                    //printf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) ), $thumbnail );
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM