Let me know how can I get status of shopping cart in magento from anywhere ?
I mean that Is It possible to get an error like
- your item "marlboro" count has been exceeded ! At present .
There is no so much in stock, please decrease it
Thanks
You can use something like that on an event that is fired on every page like controller_front_init_before
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$product = $item->getProduct();
$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($product)->getQty();
if ($stocklevel<$item->getQty()){
...the logic that will permit to show the customer that the requested
product is not avaiable in this quantity...
}
}
Related
I am programming an extension which adds relevant products to the cart once you add an item to the cart. Example if you are buying a pen I am going to add paper to the cart. Bundled packages are not an option since I need to match certain conditions. I tried the following:
I set up an event listener "sales_quote_item_collection_products_after_load" scan all the products in cart and add the relevant products. Sadly you have to reload the cart in order to make the products appear.
I used this code in, my event listener, to add products to cart:
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// Add a product (simple); id:12, qty: 3
$cart->addProduct(12, 3);
$cart->save()
The strange thing is that removing products using the cart helper works (without refreshing):
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $productId) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
Is there a way to tell Magento to "requote" or what would you recommend? I also thought of adding the product, at the add to cart listener. But in that case I will need to implement it as well on update and remove, so it will work correctly. Using sales_quote_item_collection_products_after_load after load seemed to be the best option, since I have everything in one place.
You need to change in your code
$product_model = Mage::getModel('catalog/product');
$product_id =35;
$my_product = $product_model->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => 1));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
<?php $rulesCollection = Mage::getModel('salesrule/rule')->getCollection();
$i=1;
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$totalPrice = 0;
foreach($items as $item) {
$totalPrice+=$item->getPrice();
}
$coll = Mage::getResourceModel('salesrule/rule_collection')->load();
echo '<div class=vip_test>';
foreach($coll as $rule){
$productDetail = $rule->afterLoad();
$discountAmount = $productDetail['discount_amount'];
echo "<div class=ssk><span> You Save : </span>".$totalAmount = $totalPrice-($discountAmount/100*$totalPrice)."</div>";
$ruleID = $productDetail['rule_id'];
}
foreach($rulesCollection as $rule){
$coupon = $rule->getCode();
$couponName = $rule->getName()
?>
In Magento, with this I am getting only the coupon code and coupon name. I want to display if there is any coupon code for particular product in shopping cart, in front of coupon - its showing the saving amount. Is it possible or not? Am I doing Wrong? please help me
First you need to get the collection of products which are in your cart. Then you need to check if there are any active coupons in catalog price rules or shopping cart price rules for the products which are in your cart. If all the above conditions becomes true then you need to display the coupon code of it.
On the next side, you need to calculate the difference amount of it and also you need to display if the user increases his quantity then the savings amount value also has to be increased.
I need to delete one item from cart.
I use the checkout_cart_add_product_complete event wich is fired after the cart was saved.
Then I use:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$allQuoteItems = $quote->getAllItems();
foreach ($allQuoteItems as $_item) {
$_product = $_item->getProduct();
if ($_product->getIsPreparedToDelete()) {
$quote->removeItem($_item->getId());
}
}
$quote->save();
But the item is still there...
I have no idea which event I can use - or if it is really possible to delte an item in the cart (I also added items, that works - with the same event..).
Thanks.
Take a look # Magento - remove one quantity from cart
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
...
Mage::getSingleton('checkout/cart')->removeItem($item->getId());
Found it out.
removeItem() works outside this event, when all is saved. Within works this ($_item->isDeleted(true)):
$quote = Mage::getSingleton('checkout/session')->getQuote();
$allQuoteItems = $quote->getAllItems();
foreach ($allQuoteItems as $_item) {
$_product = $_item->getProduct();
f ($_product->getIsPreparedToDelete()) {
$_item->isDeleted(true);
} else {
// ....
}
}
$quote->save();
I think you can use one of the below event to observe and can use to remove particular item from cart:
sales_quote_item_save_before: When the item will be added to cart, this event will be called before the save event of that item fired.
sales_quote_save_after: And every time the whole cart will be saved this event will be fired after that.
And for your information, sales quote object is being used for handling cart.
And below is the link to the list of magento's events:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/events
Please feel free to revert back with any queries.
Regards,
Milan
I want that when customer register product (which is choosen in backend) should get added to cart. I have done this :
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load(154);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, 1)->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
in AccountController.php in createPostAction().
But it is not showing but whenever customer buys anything it gets added into cart. I am doing anything wrong...?
-Thnx in advance.
The problem is, the quote object in the session has already collected totals and would not do it again when the cart is saved. Because of this the quote's item count is zero, even though a quote item has been successfully added and saved. Modify your code like this:
$cart->product($product, 1);
$cart->getQuote()->setData('totals_collected_flag', false);
$cart->save();
And this should solve the problem.
A suggestion: would it not be better to implement this functionality in an observer listening to the customer_register_success event?
I'm using magento 1.6.1.0. I included new sidebar in my cart page where it'll show product title, total amount & proceed to checkout button as one by one. I included total amount by calling <?php echo $this->getChildHtml('totals'); ?> and proceed to checkout button by calling <?php echo $this->getChildHtml('methods') ?>
But i don't know how to call Product name.
Anyone know how to call my cart products name which are currently added in basket? Please share your idea to do this!
Thanks
You can get access to your cart object via Mage::getSingleton('checkout/cart').
Calling Mage::getSingleton('checkout/cart')->getItems() will get you your current cart item collection that you can iterate through to get product names as well as other details.
$items = Mage::getSingleton('checkout/cart')->getItems();
foreach ($items as $item) {
echo $item->getName();
}