2

I'm trying to add a minimum order quantity of 12 or multiples of 12 to the magento checkout.

The user should be able to buy any product combination but the final allowed quantity in the cart should be 12 or 24 or 36 and so on

How can I achieve this?

2 Answers 2

2

you can rewrite the method Mage_Checkout_OnepageController::indexAction()

and where the verification for the minimum amount is done, do the same for your restriction.
The minimum amount verification is done by:

if (!$quote->validateMinimumAmount()) {
    $error = Mage::getStoreConfig('sales/minimum_order/error_message') ?
        Mage::getStoreConfig('sales/minimum_order/error_message') :
        Mage::helper('checkout')->__('Subtotal must exceed minimum order amount');

    Mage::getSingleton('checkout/session')->addError($error);
    $this->_redirect('checkout/cart');
    return;
}

you can add something like this:

$qty = 0; 
foreach ($quote->getAllVisibleItems() as $item) {
    $qty += $item->getQty(); 
} 
if ($qty % 12 != 0) {
    $error = Mage::helper('checkout')->__('You must buy in multiples of 12.');
    Mage::getSingleton('checkout/session')->addError($error);
    $this->_redirect('checkout/cart');
    return; 
}

You can check this to see how to rewrite controllers.

6
  • Hi Marius, thank you for the quick reply. I tried to override the indexAction method but it seems that nothing happens if I do so. Am I doing it correctly? (Posted the code as a new reply) Nov 17, 2015 at 23:19
  • @user1787355 Maybe you're using checkout extension? If you're using a custom checkout, you will need to override this module's controller Nov 17, 2015 at 23:27
  • @simonthesorcerer no custom modules installed. Clean installation of Magento 1.9.1.1 Nov 17, 2015 at 23:31
  • hm okay, was just a guess Nov 17, 2015 at 23:32
  • thank you @Marius♦ it worked in the end. Took me a while to learn how to override the controller method. Nov 18, 2015 at 10:51
1

I tried to overwrite the indexAction method like so:

/app/code/local/Val/CartRestriction/controllers/OnepageController.php

<?php

require_once ('Mage/Checkout/controllers/OnepageController.php');
class Val_CartRestriction_OnepageController extends Mage_Checkout_OnepageController
{
    public function indexAction()
    {
        if (!Mage::helper('checkout')->canOnepageCheckout()) {
            Mage::getSingleton('checkout/session')->addError($this->__('The onepage checkout is disabled.'));
            $this->_redirect('checkout/cart');
            return;
        }
        $quote = $this->getOnepage()->getQuote();
        if (!$quote->hasItems() || $quote->getHasError()) {
            $this->_redirect('checkout/cart');
            return;
        }
        $qty = 0; 
        foreach ($quote->getAllVisibleItems() as $item) {
            $qty += $item->getQty(); 
        } 
        if ($qty % 12 != 0) {
            $error = Mage::helper('checkout')->__('You must buy in multiples of 12.');
            Mage::getSingleton('checkout/session')->addError($error);
            $this->_redirect('checkout/cart');
            return; 
        }
        Mage::getSingleton('checkout/session')->setCartWasUpdated(false);
        Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_secure' => true)));
        $this->getOnepage()->initCheckout();
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
        $this->getLayout()->getBlock('head')->setTitle($this->__('Checkout'));
        $this->renderLayout();

    }
}

/app/code/local/Val/CartRestriction/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Val_CartRestriction>
            <version>0.1.0</version>
        </Val_CartRestriction>
    </modules>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Val_CartRestriction before="Mage_Checkout">Val_CartRestriction</Val_CartRestriction>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

/app/etc/modules/Val_CartRestriction.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Val_CartRestriction>
            <active>true</active>
            <codePool>local</codePool>
        </Val_CartRestriction>
    </modules>
</config>

No luck :(

1
  • This actually worked after waiting for the cache to clear. Tried to clear the cache manually by clearing the cache folder on the ftp and then clearing the cache through the back-end. Nov 18, 2015 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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