php – 用$product_id – Woocommerce删除项目

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 用$product_id – Woocommerce删除项目脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
一个功能,客户在达到特定数量时将产品添加购物车.

客户达到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 ($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);
        }
    }
}

我不设法从购物车中取出产品.任何想法在这里做错什么?一直在寻找,找不到任何适用于我的解决方案.

在@Rohil_PHPBeginner&amp; @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);
        }
    }
}
我想你正在使用remove_cart_item错误.如果您通过 documentation,您会发现它接受cart_item_key作为参数(如注释中提到的wisdmLabs).

而您正在使用它像WC() – > cart-> remove_cart_item($product_3);而不是wc() – > cart-> remove_cart_item($cart_item_key);

所以替换那条线,我想你可以删除产品.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 用$product_id – Woocommerce删除项目全部内容,希望文章能够帮你解决php – 用$product_id – Woocommerce删除项目所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。