I want to show a direct message if a customer wants to buy more items of an article than I have in stock. I allow backorders but the message "%s is not available in the requested quantity. %s of the items will be backordered." is only shown in their cart. But I also want to show it directly within the normal "%s was added to your shopping cart."-message. Because some customers are going directly to checkout and do not see the backorder-message in their cart. Is there a way to add this? I think I have to modify the code
"$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));"
in
"app/code/core/Mage/Checkout/controllers/CartController.php".
Regards,
Marcus
Solved! Go to Solution.
You can easily handle this using event. Don't modify core file. Try following way:
app/etc/modules/SR_MagentoCommunity.xml
<?xml version="1.0"?> <config> <modules> <SR_MagentoCommunity> <active>true</active> <codePool>local</codePool> </SR_MagentoCommunity> </modules> </config>
app/code/local/SR/MagentoCommunity/etc/config.xml
<?xml version="1.0"?> <config> <modules> <SR_MagentoCommunity> <version>0.0.0.1</version> </SR_MagentoCommunity> </modules> <global> <models> <sr_magentocommunity> <class>SR_MagentoCommunity_Model</class> </sr_magentocommunity> </models> </global> <frontend> <events> <checkout_cart_add_product_complete> <observers> <sr_magentocommunity_checkout_cart_add_product_complete> <type>singleton</type> <class>sr_magentocommunity/observer</class> <method>checkoutCartAddProductComplete</method> </sr_magentocommunity_checkout_cart_add_product_complete> </observers> </checkout_cart_add_product_complete> </events> </frontend> </config>
app/code/local/SR/MagentoCommunity/Model/Observer.php
<?php class SR_MagentoCommunity_Model_Observer { public function checkoutCartAddProductComplete(Varien_Event_Observer $observer) { /** @var Mage_Catalog_Model_Product $product */ $product = $observer->getEvent()->getData('product'); $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product); if ($stock->getQty() <= 0) { $notAvailableQty = $this->getAddedProductQty($product); $message = Mage::helper('checkout')->__('%s is not available in the requested quantity. %s of the items will be backordered.', $product->getName(), $notAvailableQty); Mage::getSingleton('checkout/session')->addSuccess($message); } else { $qty = $this->getAddedProductQty($product); $notAvailableQty = $stock->getQty() - $qty; if ($notAvailableQty < 0) { $message = Mage::helper('checkout')->__('%s is not available in the requested quantity. %s of the items will be backordered.', $product->getName(), abs($notAvailableQty)); Mage::getSingleton('checkout/session')->addSuccess($message); } } } /** * @param Mage_Catalog_Model_Product $product * @return int */ public function getAddedProductQty($product) { $cart = Mage::getSingleton('checkout/cart'); $quote = $cart->getQuote(); $qty = 0; $items = $quote->getAllVisibleItems(); /** @var Mage_Sales_Model_Quote_Item $item */ foreach ($items as $item) { if ($item->getProductId() == $product->getId()) { $qty = $item->getQty(); } } return $qty; } }
You can easily handle this using event. Don't modify core file. Try following way:
app/etc/modules/SR_MagentoCommunity.xml
<?xml version="1.0"?> <config> <modules> <SR_MagentoCommunity> <active>true</active> <codePool>local</codePool> </SR_MagentoCommunity> </modules> </config>
app/code/local/SR/MagentoCommunity/etc/config.xml
<?xml version="1.0"?> <config> <modules> <SR_MagentoCommunity> <version>0.0.0.1</version> </SR_MagentoCommunity> </modules> <global> <models> <sr_magentocommunity> <class>SR_MagentoCommunity_Model</class> </sr_magentocommunity> </models> </global> <frontend> <events> <checkout_cart_add_product_complete> <observers> <sr_magentocommunity_checkout_cart_add_product_complete> <type>singleton</type> <class>sr_magentocommunity/observer</class> <method>checkoutCartAddProductComplete</method> </sr_magentocommunity_checkout_cart_add_product_complete> </observers> </checkout_cart_add_product_complete> </events> </frontend> </config>
app/code/local/SR/MagentoCommunity/Model/Observer.php
<?php class SR_MagentoCommunity_Model_Observer { public function checkoutCartAddProductComplete(Varien_Event_Observer $observer) { /** @var Mage_Catalog_Model_Product $product */ $product = $observer->getEvent()->getData('product'); $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product); if ($stock->getQty() <= 0) { $notAvailableQty = $this->getAddedProductQty($product); $message = Mage::helper('checkout')->__('%s is not available in the requested quantity. %s of the items will be backordered.', $product->getName(), $notAvailableQty); Mage::getSingleton('checkout/session')->addSuccess($message); } else { $qty = $this->getAddedProductQty($product); $notAvailableQty = $stock->getQty() - $qty; if ($notAvailableQty < 0) { $message = Mage::helper('checkout')->__('%s is not available in the requested quantity. %s of the items will be backordered.', $product->getName(), abs($notAvailableQty)); Mage::getSingleton('checkout/session')->addSuccess($message); } } } /** * @param Mage_Catalog_Model_Product $product * @return int */ public function getAddedProductQty($product) { $cart = Mage::getSingleton('checkout/cart'); $quote = $cart->getQuote(); $qty = 0; $items = $quote->getAllVisibleItems(); /** @var Mage_Sales_Model_Quote_Item $item */ foreach ($items as $item) { if ($item->getProductId() == $product->getId()) { $qty = $item->getQty(); } } return $qty; } }
Dear Sohel, it seems to work! The backorder message appears on first place and then the normal addtocart-message on second place. Maybe it is possible to combine them?
Regards,
Marcus
Dear Sohel,
thanks for your answer again. The message works for simple products but if a customer puts a product in his cart right from a configurable-product-page the message is also shown if the relevant simple product has enough stock. Is there a way to miodify this?
Regards,
Marcus