• Resolved reeko6616

    (@reeko6616)


    I am trying to limit the order quantity of products with a specific attribute, not sure where I am going wrong, here is the code

    add_action( 'woocommerce_check_cart_items', 'wc_max_item_required_qty' );
    function wc_max_item_required_qty() {
    	$max_qty   = 50; // Max
        $qty_count = 0; // Initializing
    	$attr_val = "inflated"; //attr
    	
    	// Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ){
    		if( $cart_item['variation_id'] > 0 ){
    			// Loop through product attributes values set for the variation
    			foreach( $cart_item['variation'] as $term_slug ){
    				// comparing attribute term value with current attribute value
    				if ( $term_slug === $attr_val ) {
    					$qty_count += 1;
    				}
    			}
    		}
    	}
    
        // Display error notice avoiding checkout
        if($qty_count > $max_qty ) {
            wc_clear_notices(); // Clear all other notices
    		
            // Add an error notice (and avoid checkout).
            wc_add_notice( sprintf(__("Max %s inflated items per order.", "woocommerce"),'<strong>' . $max_qty . '</strong>',), 'error' );
        }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Limiting Order Quantity Based On Attribute’ is closed to new replies.