Skip to content
Advertisement

Add to cart validation in WooCommerce based on attribute value and product quantity from the cart page

Actually, I am trying to set a limit for adding to cart for some product ids based on product attribute.

What i’ve done is:

add_action( 'woocommerce_add_to_cart_validation', 'max_add_to_cart', 10, 2 );
function max_add_to_cart( $cart_item_key, $product_id ) {

    $product = wc_get_product( $product_id );
    $maxqty = $product->get_attribute('quantite_max');
    $nbcart = get_item_qty_cart($product_id);
    if (!empty($maxqty) && $maxqty <= $nbcart) {
        wc_add_notice("cant have more than " . " {$maxqty} x {$product->get_title()} in cart.", 'error');
        return false;

    }
    return true;
}

It works in a loop, but on the product page, it doesn’t take into account quantity input.

Is there a way to get quantity add input in my function?

Advertisement

Answer

  1. woocommerce_add_to_cart_validation contains 5 parameters (the last 2 are optional)

    • $passed = return true by default
    • $product_id = the product id, this gives you access to wc_get_product( $product_id ) to get the product object
    • $quantity = current quantity you want to add to the cart
  2. get_item_qty_cart does not seem to exist in your code

  3. Explanation via comment tags added in my answer

So you get:

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Get product object
    $product = wc_get_product( $product_id );
    
    // Flag
    $flag = false;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get the product attribute value
        $max_qty = $product->get_attribute( 'quantite_max' );
        
        // NOT empty
        if ( ! empty( $max_qty ) ) {
            // If new added quantity greater than max quantity
            if ( $quantity > $max_qty ) {
                $flag = true;
            // WC Cart NOT null & Cart is NOT empty
            } elseif ( ! is_null( WC()->cart ) && ! WC()->cart->is_empty() ) {
                // Get cart items quantities
                $cart_item_quantities = WC()->cart->get_cart_item_quantities();
                
                // Product quantity in cart
                $product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : null;
                
                // New added quantity + product quantity in cart greater than max quantity
                if ( ( $quantity + $product_qty_in_cart ) > $max_qty ) {
                    $flag = true;
                }
            }
        }
    }
    
    // True
    if ( $flag ) {
        wc_add_notice( sprintf( __( 'Cant have more than %s x %s in cart', 'woocommerce' ), $max_qty, $product->get_name() ), 'error' );
        $passed = false;
    }
    
    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement