1

I've installed an add to cart extension and has its own controller.php file on Magento 1.14.2 and have the full page cache enabled (FPC). When FPC is enabled the Magento notifications for example the screenshot below does not appear on the current page, the notification does appear on another page, hence the notification is cached. The function does work when FPC is disabled.

The notification code is question is the addSuccess and addError (the code below). Full code below for reference. How can I show the notifications when FPC is enabled?

enter image description here

Mage::getSingleton('checkout/session')->addSuccess($message);
Mage::getSingleton('checkout/session')->addException($e, $this->__('Cannot add item to shopping cart'));

Full code below:

public function indexAction() {
    $redirect_url = Mage::getStoreConfig('addproducts/general/redirect_url');
    $id=$_POST['product_hide_id'];
    $qty_flag=0;
    $qty_flage =0;      
    if ($id=="") 
    {
        $this->_redirectReferer();
    }
    foreach($id as $productIds)
    {
        $qty = $_POST['qty_'.$productIds];

        try 
        {
            if ($qty <= 0)  {
                continue;
            }else{

                $cart = Mage::getModel('checkout/cart')->init();
                $product = Mage::getModel('catalog/product')->load($productIds);
                $eventArgs = array(
                    'product' => $product,
                    'qty' => $qty,
                    'request' => $this->getRequest(),
                    'response' => $this->getResponse(),
                );
                Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);
                $add_cart=$cart->addProduct($product, $eventArgs);
                Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);
                $cart->getQuote()->setTotalsCollectedFlag(false);
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
                Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product));
                $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());    
                Mage::getSingleton('checkout/session')->addSuccess($message);
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

            }
        }
        catch (Mage_Core_Exception $e) {

            if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
               Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage());
            }
            else {

                if($redirect_url == 1)  {
                    if($qty_flage == 1) {         
                        Mage::getSingleton('checkout/session')->addError($e->getMessage());
                    }
                    $qty_flage = 1;                         
                }else{
                    Mage::getSingleton('checkout/session')->addError($e->getMessage());
                }
            }
        }
        catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addException($e, $this->__('Cannot add item to shopping cart'));
        }
        $qty_flag=1;
    }

    if($qty_flag){ 

        $cart->save();
        if($redirect_url == 1) {            
            $this->_redirect('checkout/cart');
        }else{      
            $this->_redirectReferer();
        }
    }else{ 


        //Mage::getSingleton('core/session')->setSomeSessionVar($message);  
        Mage::getSingleton('core/session')->addError('Please insert the Quantity in Qty Box.');
        $this->_redirectReferer();
    }
}

public function singleAction() {
    $redirect_url = Mage::getStoreConfig('addmultipleproducts/general/redirect_url');
    $id=$_POST['product_hide_id'];
    $qty_flage =0; 

    if ($id=="") 
    {
        $this->_redirectReferer();
    }
    foreach($id as $productIds)
    {
        $qty = $_POST['qty_'.$productIds];

        try 
        {
            if ($qty < 0) {
                continue;
            }else{

                if($_POST['pid_'.$productIds] != "")
                {
                $cart = Mage::getModel('checkout/cart')->init();
                $product = Mage::getModel('catalog/product')->load($productIds);
                $eventArgs = array(
                    'product' => $product,
                    'qty' => $qty,
                    'request' => $this->getRequest(),
                    'response' => $this->getResponse(),
                );
                Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);
                $add_cart=$cart->addProduct($product, $eventArgs);
                Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);
                $cart->save();
                $cart->getQuote()->setTotalsCollectedFlag(false);
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
                Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product));
                $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());    
                Mage::getSingleton('checkout/session')->addSuccess($message);
                Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
                }
            }               
        }
        catch (Mage_Core_Exception $e) {
            if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
                Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage());
            }
            else {
                if($redirect_url == 1)  {
                    if($qty_flage == 1){          
                        Mage::getSingleton('checkout/session')->addError($e->getMessage());
                    }
                    $qty_flage = 1;                         
                }else{
                    Mage::getSingleton('checkout/session')->addError($e->getMessage());
                }
            }
        }
        catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addException($e, $this->__('Cannot add item to shopping cart'));
        }
    }

    $cart->save();
    if($redirect_url == 1) {            
        $this->_redirect('checkout/cart');
    }else {     
        $this->_redirectReferer();
    }
}

Thank you

2

You'll most likely need to add a cache.xml, a container.php and define a block to the module.

Since FPC normally has messages hole punched, you could refer to the internal blocks used and point to them instead of the modules.

See:

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.