【问题标题】:Remove WooCommerce Cart Condition (Minimum Amount of a Product)删除 WooCommerce 购物车条件(产品的最小数量)
【发布时间】:2021-10-31 02:45:39
【问题描述】:

我对此有点卡住..每当用户添加特定产品时,我都会尝试删除条件,在这种情况下是一盒葡萄酒

所以当我添加一瓶酒时,有一个最低量条件,所以你必须添加 3,但是当你添加一个盒子时,必须删除条件

add_action('woocommerce_after_cart_contents', 'box_special_function', 1, 1);

function box_special_function()
{
// getting cart items
    $cart = WC()->cart->get_cart();
    
    $terms = [];
// Getting categories of products in the cart
    foreach ($cart as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $terms[] = get_the_terms( $product->get_id(), 'product_cat' );
    }
// Cycling categories to find if there is a box inside of the cart
    foreach($terms as $term => $item){
        foreach($item as $key){
            if($key->name == "BOX"){
                // The only thing i did is to remove notices (which doesn't even work .-.)
                $notices = WC()->session->get('wc_notices', array());
                
                foreach($notices['error']){
                    wc_clear_notices();
                }
            }
        }
    }
    
}

我什至不能强制结帐,所以我被这个卡住了..有人可以清除我的想法吗?

【问题讨论】:

    标签: php wordpress woocommerce shopping-cart woocommerce-theming


    【解决方案1】:

    您可以更新您的最低产品添加到购物车功能,并删除对您的特定产品的验证,如下所示:

    add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_changes', 10, 2 );
    
    function custom_woocommerce_quantity_changes( $args, $product ) {
        $product_id = $product->get_id();
        $product_title = $product->get_title();
        //echo "<pre>"; print_r($product_title); echo "</pre>";
        if($product_id == 1002 || $product_title == 'BOX'){
            $args['input_value'] = 1; 
            $args['min_value'] = 1; 
            $args['max_value'] = 30;
            $args['step'] = 1;
        }else{
            $args['input_value'] = 3; // Start from this value (default = 1) 
            $args['max_value'] = 30; // Max quantity (default = -1)
            $args['min_value'] = 3; // Min quantity (default = 0)
            $args['step'] = 1; // Increment/decrement by this value (default = 1)
        }
        return $args;
    }
    

    打印您的特定产品 ID 或产品名称,并在您的 if 条件中使用相同的名称或 ID:

    if($product_id == 1002 || $product_title == 'BOX')
    

    对于其余产品集的最小值、最大值和来自“其他”部分的输入产品验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多