php 删除带有 $product_id 的项目 - Woocommerce
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30569881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove item with $product_id - Woocommerce
提问by Mathias Asberg
Made a function where the customer get a product added to the cart when they reach a specific amount.
制作了一个功能,客户在达到特定数量时将产品添加到购物车。
Example of when customer reaches level 3 and get the product added.
客户达到第 3 级并添加产品的示例。
// Bonus products
$product_1 = '4751';
$product_2 = '4752';
$product_3 = '4753';
// Get cart value in a clean format
$cart_total = WC()->cart->get_cart_subtotal();
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);
$sum_raw = $cart_value;
// Set the sum level
$level3 = '1500';
// Check sum and apply product
if ($sum_raw >= $level3) {
// Cycle through each product in the cart and check for match
$found = 'false';
foreach (WC()->cart->cart_contents as $item) {
global $product;
$product_id = $item['variation_id'];
if ($product_id == $product_3) {
$found = 'true';
}
}
// If product found we do nothing
if ($found == 'true') {}
// else we will add it
else {
//We add the product
WC()->cart->add_to_cart($product_3);
If customer decides to remove item's so this statement is true i want to be able to remove it again.
如果客户决定删除项目,那么此声明是正确的,我希望能够再次删除它。
if ($sum_raw < $level3) {
// Trying to remove item
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['variation_id'] == $product_3) {
//remove single product
WC()->cart->remove_cart_item($product_3);
}
}
}
Am do not manage to remove the product from cart. Any ideas what am doing wrong here? Have been searching around without finding any solution that works for me.
我没有设法从购物车中删除产品。任何想法在这里做错了什么?一直在四处寻找,但没有找到任何适合我的解决方案。
Solution
解决方案
With help from @Rohil_PHPBeginner & @WisdmLabs I came to this solution that did the job for me.
在@Rohil_PHPBeginner 和@WisdmLabs 的帮助下,我找到了为我完成这项工作的解决方案。
global $woocommerce;
// Check if sum
if ($sum_raw < $level3) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['variation_id'] == $product_3) {
//remove single product
$woocommerce->cart->remove_cart_item($cart_item_key);
}
}
}
采纳答案by Rohil_PHPBeginner
I think you're using remove_cart_item
incorrectly. If you go through the documentation, you will find that it accepts cart_item_keyas parameter (as wisdmLabs mentioned in comment).
我认为你使用remove_cart_item
不正确。如果您阅读文档,您会发现它接受cart_item_key作为参数(如评论中提到的wisdmLabs)。
You are using it like so:
你像这样使用它:
WC()->cart->remove_cart_item($product_3);
Try this instead:
试试这个:
WC()->cart->remove_cart_item($cart_item_key);
After updating that line, I think you will able to remove product.
更新该行后,我认为您将能够删除产品。
回答by Mehdico
Use this for latest versions of WooCommerce:
将此用于最新版本的 WooCommerce:
$cartId = WC()->cart->generate_cart_id( 'PRODUCT ID' );
$cartItemKey = WC()->cart->find_product_in_cart( $cartId );
WC()->cart->remove_cart_item( $cartItemKey );
replace PRODUCT ID with yours.
用您的产品 ID 替换产品 ID。