wordpress 如何在WooCommerce中只显示非变体产品所有者数据

v8wbuo2f  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(154)

我尝试使用woocommerce_get_item_data挂钩在购物车中显示非变体产品的一些自定义数据。即使我设置了条件,我仍然遇到变体产品的问题,并给我一个错误。
我的代码:

add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
    
    if ( empty($cart_item["variation"])) {
    
            $my_data = $cart_item["my_data"];
            $data_array = array();
        
    $data_array[] = array(
                    'key'       => "Some Key",
                    'value'       => "Some Value",
                );
    
return $data_array;
        
        }
}

变量错误:

Warning: Invalid argument supplied for foreach() in /data/0/b/0b142c6f-d1fc-41f2-b82d-95dd95505032/xxx.sk/sub/shop/wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 3741

Warning: count(): Parameter must be an array or an object that implements Countable in /data/0/b/0b142c6f-d1fc-41f2-b82d-95dd95505032/xxx.sk/sub/shop/wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 3752

请问问题出在哪里?

yvt65v4c

yvt65v4c1#

add_filter('woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2);

function display_cart_item_custom_meta_data($item_data, $cart_item) {

    if (empty($cart_item["variation"])) {

        $my_data = $cart_item["my_data"];
        $data_array = array();

        $data_array[] = array(
            'key' => "Some Key",
            'value' => "Some Value",
        );

        return $data_array;
    }
    return $item_data;
}

你应该总是返回数据,否则,它将抛出错误。

相关问题