0

I used a cart observed to block 1 single item in the cart and then display a notification to the user like a notification style "you coold select just one item" the problem that it's appears to me the error log magento first then when I return to the home page. I find my message displayed. I just want to display it in my page and not also in the error log page.

config.xml

        <events>
            <catalog_product_type_prepare_full_options>
                <observers>
                    <Amir_Prince_observer>
                        <class>Amir_Prince/ObserverCountItemCart</class>
                        <method>limiter_panier</method>
                    </Amir_Prince_observer>
                </observers>
            </catalog_product_type_prepare_full_options>
        </events>

My Observer:

class Amir_Prince_Model_ObserverCountItemCart{
    public function limiter_panier(Varient_Event_Observer $observer) {

    $session = Mage::getSingleton('checkout/session');
    $quote = $session->getQuote();

    if($quote->getItemsCount()>=1){
    $message = 'You can select only one item.';
    $session->addError($message); 
    // Mage::thrownewException($message); // the log error commented
    throw new Exception($message);// the frontend notification

      }
    }
}
  • what event are you using? – Marius Oct 27 '15 at 9:43
  • my event: catalog_product_type_prepare_full_options – Miroo Oct 27 '15 at 10:25
  • where is that event dispatched? I cannot find the Mage::dispatchEvent() call in the code. – Marius Oct 27 '15 at 12:06
  • what event you want to say, in the config.xml ? look I post you what I have done in my question on the top – Miroo Oct 27 '15 at 13:18
  • Not your code...I wanted to know the core file where this event is dispatched. To get and idea what happens before or after your observer is called. – Marius Oct 27 '15 at 13:29
0

Instead of throw new Exception($message); you can try this:

$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();
}
Mage::app()->getResponse()->setRedirect($refererUrl);
Mage::app()->getResponse()->sendResponse();
exit;

this will redirect to the previous page and you will see the notice above the page.

[EDIT]
and add this method in your observer

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;
}
  • i tried it and look the result: (364 Fatal error) at this line : if (!$this->_isUrlInternal($refererUrl)) – Miroo Oct 27 '15 at 14:37
  • Sorry. I forgot a method. See my update. – Marius Oct 27 '15 at 14:42
  • should i put some url in the mathod in the commented part: /*Url must start from base secure or base unsecure url*/ this time i havn't an error against but a blank page. I wonder if it's my page that contains an error or in the observ. your code it refreshed me well result.phtml page but no content. I try to go from result.phtml to cart and returned backwards or just refresh the page, and it shows me although my content. ! – Miroo Oct 27 '15 at 15:20
  • I verified my page code, there's no mistake. can we do a redirect to cart which is the next page and display the message above, otherwise if it's not possible display it in the home page. – Miroo Oct 28 '15 at 15:30

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.