Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - How to add multiple items to the cart programmatically?

I'm trying to add multiple simple products to the cart at the same time using a query string as below, however it only adds the last product to the cart instead of both:

Can someone let me know what I'm doing wrong?

http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&product=15749&qty=4

I have also tried this:

http://www.domain.co.uk/checkout/cart/add?product[]=9916&qty[]=4&product[]=15749&qty[]=4

Any help much appreciated!

like image 943
Adam Moss Avatar asked Dec 19 '25 09:12

Adam Moss


2 Answers

Add Product To Cart With Querystring

  • Add simple product in shopping cart with no attribute.
    http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY
    Here PRODUCT_ID = 'Product Id',PRODUCT_QUANTITY = 'product quantity to purchase'.
  • Add product into shopping cart with single custome option.
    http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID]=OPTION_VALUE
    Here OPTION_ID = 'Custom attribute option id',OPTION_VALUE = 'Custom attribute option value'.
  • Add product into shopping cart with multipal custome option.
    http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID_1]=OPTION_VALUE_1&super_attribute[OPTION_ID_2]=OPTION_VALUE_2
    Here OPTION_ID_1 & OPTION_ID_1 = 'Custom attribute option ids',OPTION_VALUE_1 & OPTION_VALUE_2 = 'Custom attribute option values'.Here add more options in `super_attribute` array
  • Add Extra products with mail product with only 1 quantity.
    http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2
    Here PRODUCT_ID_1 and PRODUCT_ID_2 is other products id. add more product by id using `,` separator. Example:- &related_product=1,2,3,4.

Default magento there is not setting for add related product quantity into cart.so if you want to add this code than open app/code/core/Mage/Checkout/controllers/CartController.php find public function addAction().

if (!empty($related)) {
                $cart->addProductsByIds(explode(',', $related));
            }

Replace with

$rel_qty = $this->getRequest()->getParam('related_qty');
            if (!empty($related)) {
                $relatedproducts = explode(',', $related);
                $relatedqtys = explode(',',$rel_qty);
                $i = 0;
                foreach($relatedproducts as $relatedproduct)
                {
                    $cart->addProduct($relatedproduct, array('qty'=>$relatedqtys[$i]));
                $i++;
                }
            }

Now use query string for add related products with quantity.

http://yourserver.com/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2&related_qty=PRODUCT_ID_1_QUANTITY,PRODUCT_ID_2_QUANTITY
like image 56
himansu Avatar answered Dec 21 '25 06:12

himansu


If you don't want to change any code, you can try to utilize related products functionality by adding related_product parameter to your request. So your url will look like this:

http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&related_product=15749

If you want to add more products, just list them with comma separator: related_product=1,2,3 The only drawback from that is that you actually can't specify the qty for related products. To see how it works - Mage_Checkout_Model_Cart::addProductsByIds(array_of_ids)

If qty for subsequent products is a mandatory for you, you'll need to create your own controller, or override the Mage_Checkout_CartController::addAction method.

like image 33
Slayer Birden Avatar answered Dec 21 '25 05:12

Slayer Birden