How to add multiple products to cart

Hello all,

How to add multiple products(like 3 products) to the cart programmatically.

Can anyone please give me the solution

Thanks in advance.

Simply build an array with at least productids and amounts.
In forach of the array call the basket an add the products with addToBasket($id,$amount).

@rubbercut Thank you for your quick and valuable feedback.

Could you please share the sample code snippet.

$id = "productid1";
$amount = 1;
$intobasket[] = array($id,$amount);

$id = "productid2";
$amount = 2;
$intobasket[] = array($id,$amount);

foreach($intobasket as $values){
$oBasket = Registry::getSession()->getBasket();
$oBasket->addToBasket($values[0], $values[1]);
}

I would like to put several items directly into the shopping cart via a link or a form, can someone please tell me whether this is possible directly?

I found a post where id and amount are indexed into a form, but this causes an error.

<input type="hidden" name="id[0]" value="ed6573c0259d6a6fb641d106dcb2faec">
      <input type="hidden" name="am[0]" value="1">
      
      <input type="hidden" name="id[1]" value="058de8224773a1d5fd54d523f0c823e0">
      <input type="hidden" name="am[1]" value="2">

thanks

$intobasket = oxRegistry::getConfig()->getRequestParameter('id');
$am = oxRegistry::getConfig()->getRequestParameter('am');

foreach($intobasket as $key => $value){
$oBasket = Registry::getSession()->getBasket();
$oBasket->addToBasket($value, $am[$key]);
}

Thanks. Can you please tell me where to use the code?

that depends on where your form points to. Where do you want to start the lines? In an existing one or a new one?

The easiest way would be to create a new form that points to basket and starts a new method addmyprods();

Example:

<form name="basket" id="basket_formXYZ" action="http:/XXX.de/index.php?" method="post">
    <input type="hidden" name="cl" value="basket">
    <input type="hidden" name="fnc" value="addmyprods">
    <input type="hidden" name="id[0]" value="ed6573c0259d6a6fb641d106dcb2faec">
    <input type="hidden" name="am[0]" value="1">
    <input type="hidden" name="id[1]" value="058de8224773a1d5fd54d523f0c823e0">
    <input type="hidden" name="am[1]" value="2">
    <button type="submit" class="btn btn-primary submitButton"><i class="fa fa-gift"></i> Add Prods</button>
</form>
public function addmyprods(){

$intobasket = oxRegistry::getConfig()->getRequestParameter('id');
$am = oxRegistry::getConfig()->getRequestParameter('am');

foreach($intobasket as $key => $value){
$oBasket = Registry::getSession()->getBasket();
$oBasket->addToBasket($value, $am[$key]);
}
}

rubbercut, thanks for your help, i have a new form like yours, and i put the function in basket.php, or where i should put the code inside? but nothing happened, when i submit the form, only the shop site refresh.

Try to add the method to BasketController.php. Note: It’s only for testing. For work you should write an extension

Ok, that works. Do you perhaps know how to include items from the selection list in the function?

Thanks for your help.

What do you mean? $sel?
$oBasket->addToBasket($value, $am[$key], $sel);

yes i meant $sel, it worked again. thanks

how can i now say $sel from id 1 and $sel from id2, can i index the index? :slight_smile: like sel[0][2] and sel[1][2]?

my code is now like this

<input id="farbe" type="hidden" name="sel[0]" value="1">
    <input id="hoehe" type="hidden" name="sel[1]" value="2">
    <input type="hidden" name="cl" value="basket">
    <input type="hidden" name="fnc" value="addmyprods">
    <input type="hidden" name="id[0]" value="5502b07b540348d4c776b48a4d14815b"><br>
    <input id="stueckZaun" type="hidden" name="am[0]" value="1"><br>
    <input type="hidden" name="id[1]" value="6d1912c7f3e45e652176d0a267e2aeb0">
    <input id="stueckPfosten" type="hidden" name="am[1]" value="1">

both sel are for the first id, the second id need another select list, how can we fix this?

Thanks a lot

You have to extend the sel[] and the getter like: sel[art_oxid][1]

$sel = oxRegistry::getConfig()->getRequestParameter(‘sel’);

and add the array in the foreach in the same way.$sel[art_oxid]