【问题标题】:Display a custom thumbnail for a specific product ID in Woocommerce cart在 Woocommerce 购物车中显示特定产品 ID 的自定义缩略图
【发布时间】:2019-01-31 21:47:23
【问题描述】:

在 Woocommerce 中,我想使用个性化图像更改特定购物车项目的缩略图。我可以更改图像,但它会更改所有购物车项目缩略图。
我只想更改特定产品 ID 的图像。

我该怎么做?

这是我正在使用的代码:

add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 10, 3 ); 

function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, 

    $cart_item_key ) { 

    $cart = WC()->cart->get_cart();

    foreach( $cart as $cart_item ){

        $product = wc_get_product( $cart_item['product_id'] );

        if($cart_item['product_id'] == 75){

        echo  'New Image Here';

        }
    }

};

请帮忙。

【问题讨论】:

    标签: php wordpress woocommerce thumbnails cart


    【解决方案1】:

    您不需要 foreach 循环,因为 $cart_item 已作为参数包含在函数中。以下代码适用于所有产品类型,并允许您在购物车页面中拥有特​​定产品的自定义缩略图:

    add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 20, 3 ); 
    function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){ 
        // HERE your targeted product ID
        $targeted_id = 75;
    
        if( $cart_item['product_id'] == $targeted_id || $cart_item['product_id'] == $targeted_id ){
            echo  'New Image Here';
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      【解决方案2】:

      LoicTheAztec 的解决方案很棒,但它会将您的所有缩略图保留为空白,除了与 if 语句中的条件匹配的缩略图。它让我走了 90% 的路。为了克服这个问题,我添加了...

      echo $thumbnail;
      

      添加后(在 if 块之外),您可以看到所有缩略图以及您的自定义拇指都毫发无损。 LoicTheAztec 解决方案也有一个 OR 运算符,但因为它使用与 OR 之前的第一个条件完全相同的条件,所以不需要它。也许它只是您需要满足多个条件的场景的占位符。

      在这种情况下,switch 语句似乎是更好的选择。它允许您以非常易读的方式轻松自定义任意数量的拇指。这是对我有用的最终代码...

      add_filter( 'woocommerce_cart_item_thumbnail', 'aw_change_woocommerce_cart_item_thumbnail', 20, 3 );
      function aw_change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){
        $theProductID = $cart_item['product_id']; // save product ID into a variable
        switch ($theProductID) {
          case 47056:
            //this is a gift membership which needs a different shaped thumbnail than other products;
          echo '<img width="73" height="46" src="/wp-content/uploads/2020/05/giftcard-thumb.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="gift card">';
          break;
          case 47149:
            //this is a gift card which also needs a different shaped thumbnail than other products;
          echo '<img width="73" height="46" src="/wp-content/uploads/2020/05/giftcard73x58.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="gift card">';
          break;
          default:
          echo $thumbnail;
        }
      }
      

      【讨论】:

      • 虽然此代码可以解决 OP 的问题,但最好包含关于您的代码如何解决 OP 问题的说明。这样,未来的访问者可以从您的帖子中学习,并将其应用到他们自己的代码中。 SO 不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能获得支持。这些功能,以及所有帖子都是独立的要求,是 SO 作为一个平台的一些优势,使其与论坛区分开来。您可以编辑以添加其他信息和/或使用源文档补充您的解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2021-06-10
      • 2021-12-29
      • 2019-05-11
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多