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:  113
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
Magento Community
Magento Community
    Back to top