【问题标题】:Woocommerce how to get products images of recent ordersWoocommerce如何获取最近订单的产品图片
【发布时间】:2021-11-06 11:53:48
【问题描述】:

下面的代码重新显示每个图像,而不在每个 foreach 循环中重置。

例如它给出:

  • 产品 1 - image1
  • 产品 2 - image1+2
  • 产品 3 - image1+2+3 ...

如何确保以后的产品不再显示过去产品的图片?

预期结果:

  • 产品 1 - 图片 1
  • 产品 2 - 图片 2
  • 产品 3 - 图片 3 ...

提前谢谢你,这是我的代码:

    <?php foreach ( $last_order->get_items() as $item ) : ?>
        <?php 
   $product   = $item->get_product(); // Get the WC_Product object (from order item)
    $thumbnail = $product->get_image(array( 50, 50)); // Get the product thumbnail (from product object)
    if( $product->get_image_id() > 0 ){
        $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
}
 echo $item_name . $item->get_name();?>
    <?php endforeach;?>

【问题讨论】:

    标签: php wordpress woocommerce orders woocommerce-theming


    【解决方案1】:

    这是因为在 $item_name 变量的末尾有一个名为 $item_name 的额外变量!用以下代码替换您的代码以修复重复项:

    foreach ($last_order->get_items() as $item) :
      $product   = $item->get_product(); 
      $thumbnail = $product->get_image(array(50, 50));
      if ($product->get_image_id() > 0) {
        $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>'; // You had an extra variable here
      }
      echo $item_name . $item->get_name();
    endforeach;
    

    经过测试并且有效!让我知道你是否也能让它工作!

    【讨论】:

    • 非常感谢它有效! (第 1 行只有双 $$,但我仍然设法自己更正了这个细节?)
    • 太棒了!是的,这是一个错字,因为我在另一端对它进行了测试,但我忘了删除它。我刚刚更新了我的答案。
    猜你喜欢
    • 2022-01-21
    • 2015-12-10
    • 2016-12-26
    • 2021-12-06
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多