0

I want to show "Text 1", when the Product with the ID 1 is in the cart. I want to show "Text 2", when the Product with the ID 1 is NOT in the cart.

My code already works for "Text 1". This Text is shown on the right place (in the cart header). But I don´t know how to setup, that there is a "Text 2" shown, when the product with ID 1 is not in cart.

I´m really new into magento and don´t know exactly how to do that.

Here is my code, I came so far until now:

 <?php
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$found = false;
foreach ($items as $item) {
    if ($item->getProductId() == 1){
        $found = true;
        echo "Text 1";
        break;
    } 
}
?>
1

You are halfway there,

if (!$found) {
    echo "text 2";
}
| improve this answer | |
0

Welcome to magento. Make sure to learn magento 2 ;)

Where exactly do you want this text to display? In the cart heading or as part of the actual line item display?

As an example, I'll demonstrate one way to display the text in the line item, as that seems like the most likely scenario.

Cart line items are rendered using the templates located in the path /app/design/frontend/base/default/template/checkout/cart/item/default.phtml

things to note:

  • the actual files has changed slightly during magento versions, but the general location is the same
  • some line items have their own renderers located in their respective folders (example downloadable/bundles)

You can find the respective product type line item render template(s) in the layout xml.

refer to file: /app/design/frontend/base/default/layout/checkout.xml

<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
                <action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
                <action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>

So, to make the text appear for a specific product, with a specific ID, you'd simply need to edit the template .phtml, and place the required logic to output your string.

Please do make a copy of the template from the base to your theme folder. Never edit the base templates directly.

| improve this answer | |
  • Thank you for your quick answer - and sorry for being such a newbie ;) My code already works for "Text 1". This Text is shown on the right place (in the cart header). But I don´t know how to setup, that there is a "Text 2" shown, when the product with ID 1 is not in cart. – Aurya Feb 22 '15 at 16:20
  • @Aurya Ahh, ok, misread the question, however, this answer could still relate/help someone, so I placed a new answer more specific to your needs – ProxiBlue Feb 22 '15 at 22:49
0

There is no need to iterate the quote object. You can use the method getItemByProduct on the quote object to find the quote item id of a product in the quote.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$item = $quote->getItemByProduct($product);
($item !== false)?echo "text 1":"text2";
| improve this answer | |

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.