在 WooCommerce 我的帐户订单列表上显示产品缩略图 [英] Display product thumbnails on WooCommerce My account orders list

查看:45
本文介绍了在 WooCommerce 我的帐户订单列表上显示产品缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WooCommerce 我的帐户上显示产品缩略图 >订单列表,在订单号旁边.

I am trying to display product thumbnails on WooCommerce My account > Orders list, beside the order number.

以下是订单截图

我必须使用什么钩子来显示图像?

What hook I have to use to display the image?

我尝试了添加产品图像到 Woocommerce 我的帐户订单视图 答案代码,但它在单一视图订单上显示图像.

I tried Add the product image to Woocommerce my account order view answer code, but it displays the image on single view orders.

推荐答案

更新

您可以使用以下内容在 WooCommerce 我的帐户上添加产品缩略图 >订单列表,在订单号旁边:

You can use the following to add product thumbnails on WooCommerce My account > Orders list, beside the order number:

add_action( 'woocommerce_my_account_my_orders_column_order-number', 'my_account_orders_product_thumbnails', 20, 1 );
function my_account_orders_product_thumbnails( $order ) {
    echo '<a href="'. wc_get_endpoint_url('view-order') . $order->get_id()  . '/' . '">' . '#' . $order->get_order_number() . '</a>';

    // Loop through order items
    foreach( $order->get_items() as $item ) {
        $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 ) {
            echo '&nbsp;' . $thumbnail;
        }
    }
}

或者您可以在订单号后添加带有产品缩略图的新列,例如:

Or you can add a new column with the product thumbnails after the order number like:

add_filter( 'woocommerce_my_account_my_orders_columns', 'filter_woocommerce_my_account_my_orders_columns', 10, 1 );
function filter_woocommerce_my_account_my_orders_columns( $columns ) {
    $new_column = array( 'order-number' => $columns['order-number']);
    unset($columns['order-number']);

    $new_column['order-thumbnails'] = '';

    return array_merge($new_column, $columns);
}


add_action( 'woocommerce_my_account_my_orders_column_order-thumbnails', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
    // Loop through order items
    foreach( $order->get_items() as $item ) {
        $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 ) {
            echo $thumbnail . '&nbsp;' ;
        }
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于在 WooCommerce 我的帐户订单列表上显示产品缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆