【问题标题】:Add order item image only if the product exist on Woocommerce order view仅当 Woocommerce 订单视图中存在产品时才添加订单商品图片
【发布时间】:2019-01-21 17:04:13
【问题描述】:

在 woocommerce 我的帐户查看订单页面上,我可以使用以下答案代码添加产品图片:
Add the product image to Woocommerce my account order view

但我有一个问题导入的订单有关,其中产品不存在

因此,在查看这些订单时,会出现空白页错误。

知道如何避免这个问题吗?

【问题讨论】:

    标签: php wordpress woocommerce thumbnails orders


    【解决方案1】:

    所以如果我理解得很好,订单中的产品有时可能不存在于 Woocommerce 中。此代码版本测试产品是否存在,如果不存在,则仅返回订单商品名称。

    尝试以下方法:

    // 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' ) ) {
            // Get the WC_Product object (from order item)
            $product   = $item->get_product(); 
    
            // Testing if the product exist in Woocommerce <== UPDATE
            if( $product && is_object( $product ) ) {
                // Get the product thumbnail (from product object)
                $thumbnail = $product->get_image(array( 36, 36)); 
                // Avoiding empty thumbnail (updated)
                if( $product->get_image_id() > 0 )
                    $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
            } else {
                // When product doesn't exist, we get the name from the order item (with no thumbnail)
                $item_name = $item->get_name();
            }
        }
        return $item_name;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。我希望它会起作用。

    【讨论】:

    • 我复制了你的代码,它可以工作 gr8 感谢@LoicTheAztec
    • @ParthShah 很高兴它可以正常工作,因为我不确定,因为我无法对其进行真实测试。
    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 2022-01-21
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多