如何仅为WooCommerce中的非变体产品显示自己的数据



我试图使用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
请问问题出在哪里?

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;
}

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

最新更新