如何获得WooCommercer的产品差异化形象

时间:2017-05-15 作者:Guillermo Carone

我正在构建一个WooCommerce主题,但当用户选择一个产品变体时,我很难改变特征图像。

我在后端为每个变体设置了不同的特征图像,这是我用来调用产品特征图像的函数。

为了获取变体的特征图像而不是整个产品的特征图像,我应该向该功能添加什么?

function show_featured_image() {
  global $product;
  echo \'<div class="product_fetured_image">\';
    echo \'<div class="feautred_image"><img src="\' . get_the_post_thumbnail_url() . \'"></div>\';
  echo \'</div>\';
}

1 个回复
SO网友:Samir Lakhani

这是在购物车中的购物车中显示的产品集。php

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

结束

相关推荐