1

在 Woocommerce 中,我在 MyAccount 中自定义我的视图顺序。我已经使用此答案代码添加了产品图片:将产品图片添加到 Woocommerce 我的帐户订单视图

现在我想将产品 SKU 添加到查看订单页面,但是我不知道如何获取它。

有人有想法吗?

4

1 回答 1

0

用以下代码替换您的代码以在订单项目中显示产品 SKU:

// 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)
        // The thumbnail
        if( $product->get_image_id() > 0 )
            $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
        // The SKU
        if( $sku = $product->get_sku() )
            $item_name .= '<br><div class="product-sku">' . $sku . '</div>';
    }
    return $item_name;
}

代码在您的活动子主题(活动主题)的 function.php 文件中。它应该有效。

于 2018-11-06T17:40:31.780 回答