How can i get last 3 added products of cart in magento.
I am using $session->getQuote()->getAllVisibleItems()
for that.
Please Provide me if any other feasible solution
cartmagento-1.9mini-cart
How can i get last 3 added products of cart in magento.
I am using $session->getQuote()->getAllVisibleItems()
for that.
Please Provide me if any other feasible solution
The call to
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
... is actually clearing the session variable after it is read. Magento uses magic methods extensively. In this case you are using the __call()
magic method which in turn uses the getData()
method. In Mage_Core_Model_Session_Abstract_Varien
you will see that they override the default behaviour of getData()
to expect the second parameter to be a boolean (The first parameter to getData()
is the key name for the value you are looking for). That boolean is a flag telling the session to clear the variable after reading.
You could always listen for the checkout_cart_product_add_after
event and add the item to your own variable in the session. That event is actually fired on the line before setLastAddedProductId()
is called.
Refered links:
Edit 1:
You're almost there - try this code:
$collection = Mage::getSingelton('checkout/session')->getQuote()->getItemsCollection();
$collection->getSelect()->order('created_at DESC');
$latestItem = $collection->getFirstItem();
Note that when you get the latest quote item, you're not actually obtaining the product. To get the actual product, you would need to add this line:
$product = $latestItem->getProduct();
Edit 2:
Use this below code
Code to get the the full product information (where 3 is the configurable product Id)
$product = Mage::getModel('catalog/product')->load(3);
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach($childProducts as $child) {
print_r($child->getName()); // You can use any of the magic get functions on this object to get the value
}
Edit 3:
You can also get by sku
$associated_products = $_product->loadByAttribute('sku', $_product->getSku())->getTypeInstance()->getUsedProducts();
foreach ($associated_products as $assoc)
{
$assocProduct = Mage::getModel('catalog/product')->load($assoc->getId());
}
You can use checkout_cart_product_add_after
event to allow or disallow products being added ...
app/code/local/My/Module/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<models>
<my_module>
<class>My_Module_Model</class>
</my_module>
</models>
<events>
<checkout_cart_product_add_after>
<observers>
<test>
<class>my_module/observer</class>
<method>disallowProductAddToCart</method>
</test>
</observers>
</checkout_cart_product_add_after>
</events>
<!-- make custom attrribute available in quote item product -->
<sales>
<quote>
<item>
<product_attributes>
<preorder />
</product_attributes>
</item>
</quote>
</sales>
</global>
</config>
app/code/local/My/Module/Model/Observer.php
class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
public function disallowProductAddToCart(Varien_Event_Observer $observer)
{
$quote = $observer->getQuoteItem();
$cartItems = $quote->getQuote()->getAllItems();
if (count($cartItems) > 1) {
$product = $observer->getProduct();
$isPreorder = $product->getPreorder();
if ($isPreorder) {
$quote->getQuote()->removeItem($quote->getId());
Mage::throwException(
Mage::helper('sales')->__('Can not add pre-order product to existing cart.')
);
}
foreach ($cartItems as $item) {
if (!$isPreorder && $item->getProduct()->getPreorder()) {
$quote->getQuote()->removeItem($quote->getId());
Mage::throwException(
Mage::helper('sales')->__('Cart already contains an pre-ordered product.')
);
break;
}
}
}
}
}
Edit: allow multiple "pre-order" products in cart
public function disallowProductAddToCart(Varien_Event_Observer $observer)
{
$quote = $observer->getQuoteItem();
$cartItems = $quote->getQuote()->getAllItems();
if (count($cartItems) > 1) {
$product = $observer->getProduct();
$isPreorder = $product->getPreorder();
foreach ($cartItems as $item) {
if ($isPreorder != $item->getProduct()->getPreorder()) {
$quote->getQuote()->removeItem($quote->getId());
$message = $isPreorder ? 'Can not add pre-order product to existing cart.' : 'Cart already contains pre-ordered products.';
Mage::throwException(
Mage::helper('sales')->__($message)
);
break;
}
}
}
}
If your are curious why using checkout_cart_product_add_after
and remove item from cart later $quote->getQuote()->removeItem($quote->getId())
... it is just because there is no checkout_cart_product_add_before
event dispatched in Mage_Checkout_Model_Cart::addProduct()
:(
Best Answer
You can find answer by looking at
getRecentItems
method insideMage_Checkout_Block_Cart_Sidebar
class.This applies to current customer session.