[英]Remove specific product from WooCommerce cart only on a specific page
[英]Remove specific product variation from cart in WooCommerce
我想從 wordpress、woocommerce 的購物車中刪除一個變體產品。
我可以刪除一個簡單的產品,但不能刪除一個變體產品。 使用此代碼,我可以刪除一個簡單的產品。 我嘗試傳入變體 ID 和父變體 ID。 我不知道為什么它不起作用,如果有人有線索,我將不勝感激。
$product_cart_id = WC()->cart->generate_cart_id( $aCurrentUserDataItem->id );
$cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
您還可以使用如下的 foreach 循環來定位並從購物車中刪除特定的變體 ID:
$remove_variation_id = 41; // The variation id to remove
// loop through cart items
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
// If the targeted variation id is in cart
if ( $item['variation_id'] == $remove_variation_id ) {
WC()->cart->remove_cart_item( $item_key ); // we remove it
break; // stop the loop
}
}
測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.