• adding code to limit the quantity of certain items does not work when your plugin is activated. Im using the woocommerce_add_to_cart_validation filter:

    function limit_cart_quantity_from_category( $passed, $product_id, $quantity ) {
        $category_slug = 'livret';
        $max_quantity = 3; // Maximum quantity allowed per order
    	
    
        // Check if product belongs to the specified category
        if ( has_term( $category_slug, 'product_cat', $product_id ) ) {
            // Get the total quantity of items in the cart from the specified category
            $total_quantity = 0;
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( has_term( $category_slug, 'product_cat', $cart_item['product_id'] ) ) {
                    $total_quantity += $cart_item['quantity'];
                }
            }
    
            // Check if the total quantity plus the quantity being added exceeds the maximum quantity
            if ( $total_quantity + $quantity > $max_quantity ) {
                wc_add_notice( sprintf( __( 'Sorry, you can only add %d items from this category to the cart.', 'your-textdomain' ), $max_quantity ), 'error' );
                $passed = false; // Prevent the item from being added to the cart
    			remove_all_filters("woocommerce_add_to_cart_validation", 10);
    			remove_all_filters("woocommerce_add_to_cart", 10);
    			return $passed;
            }
        }
        
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_quantity_from_category', 10, 3 );

    the code works and the notice appears at the right moment, but the cart gets the undesired items anyways…

    I cant provide a temporary account for you to troubleshoot > plugin and wordpress are at the latest versions.

    ps ive donated to encourage you to look into my issue 😉

    thanks in advance

    The page I need help with: [log in to see the link]

The topic ‘quantity limiting filter conflicts with plugin’ is closed to new replies.