显示带有变体的WooCommerce购物车

时间:2019-06-21 04:37:39

标签: php wordpress woocommerce

我正在构建自定义主题,并尝试创建一个“购物车”链接,将其悬停时将显示WooCommerce购物车的预览。

使用本文中的以下代码: 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>";
        }
?>

我已经成功地创建了一个漂亮的购物车小展示架。

但是-没有显示已添加到购物车的产品的正确版本。它仅显示产品的主要特色图片,而不显示已添加的变体形式的图片。

有人可以告诉我如何获取和显示已添加到购物车中的特定产品变体的详细信息吗?

2 个答案:

答案 0 :(得分:1)

尝试一下。适用于带有图片的常规和版本产品详细信息

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>";
}

答案 1 :(得分:0)

这是图像购物车详细信息显示图像...

$_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 );
        }