2014-12-03 84 views
0

我已經爲每個產品添加了自定義字段。但是,當用戶從購物車中刪除產品時,這些自定義字段的會話不會被刪除。我想根據產品刪除自定義字段。請幫幫我。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 ]); 
    } 


    } 

}

回答

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