I need ot check if the user can add the product to the cart and if not return to the product page with an error, I lsten to this event: controller_action_predispatch_checkout_cart_add.
And this is what I have written:

public function checkProductOnAdd(Varien_Event_Observer $observer){

    if($observer->getEvent()->getControllerAction()->getFullActionName() == "checkout_cart_add"){

        $productId  = Mage::app()->getRequest()->getParam('product');
        $collection = Mage::getModel('country/product')->getCollection()
                                                       ->addFieldToFilter('product_id', $productId);

        $country = Mage::getSingleton('core/session')->getRazorphynCountry;

        foreach($collection as $res){
            if(($res->allowed==0 && strpos($res->country, $country) !== false) || ($res->allowed==1 && strpos($res->country, $country) === false)){
                Mage::getSingleton('core/session')->addError(Mage::helper('country')->__("Sorry, this product isn't available in your country"));

in my config.xml:


add a new method in your controller that looks like this

protected function _isUrlInternal($url)
    if (strpos($url, 'http') !== false) {
         * Url must start from base secure or base unsecure url
        if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) === 0)
            || (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) === 0)
        ) {
            return true;
    return false;

replace NOW? with

$request = Mage::app()->getRequest();
$refererUrl = $request->getServer('HTTP_REFERER');
if ($url = $request->getParam(Mage_Core_Controller_Varien_Action::PARAM_NAME_REFERER_URL)) {
    $refererUrl = $url;
if ($url = $request->getParam(Mage_Core_Controller_Varien_Action::PARAM_NAME_BASE64_URL)) {
    $refererUrl = Mage::helper('core')->urlDecode($url);
if ($url = $request->getParam(Mage_Core_Controller_Varien_Action::PARAM_NAME_URL_ENCODED)) {
    $refererUrl = Mage::helper('core')->urlDecode($url);
$refererUrl = Mage::helper('core')->escapeUrl($refererUrl);
if (!$this->_isUrlInternal($refererUrl)) {
    $refererUrl = Mage::app()->getStore()->getBaseUrl();
  • Thanks, the returning url is fine(check with log), however the product is added to the cart and magento redirect me to the shopping cart page(if I use return) or a blank page if I use die() – Razorphyn Jun 8 '15 at 15:06
  • See my edit. I forgot one line at the end ` Mage::app()->getResponse()->sendResponse();` You can add an exit or die() after that line. – Marius Jun 8 '15 at 15:09
  • Ok, I have added this: Mage::app()->getRequest()->setParam('return_url',$refererUrl); just before: Mage::app()->getResponse()->setRedirect($refererUrl); and now it looks like it works, let me know what you think – Razorphyn Jun 8 '15 at 15:58
  • If it works for you then it the best solution :). – Marius Jun 8 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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