Jump to content

Adding multiple products to the cart with product options


goodspark

Recommended Posts

I am looking for a way to create a grid for the product options so that customers can order different quantities of different options with one add to cart. For example, a dress might have a grid that shows:

 

qty box   size Small

qty box   size Medium

qty box   size Large

 

I found the code in the categories template to add multiple products to the cart (<input type="text" name="add[{$product.product_id}][quantity]" value="1" class="quantity" />) and I found the code where it parses adding multiple items in cart.class.php

 

if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
// Multi-product adding from category page
if(is_numeric($value['quantity']) && $value['quantity'] > 1) {
$quantity = (int)$value['quantity'];
} else {
$quantity = 1;
}
$this->add((is_numeric($value)) ? $value : $key, null, $quantity);
}
}
 
How do I do an add to cart with multiple quantities linked up to multiple options on a single product ID?
Link to comment
Share on other sites

Welcome goodspark! Glad to see you made it to the forums.

 

That would be a very interesting enhancement.

 

A number of versions back, I've experimented with something similar: putting the options selectors on the View category page. Once the form fields were on the web page, I then had to populate them. That involved adding a call to the class method that gets all product info and related info -- which isn't otherwise necessary for the View Category listing. Then a minor edit to how the Cart->add() method processes the product options array.

 

That minor edit is by taking this statement:

$this->add((is_numeric($value)) ? $value : $key, null, $quantity);

and changing null to $_POST['productOptions']

 

I'll be happy to help in any way I can.

Link to comment
Share on other sites

Thanks for the quick response. The issue I am running in to is tying multiple quantity boxes to the inputs. I have this on the products page:

 

{foreach from=$option.values item=value}
<input type="text" size="2" {if $value.stock_level == 0 && $value.matrix == 1} disabled="disabled" {/if} name="add[{$PRODUCT.product_id}][quantity]" />
{$PRODUCT.product_code}
{$value.value_name}
<input type="hidden" name="add[{$PRODUCT.product_id}][opts]" value="{$value.assign_id}" />
<input type="hidden" name="productOptions[{$option.option_id}]" value="{$value.assign_id}" />
{/foreach}
 
and then tried this on the cart class
 
if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
// Multi-product adding from category page
if(is_numeric($value['quantity']) && $value['quantity'] > 1) {
$quantity = (int)$value['quantity'];
$opts = $value['opts'];
} else {
$quantity = 1;
}
 
if(isset($_POST['productOptions'])){
$this->add((is_numeric($value)) ? $value : $key, $opts, $quantity);
} else {
$this->add((is_numeric($value)) ? $value : $key, null, $quantity);
}
}
 
This just added the last option with a quantity of 1 to the cart.
Link to comment
Share on other sites

For the skin template file content.product.php, at the end, add {debug} and save. When you next call for a View Product page, a popup will show all the template variables available.

 

Look at $PRODUCT and $OPTIONS and from the array structure, one can better develop the necessary {foreach} loops.

 

The grid you propose would need to have every option assigned to it (in a column of rows as it is now?) with (a dynamically added column of rows?) the provision for more sets of options. That's an array within an array. So, the Cart->add() method will need to be rewritten to explode that out properly.

Link to comment
Share on other sites

I think the issue is that they key on the array using the admin uses the same key over and over since it is the product id, which is why the version I darted with wasn't working. So I updated to use the option value as the key so that it is unique. Still stuck, but this is where I am at right now. I think I am passing in the options array wrong, but can't figure out why.

 

On the products page:

<input type="text" size="2" {if $value.stock_level == 0 && $value.matrix == 1} disabled="disabled" {/if} name="add[{$value.assign_id}][quantity]" />

<input type="hidden" name="add[{$value.assign_id}][product]" value="{$PRODUCT.product_id}" />

<input type="hidden" name="add[{$value.assign_id}][opt]" value="{$option.option_id}" />

 

This correctly outputs:

<input type="text" size="2"  name="add[1959][quantity]" />

<input type="hidden" name="add[1959][product]" value="838" />

<input type="hidden" name="add[1959][opt]" value="1" />

 

On the cart class:

if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
// adding multiple options at once
$optvalue = $key;
$optid = $value['opt'];
$prodid = $value['product'];
$quan = $value['quantity'];
 
if(is_numeric($quan) && $quan > 1) {
$quantity = (int)$quan;
$this->add((int)$prodid, $options[$optid][$optvalue], $quantity);
 
On adding to cart, I am getting the "Please select required options before adding to your basket." message, which makes me think the option array isn't formatted correctly.  Any help would be hugely appreciated. I'm banging my head against the wall on this one.
Link to comment
Share on other sites

I feel like I am getting close.  I have it adding to the basket, but only the first item in the grid.

 

Products page:

{foreach from=$option.values item=value}

<input type="text" size="2" {if $value.stock_level == 0 && $value.matrix == 1} disabled="disabled" {/if} name="quan[{$value.assign_id}]" />

{$PRODUCT.product_code}{$value.value_name}<input type="hidden" name="productOptions[{$value.assign_id}]" value="{$value.assign_id}" /><input type="hidden" name="optid" value="{$option.option_id}" />

{/foreach}

 

Cart Class:

$opts = $_POST['productOptions'];
$quans = $_POST['quan'];
foreach($quans as $key => $value) {
if($quans[$key] > 0){
$masteropt[$_POST['optid']] = $opts[$key];
$this->add((int)$_POST['add'], $masteropt, (int)$quans[$key]);
}
 
Is there a way to see the POST variables that are going to the cart? I'm not sure why it isn't iterating past the first item.
Link to comment
Share on other sites

There is.

 

In admin, Store Settings, Advanced tab, enter your computer's (ISP supplied, www.whatismyip.com) IP address so that only you see this. Then enable debug mode. But this won't help you as I will explain shortly.

 

If you are using FireFox, get the FireBug addon. FireBug has a 'Net' panel that shows you all the traffic that comes in and goes out of the browser. It will show the POST array.

 

CubeCart will process all the data that it is given in POST. The debug mode collects all this and more for your inspection. CubeCart then sends a 301 redirect to your browser to re-request the page, CubeCart now having current data. CubeCart then shuts down. PHP has no more script to run, so it exits. All that debug data that was collect is trashed. When your browser makes the re-request, PHP wakes up, CubeCart starts, and debug data is getting collected. So, what you see in the debug section is of value only from the last page get.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...