Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果卡值<;x_Php_Wordpress_Action_Product - Fatal编程技术网

Php 如果卡值<;x

Php 如果卡值<;x,php,wordpress,action,product,Php,Wordpress,Action,Product,我希望有人能帮忙。我已经设置了一些代码(),如果购物车的价值超过50美元,它会将产品添加到我的购物车中 这很有效。但是,如果值小于50美元,我想确保产品不在购物车中。比如说,用户添加了一个产品,添加了额外的产品,但是他们删除了一个产品,所以价值低于50美元(不包括添加的新产品!)。。。我希望它删除添加的产品 我试图实现上述帖子中提到的代码: add_action( 'template_redirect', 'add_product_to_cart' ); function add_produ

我希望有人能帮忙。我已经设置了一些代码(),如果购物车的价值超过50美元,它会将产品添加到我的购物车中

这很有效。但是,如果值小于50美元,我想确保产品不在购物车中。比如说,用户添加了一个产品,添加了额外的产品,但是他们删除了一个产品,所以价值低于50美元(不包括添加的新产品!)。。。我希望它删除添加的产品

我试图实现上述帖子中提到的代码:

add_action( 'template_redirect', 'add_product_to_cart' ); 

function add_product_to_cart() {
// Bonus products
$product_1 = '4751'; 
$product_2 = '4752'; 
$product_3 = '24711'; 

// 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 = '50';

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


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

}
add_操作('template_redirect','add_product_to_cart');
函数添加商品到购物车(){
//奖金产品
$product_1='4751';
$product_2='4752';
$product_3='24711';
//以干净的格式获取购物车值
$cart_total=WC()->cart->get_cart_subtotal();
$cart_total=html_entity_decode($cart_total,ENT_引号,'UTF-8');
$cart\u total\u format=带标签($cart\u total);
$cart\u value=preg\u过滤器(“/[^0-9]/”,“,$cart\u total\u格式);
$sum_raw=$cart_值;
//设定总和水平
$level3='50';
//检查和并应用乘积
如果($sum_raw>=$level3){
//循环检查购物车中的每个产品并检查是否匹配
$found='false';
foreach(WC()->cart->cart\u内容为$item){
全球$产品;
$product_id=$item['variation_id'];
如果($product\U id==$product\U 3){
$found='true';
}
}
//如果发现产品,我们什么也不做
如果($found='true'){}
//否则我们将添加它
否则{
//我们添加产品
WC()->购物车->将购物车添加到购物车($product_3);
}
}
//检查总数是否正确
如果($sum_raw<$level3){
foreach($woocommerce->cart->get_cart()作为$cart\u item\u key=>$cart\u item){
如果($cart\u item['variation\u id']==$product\u 3){
//删除单个产品
$woocommerce->cart->remove\u cart\u item($cart\u item\u key);
}
}
}
}
但它并没有移除产品。:/任何人都知道为什么它不能工作,或者是否有更好的解决方案来检查购物车:如果购物车>50美元添加一个产品。。。如果它发生变化且价格低于50美元,请删除相同的产品

并将新添加的产品从检查中排除(因此<$50不包括刚通过编程添加的产品)


请帮忙(

也许您必须更改此行:

$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);
至(加-减):


尝试此代码…您当前正在尝试更新“添加”操作,而它应该更新“删除”操作


谢谢,我试过了,但似乎什么都没用。你可以在这里测试:-添加一个超过50美元的物品,它将添加25美元的礼品卡,但是删除该物品,使其价值低于50美元,礼品卡将保持不变:(谢谢你的帮助,但这似乎无法解决问题:(
$cart_value = preg_filter("/[^0-9\-]/", "", $cart_total_format);
add_action( 'template_redirect', 'remove_product_from_cart' );
function remove_product_from_cart() {
    // Run only in the Cart or Checkout Page
    if( is_cart() || is_checkout() ) {


        /*
         GET THE CART TOTAL 
         */

        // Set the product ID to remove
        $prod_to_remove = PRODUCT-ID-TO-BE-REMOVED;

        // Cycle through each product in the cart
        foreach( WC()->cart->cart_contents as $prod_in_cart ) {
            // Get the Variation or Product ID
            $prod_id = ( isset( $prod_in_cart['variation_id'] ) && $prod_in_cart['variation_id'] != 0 ) ? $prod_in_cart['variation_id'] : $prod_in_cart['product_id'];
            // Check to see if IDs match
            if( $prod_to_remove == $prod_id ) {
                // Get it's unique ID within the Cart
                $prod_unique_id = WC()->cart->generate_cart_id( $prod_id );
                // Remove it from the cart by un-setting it
                unset( WC()->cart->cart_contents[$prod_unique_id] );
            }
        }
    }
}