Magento: Allow checkout if cart quantity multiplies in predefined number

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Every day I get new ideas but unfortunately there is the time factor and my personal goals that don’t allow me to complete everything I want. At the beginning of this week I got interesting email from Ronald about something that was in my head since I’ve created Magento extension for custom cart validations. So this time I said to myself “Now is the moment!”:

Hi,

I read your article on custom cart validation in Magento on http://www.sessiondigital.de/blog/magento-create-custom-cart-validation-rules-developers-module/ and I have a question.

I have been breaking my head over a way to enable Magento to sell products in multiples of X, but with any product combination possible. An example would be wine bottles. Any combination of wine bottles is possible, but checkout is only possible if the total quantity is a multiple of 6. If that rule is not validated, a message for the customer is displayed and checkout is disabled.

Would your method be the way to go here, and if so, can you point me in the right direction? Do you have a working example?

Your help is much appreciated.

Cheers,

Ronald


If you want same or similar as Ronald’s request feel free to get the extension: Extendix_ValidateCartMultipleQty

Basically Ronald wanted something like this:

Shopping Cart Total Qty Rule

If in Admin you go to System -> Configiguration -> Sales -> Cart Multiple Qty Validation, you will find simple interface that allows you to:

  • Enable / Disable extension
  • Change validation multilier
  • Change validation error message template

Configuration Cart Validation

There were 2 key moments:

  1. Choosing best event for quantity validation:

    According to me and results of some of Ron’s test the best event is: sales_quote_load_after.

  2. Setting the quote as not valid:

    In Observer I had to calculate if car quantity is multiple times by defined multiplier, if not I added error message using Mage_Sales_Model_Quote::addErrorInfo():

    public function validateCartTotalMultipleQty(Varien_Event_Observer $observer)
    {
        $multiplier = 6;
        $totalQty   = 0;
    
        /** @var Mage_Sales_Model_Quote $quote */
        $quote = $observer->getEvent()->getQuote();
    
        $items = $quote->getAllVisibleItems();
    
        foreach($items as $item) {
            $totalQty += $item->getQty();
        }
    
        if(0 != $totalQty % $multiplier) {
            $quote->addErrorInfo(
                'multiple_qty',
                'Extendix_CartValidationRules',
                'not_multiple_total_cart_qty',
                sprintf('You can only order in multiples of %s. Please review your cart before checking out.', $multiplier)
            );
        }
    
        return $this;
    }
    

Your thoughts?

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Tsvetan Stoychev

Tsvetan aka. Cecko is the founder of Cecko's Lab. He is Magento addicted since Magento CE 1.2.1.2 and has worked on over 30 Magento projects. At the moment he is in charge to take care about the money flow of the company, to keep constant communication with the clients and to keep the people in the office busy.

More Posts

Follow Me:
TwitterLinkedIn

  • Ron

    Tsvetan, the extension works great. Thanks for the quick response!