Error when using uc_cart_add_item with a product kit

Posts: 33
Joined: 01/30/2008

Hi,

I`m using uc_cart_add_item to add a product kit to the cart.

I get this warning for every product in the kit:

warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ubercart\modules\node\node.module on line 521.
warning: implode() [function.implode]: Bad arguments. in C:\xampp\htdocs\ubercart\modules\node\node.module on line 525.
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in C:\xampp\htdocs\ubercart\includes\database.mysql.inc on line 172.

The code I use:

$pid = 12; // id of the product kit
$data = array('module' => 'uc_product_kit');
uc_cart_add_item($pid , 1, $data, null, true, true);

The product kit with its products are being added to the cart
but I get the warning.

Is this a bug or wrong use of the function?

Thanks

Posts: 2008
Joined: 08/07/2007
AdministratoreLiTe!

The product kit module passes it's entire $form_values array to uc_cart_add_item() when the Add to Cart button is clicked. This array includes the product's nid and quantity which is passed to uc_product_add_to_cart_data() when each individual product is added to the cart. That would be the source of the error.

Use this:

<?php
$data
= array('products' => array(
 
$nid1 => array('nid' => $nid1, 'qty' => $qty1),
 
$nid2 => array('nid' => $nid2, 'qty' => $qty2),
 
//... etc.
));
?>

The 'module' is included as part of uc_product_kit_add_to_cart() so you don't need to specify it when you're calling uc_cart_add_item() on the product kit. Also, the "null, true, true" are the default values so you don't need to specify them.

Posts: 33
Joined: 01/30/2008

Thank you very much, it works.

I needed it because I'm creating a custom form with several select elements.
The form is used to present a product kit with several options in each section.

A computer with 3 different video cards, 2 screen types, and 5 HD types...
When a user chooses his package and submit the form I'm creating a product kit $node and save it to the database using node_save.

I set the discount property of the items in the product kit and call node_save again. This time it updates the discount.

Then I call uc_product_kit_add_to_cart().

It works well but I create unnecessary product kits in my database.

I wish I could create a temp product kit without writing it to the database. Is it possible?

Thanks