0

Based on this snippet, which checks the quantity in cart for MAX value:

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
    $args['max_value'] = 10; // Maximum value
    return $args;
}

I'm trying to add a condition, i.e. if the item is onsale AND equals the value of SESSION variable:

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) { 
    $onsale = $product->is_on_sale();
    if ( ( $_SESSION['odertype'] = 'local_delivery' ) && $onsale ) {
        $args['max_value'] = 10; // Maximum value
        return $args;
    }
}

Unfortunately, it looks like the 'AND' portion of the IF statement is not working, as the filter seems to be applied even if the session variable is NOT set!

2

You have a typo in $_SESSION['odertype'], should be $_SESSION['ordertype']. You are also ASSIGNING a value instead of testing it for equivalency, try ==.

If in doubt you can always use var_dump($_SESSION) to get all the values stored there.

  • @bas You also have the return inside your if condition - $args should always be returned. – TheDeadMedic Oct 28 '15 at 15:27
2

Following @Burgi's answer, the correct snippet should be:

function jk_woocommerce_quantity_input_args( $args, $product ) { 
    if (
        isset( $_SESSION['ordertype'] ) && // Never assume an array index exists
        $_SESSION['ordertype'] == 'local_delivery' && // Note == "same as" operator, not = "assign"
        $product->is_on_sale()
    ) {
        $args['max_value'] = 10; // Maximum value
    }

    // Always return args for other filters/sanity
    return $args;
}
  • The isset is a very good point. I missed that in my original read through of the question. – Burgi Oct 28 '15 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.