1

please can you help to add a custom variable in the ajax popup cart?

I want to show a custom text under "Your product is added to the cart"

example: your product price is 45€, if you buy another item your shipping it will be free !

i tried to add a variable in cartcontroller.php but doesn't work,...what can I do?

thanks in advance

Alberto

Update

if (!$this->_getSession()->getNoCartRedirect(true)) {
                if (!$cart->getQuote()->getHasError()){
                    $message = $this->__( $this->__('%s was added to your shopping cart.') .'<br/><br/> <button class="button" onclick="setLocation(\''.Mage::getUrl('checkout/cart').'\');"><span><span>'. $this->__('View Cart') .'</span></span></button><button class="button" onclick="setLocation(\''.Mage::helper('checkout/url')->getCheckoutUrl().'\');"><span><span>'. $this->__('Checkout') .'</span></span></button>', Mage::helper('core')->escapeHtml($product->getName()));
                    $response['status'] = 'SUCCESS';
                    $response['message'] = $message;

                    $this->loadLayout();

this is the original part of cartcontroller.php,in this part of code after "%s was added to your shopping cart" i want to add a variable with the text inside.

i tried

$message = $this->__( $this->__('%s was added to your shopping cart.') echo"text";

or simply

    $mytext="mytext";    
$message = $this->__('%s was added to your shopping cart.'); echo $mytext;
  • what you have tried please update the code. – Keyul Shah Nov 7 '14 at 10:23
0

Try

$mytext="mytext";
$productName = Mage::helper('core')->htmlEscape($product->getName());
$message = $this->__('%s was added to your shopping cart.', $productName) . ' ' . $this->__($mytext);

$message = $this->__('%s was added to your shopping cart.'); - assign a string to a variable.

echo $mytext; - Output info to screen

  • Thanks, now it's okay, I added different fields now is perfect :) – Alberto Nov 11 '14 at 8:49
0

The success/notice/error messages like this are actually stored in the session, you don't output them directly with an echo (you shouldn't be adding an echo into a controller at all). Not sure if your core code has been altered (which you never need to do), but the section of core you are referring to should be something more along the lines of:

if (!$this->_getSession()->getNoCartRedirect(true)) {
    if (!$cart->getQuote()->getHasError()) {
        $message = $this->__('%s was updated in your shopping cart.', Mage::helper('core')->escapeHtml($item->getProduct()->getName()));
        $this->_getSession()->addSuccess($message);
    }
    $this->_goBack();
}

In your original code you are missing a key element, the line $this->_getSession()->addSuccess($message);. This is the line which adds the success message to the section. You will commonly also see this happen in the following format:

Mage::getSingleton('customer/session')->addSuccess('Some success message');

After this, just as importantly you have $this->_goBack(); which redirects the request so the message can be pulled from the session and rendered out in the next request.

Whilst you can add a success/notice/error message to the session at any point, all of the work to enable the message to be displayed is done in the controller after loading the layout by a call to the method _initLayoutMessages():

$this->loadLayout()
$this->_initLayoutMessages('customer/session');
$this->renderLayout();

Note that there are a fair few different options for which session namespace to add the message into, with customer/session being just one. The full list (of frontend namespaces) is as follows:

catalog/session
checkout/session
catalogsearch/session
customer/session
paypal/session
review/session
tag/session'
wishlist/session
core/session

If you add a message into a particular session namespace, the same namespace must be passed as and argument when calling _initLayoutMessages() in order for that message to be rendered out (with the exception of core/session which is always rendered out). You can also pass multiple session namespaces as an array:

$this->_initLayoutMessages(array('catalog/session', 'customer/session'));

As long as you add a message into the session before the call to _initLayoutMessages() then it will be displayed when the page renders and the global_messages/messages block is built, but because you are often not going to be able to make a decision about whether a message should be added before the call to _initLayoutMessages(), this is the reason why you normally redirect the request at some point after adding the message so that the next request can render the message when it calls _initLayoutMessages().

So the flow is as follows

  1. Request reaches controller
  2. Layout is loaded, messages are initialised, layout is rendered
  3. Some logic is run which determines a message needs to be displayed
  4. Message is added to the session
  5. Request is redirected to some controller action
  6. Request reaches controller
  7. Layout is loaded, messages are initialised, layout is rendered
  8. Message added in previous request is displayed.

In your particular application all you need to do is alter the message added to the session and then either ensure this has been done before the call to _initLayoutMessages(), or ensure you add in a redirect so that the message renders in the next request - if you are changing the message where I think you are, you'll need a redirect. So just append the extra text you need to the $message variable:

$message = $this->__('%s was updated in your shopping cart. Some new text.', Mage::helper('core')->escapeHtml($item->getProduct()->getName()));

A side note, if you need to make the change in Mage_Checkout_CartController you should be rewriting this class not changing the core class directly.

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.