我在每个产品中添加了自定义字段。但是,当用户从购物车中删除产品时,这些自定义字段的会话不会被删除。我想根据产品删除自定义字段。请帮帮我。
<?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 ] );
}
}
}
答案 0 :(得分:0)
请尝试以下代码:
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 ] );
}
}
}
}