删除带有 $product_id 的项目 - Woocommerce

Remove item with $product_id - Woocommerce(删除带有 $product_id 的项目 - Woocommerce)
本文介绍了删除带有 $product_id 的项目 - Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

开发了一个功能,让客户在达到特定数量时将产品添加到购物车.

Made a function where the customer get a product added to the cart when they reach a specific amount.

客户达到第 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.

解决方案

在@Rohil_PHPBeginner & 的帮助下@WisdmLabs 我来到了这个为我完成工作的解决方案.

With help from @Rohil_PHPBeginner & @WisdmLabs I came to this solution that did the job for me.

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

推荐答案

我认为您对 remove_cart_item 的使用不正确.如果您浏览文档,您会发现它接受cart_item_key 作为参数(如评论中提到的 wisdmLabs).

I think you're using remove_cart_item incorrectly. If you go through the documentation, you will find that it accepts cart_item_key as parameter (as wisdmLabs mentioned in comment).

你是这样使用它的:

WC()->cart->remove_cart_item($product_3);

试试这个:

WC()->cart->remove_cart_item($cart_item_key);

更新该行后,我认为您将能够删除产品.

After updating that line, I think you will able to remove product.

这篇关于删除带有 $product_id 的项目 - Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)