将产品图片添加到 Woocommerce 我的帐户订单视图

问题描述 投票:0回答:5

我想在 Woocommerce 的帐户查看订单页面上添加图像。我能够获取图像,但尺寸太大,我不知道需要在哪里添加此代码:

<?php 
    // Get a list of all items that belong to the order
    $products = $order->get_items();

    // Loop through the items and get the product image
    foreach( $products as $product ) {                  

        $product_obj = new WC_Product( $product["product_id"] );

        echo $product_obj->get_image();

    }
?>   

任何帮助将不胜感激

这是查看订单页面上的位置,我想在其中添加产品图片:

wordpress image woocommerce hook-woocommerce orders
5个回答
10
投票

下面的挂钩函数将完成这项工作(您可能需要添加一些CSS样式规则)

// Display the product thumbnail in order view pages
add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );
function display_product_image_in_order_item( $item_name, $item, $is_visible ) {
    // Targeting view order pages only
    if( is_wc_endpoint_url( 'view-order' ) ) {
        $product   = $item->get_product(); // Get the WC_Product object (from order item)
        $thumbnail = $product->get_image(array( 36, 36)); // Get the product thumbnail (from product object)
        if( $product->get_image_id() > 0 )
            $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
    }
    return $item_name;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。

注意

WC_Product
方法
get_image()
在内部使用 Wordpress
get_the_post_thumbnail()


更新:在代码中添加了

if( $product->get_image_id() > 0 )
,仅当产品图片存在时才显示产品图片。


0
投票

您不需要获取主图像,只需通过 get_the_post_thumbnail() 获取缩略图即可:

<?php 
    // Get a list of all items that belong to the order
    $products = $order->get_items();

    // Loop through the items and get the product image
    foreach( $products as $product ) {                  

        $product_obj = new WC_Product( $product["product_id"] );

        echo get_the_post_thumbnail();

    }
?>   


0
投票

写在 woocommerce/template/order/ordr-details-item.php 上

echo '<div class="product-imgae">'.$product->get_image(array( 80, 80)).'</div>';

这将为您提供 80x80 尺寸的图像,根据您的需要进行更改。


0
投票

我知道这个主题更多的是产品图片,但我如何上传产品变体的客户特定徽标,因为他还没有准备好上传。我根据产品页面的功能和逻辑制作的上传和预览。但现在我缺少更新不完整订单的部分。 我确实尝试过这段代码:

add_filter('woocommerce_order_item_meta_end', '_hook_order_detail_item', 30, 3);
function _hook_order_detail_item($item_id, $item, $order)
{
   foreach ($order->get_items() as $inner_item)
   {
      if ($inner_item['_img_file_degree']&&$item_id===$inner_item->get_id())
      {
          $_img_file = $inner_item['_img_file'];
          $_img_file_title = $_img_file[0]['title'];
          //print_r($_img_file_title);
          //$order_id = $order->get_order_number();$cart_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
          //echo '<pre>Hallo<br />'; print_r($cart_items);echo '<br /></pre>';
         echo '<ul class="wc-item-meta" style="list-style: none; padding-left: 0; font-size: inherit;"><li><strong class="wc-item-meta-label">' . esc_html__('Bild hochgeladen:', 'woocommerce') . '</strong><span>' . $_img_file_title . '</span></li><li><strong class="wc-item-meta-label">' . esc_html__("Logo-Bilddrehung:", "woocommerce") . '</strong><span>' . $inner_item['_img_file_degree'] . '</span></li></ul>';
      }
      elseif (is_wc_endpoint_url( 'view-order' ))
      {
          $order_id = $order->get_order_number();$cart_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
          $items = $order->get_items();
          foreach ( $items as $item ) {
              $product_name = $item->get_name();
              $product_id = $item->get_product_id();
              $product_variation_id = $item->get_variation_id();
          };
          ?><form class="variations_form" method="post"><?php display_additional_product_fields();
          // echo '<pre>Hallo<br />'; print_r($product_variation_id);echo '<br /></pre>';
          ?><button type="submit" class="button" name="update_product" id="image_variant_upload" value="<?php esc_attr_e( 'Update product', 'woocommerce' ); ?>"><?php esc_html_e( 'Update product', 'woocommerce' ); ?>
          </button>
          <input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
          <input type="hidden" name="variation_id" class="variation_id" value="<?php echo $product_variation_id; ?>">
          </form><?php
          if(array_key_exists('update_product', $_POST)) {add_custom_fields_data_as_custom_cart_item_data($cart_item, $product_id);};

);
      };
   }
}

此代码与 add_action 位于同一文件中,用于在购物车上添加徽标作为产品变体图像上传。


0
投票

我几个月前添加了这段代码,它运行得很好。唯一的问题是,当我删除产品时似乎会产生问题。如果产品被删除,当我进入帐户的订单历史记录页面时,会产生致命错误。这个问题可以解决吗?

© www.soinside.com 2019 - 2024. All rights reserved.