3

i'm a bit stuck for this.. i'm trying to remove a condition whenever the user adds a specific product, in this case a box of wines

enter image description here

So when i add a bottle of wine there's a minium amount condition so you have to add 3, but when you add a box the condition must be removed

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

I can't even force to checkout so i'm stuck with this.. can somebody clear my mind?

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Claudio
  • 31
  • 3

1 Answers1

0

You can update your minimum product add to cart functionality, and remove validation for your specific products like this:

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

print your specific product id or product name and use same name or id in your if condition :

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

For rest of product set min, max and input product validation from "else" parts.

Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23