1

I would like to ask for some advice on how I could show a Javascript alert to customers if their current shopping cart value is above say £300.

I am using an excellent observer based module from Inchoo that allows me to set maximum order values via admin. However, on the from end of our site we are using Ajax to add to the shopping cart so I would need to modify the Observer.php file in order to throw an exception in the form of a Javascript alert because the page simply freezes when the order value reaches the limit.

So far I have tried to add a

Mage::throwException("Quantity cannot exceed the maximum cart value ".$formattedPrice->getSingleOrderTopAmount());

to the Observer.php file but it hasn't worked as intended, also should I use the Incoo Observer.php or create a new one inside the Ajax add to cart module?

Observer.php from Inchoo module

    <?php

    class Inchoo_MaxOrderAmount_Model_Observer
{
    private $_helper;

    public function __construct() 
    {
        $this->_helper = Mage::helper('inchoo_maxorderamount');
    }

    /**
     * No single order can be placed over the amount of X
     */
    public function enforceSingleOrderLimit($observer)
    {
        if (!$this->_helper->isModuleEnabled()) {
            return;
        }

        $quote = $observer->getEvent()->getQuote();

        if ((float)$quote->getGrandTotal() > (float)$this->_helper->getSingleOrderTopAmount()) {

            $formattedPrice = Mage::helper('core')->currency($this->_helper->getSingleOrderTopAmount(), true, false);

            Mage::getSingleton('checkout/session')->addError(
                $this->_helper->__($this->_helper->getSingleOrderTopAmountMsg(), $formattedPrice));

            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
    }

    /**
     * An email is sent to admins when an order over X total price is placed in order to investigate
     */
    public function pushOrderAmountNotification($observer)
    {
        $order = $observer->getEvent()->getOrder();

        if ((float)$order->getGrandTotal() < (float)$this->_helper->getOrderAmountNotification()) {
            return;
        }

        $storeId = $order->getStoreId();

        if (!$this->_helper->isModuleEnabled()) {
            return;
        }  

        try {
            $notifyToEmails = $this->_helper->getNotifyToEmails();

            if (empty($notifyToEmails)) {
                return;
            }

            $templateId = $this->_helper->getOrderAmountNotificationEmailTemplate($storeId);
            $mailer = Mage::getModel('core/email_template_mailer');            

            foreach ($notifyToEmails as $entry) {
                $emailInfo = Mage::getModel('core/email_info');
                $emailInfo->addTo($entry['email'], $entry['name']);
                $mailer->addEmailInfo($emailInfo);
            }

            $mailer->setSender(array(
                'name' => $this->_helper->getStoreEmailAddressSenderOption('general', 'name'),
                'email' => $this->_helper->getStoreEmailAddressSenderOption('general', 'email'),
            ));

            $mailer->setStoreId($storeId);
            $mailer->setTemplateId($templateId);
            $mailer->setTemplateParams(array(
                'order' => $order,
            ));

            $mailer->send();


        } catch (Exception $e) {
            Mage::logException($e);
        }
    }
}

My question is where would I add the function to use a Javascript alert?

1 Answer 1

1

You can use Inchoo's observer.php and modify below code

Mage::getSingleton('checkout/session')->addError($this->_helper->__($this->_helper->getSingleOrderTopAmountMsg(), $formattedPrice));

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
Mage::app()->getResponse()->sendResponse();
exit;

with your custom code to show alert box.

1
  • Adding an exit into any code is bad style. The code is untestable and finding bugs might be quite hard Dec 27, 2013 at 19:04

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.