Showing posts with label How to set minimum qty purchase on shopping cart magento. Show all posts
Showing posts with label How to set minimum qty purchase on shopping cart magento. Show all posts

Tuesday 8 November 2016

How to set validation on checkout with minimum qty purchase magento



How to set validation on checkout with minimum qty magento

Put below code in your module/any module config.xml file after <models> block
<global>
    <events>
        <controller_action_predispatch_checkout_onepage_index>
            <observers>
                <spirit_cms_validate_checkout>
                    <class>Setblue_Banner_Model_Observer</class>
                    <method>validateCheckout</method>
                </spirit_cms_validate_checkout>
            </observers>
        </controller_action_predispatch_checkout_onepage_index>
    </events>
</global>

Next we need to create observer file to read this code. Create Observer.php in /yourmodule/Model/Observer.php and put the below code :

public function validateCheckout($observer)
{
    // Ensure we only observe once. $oObserver
    if( Mage::registry( 'restrict_checkout_flag' ) )
    {
        return $this;
    }
    else
    {
        $oQuote = Mage::getSingleton( 'checkout/cart' )->getQuote();
        $oCartItems = $oQuote->getAllItems();
        $iTotalAmount = $oQuote->getGrandTotal();
        $iGroupId = $oQuote->getCustomerGroupId();
        $iTotalQty = 0;
        foreach( $oCartItems as $oCartItem )
        {
            $iTotalQty = $iTotalQty + $oCartItem->getQty();
        }
        if($iGroupId == '2'){
            if( $iTotalQty < 15 )
            {
                $oSession = Mage::getSingleton( 'checkout/session' );
                $oSession->addError( 'Please add at least 15 items to your cart.' );
                Mage::app()->getResponse()->setRedirect( Mage::getUrl( 'checkout/cart' ) );
            }
            Mage::register( 'restrict_checkout_flag', 1, TRUE );
        }
     
    }
}