Woocommerce删除产品时删除自定义数据
问题描述:
我已经为每个产品添加了自定义字段。但是,当用户从购物车中删除产品时,这些自定义字段的会话不会被删除。我想根据产品删除自定义字段。请帮帮我。Woocommerce删除产品时删除自定义数据
<?php add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1);
if(!function_exists('wdm_remove_user_custom_data_options_from_cart'))
{
function wdm_remove_user_custom_data_options_from_cart($cart_item_key)
{
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach($cart as $key => $values)
{
if ($values['wdm_user_custom_data_value'] == $cart_item_key)
print_r($woocommerce->cart->cart_contents[ $key ]);
//unset($woocommerce->cart->cart_contents[ $key ]);
}
}
}
答
尝试下面的代码:
add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1);
if(!function_exists('wdm_remove_user_custom_data_options_from_cart'))
{
function wdm_remove_user_custom_data_options_from_cart($cart_item_key)
{
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach($cart as $key => $values)
{
if ($_REQUEST['remove_item'] == $key)
{
unset($woocommerce->cart->cart_contents[ $key ]);
}
}
}
}