Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Add products to cart programmatically
 
dmf1978
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2009-07-06
 

Hi!

I am trying to add a product to the cart of the current user (logged in or not) using the code below. But then, when I go to the shopping cart to see if the product was added, the page says that the cart is empty.
Any ideas?

Thanks!

<?php
    
require_once('app/Mage.php');    
    
umask(0);
    
Mage::app('admin');
    
    
$product_model Mage::getModel('catalog/product');
    
    
// Load product
    
$my_product_sku 'test';        
    
$my_product_id  $product_model->getIdBySku($my_product_sku);
    
$my_product     $product_model->load($my_product_id);
    
    
$qty_value 13;
    
    
// Add to cart (?)
    
$cart Mage::getModel('checkout/cart');
    
$cart->init();
    
$cart->addProduct($my_product, array('qty' => $qty_value));
    
$cart->save();
    
    
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
 
Magento Community Magento Community
Magento Community
Magento Community
 
danshields
Member
 
Total Posts:  32
Joined:  2009-03-11
 

hmm worked for me, but the price was 0.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dmf1978
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2009-07-06
 

Something I’ve found: If I put this code inside a function in a module controller (indexController.php) this works perfect.
May be there is something missing…

 
Magento Community Magento Community
Magento Community
Magento Community
 
danshields
Member
 
Total Posts:  32
Joined:  2009-03-11
 

I had your issued when I switched attribute sets and then I notice I didn’t have a lot of attributes such as visibility, status and some others in there. I added them and it works now

 
Magento Community Magento Community
Magento Community
Magento Community
 
danshields
Member
 
Total Posts:  32
Joined:  2009-03-11
 

crap now its not working for me, yeah there is something goofy going on

 
Magento Community Magento Community
Magento Community
Magento Community
 
danshields
Member
 
Total Posts:  32
Joined:  2009-03-11
 

I found the problem with your code since I was working on something similar. It is because you have the store set to admin.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sebastian_keutm
Jr. Member
 
Total Posts:  6
Joined:  2012-01-11
 

Had trouble with that.
My solution was to use:

Mage::getModel('sales/quote')->loadByCustomer($customer_id);
instead of
Mage::getSingleton('checkout/cart');

This works for me with magento 1.7.0.2

 
Magento Community Magento Community
Magento Community
Magento Community
 
TutorialMagento
Sr. Member
 
Avatar
Total Posts:  133
Joined:  2013-10-02
 

You may follow this tutorial, it worked properly
http://magentotutorial.net/programmatically-add-product-to-cart-with-price-change-magento/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Amasty
Mentor
 
Avatar
Total Posts:  3988
Joined:  2009-11-10
 

Hello,

you can also have a look at Auto Add Promo Items. It also adds promotional free items to a shopping cart. The module features include:

- Unlimited number of rules per shopping cart
- Works both with and without coupon code
- Clear indication of free items in the cart
- Classic ‘Buy 1, get 1 Free’ promotion

Image Attachments
autoadd+promobanners.pngautoadd+promobanners2.png
 
Magento Community Magento Community
Magento Community
Magento Community
 
aheadWorks
Guru
 
Avatar
Total Posts:  424
Joined:  2008-10-30
 

Hi,
I would advise you the Add Free Product To Cart Magento module that extends native Magento functionality with an ability of automatic product adding to the cart. An item appears in the shopping cart based on set criteria of the created rule.

You can give your client a product for free or suggest getting it with a specified discount. The information with the offer appears in a pop-up window or the product is added to the cart without any notice, so your clients will be in a daze after viewing the real ability of getting one more product absolutely for free.

Click here to learn more about the extension.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anatol NEKLO
Member
 
Avatar
Total Posts:  38
Joined:  2011-02-07
NEKLO LLC
 

I guess there is an error in your code here

require_once('app/Mage.php');    
    
umask(0);
    
Mage::app('admin');  # I guess this is an error. You should load Any Frontend Store Code,  default  for example

NEKLO LLC

+1-855-380-5958
1001 Bayhill Drive, 2nd Floor, San Bruno, 94066 California, United States of America

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top