Jump to content

Webservice - Add Cart - Multiple Cart_rows


sombra84

Recommended Posts

Hi Everyone - I am working with prestashop 1.6 and have a fully working setup with the webservice(api) and prest5ashop but I have a slightly issue with the cart adding mechanism.

Basically I go to create my cart but when I do each subsequent item I add (cart_row) under (cart_rows) seems to be malformed and thus does not get added.

My code is:

   

 $product_list = array(
    "1" => array("id_product" => "219", "quantity" => "1"),
    "2" => array("id_product" => "219", "quantity" => "1"),
    "3" => array("id_product" => "219", "quantity" => "3")   
    );
                $i = 0;
                foreach ($product_list as $product) {
                    $xml->cart->associations->cart_rows->cart_row[$i]->id_product = $product['id_product'];
                    $xml->cart->associations->cart_rows->cart_row[$i]->quantity = $product['quantity'];
                    $i++;
                }
    
    $opt = array('resource' => 'carts');
    $opt['postXml'] = $xml->asXML();
    echo '<pre>'; print_r($opt); echo '</pre>';
    $xml = $webService->add($opt);
    $id['cart'] = $xml->cart->id; // ID of created cart

So basically in the above example, product 1 gets added but 2 and 3 do not.  When I look at the XML response from Prestashop I notice this:

 

   <associations>
    <cart_rows>
            <cart_row>
            <id_product>219</id_product>
            <id_product_attribute/>
            <id_address_delivery/>
            <quantity>1</quantity>
            </cart_row>
    <cart_row><id_product>219</id_product><quantity>1</quantity></cart_row><cart_row><id_product>219</id_product><quantity>3</quantity></cart_row></cart_rows>
    </associations>

I can see the second and third products added seem malformed, but I do not understand why.  Can anybody help out?

Thanks!
 

Link to comment
Share on other sites

  • 2 months later...

You must have:

 $product_list = array(
    "1" => array("id_product0" => "219", "quantity0" => "1"),
    "2" => array("id_product1" => "219", "quantity1" => "1"),
    "3" => array("id_product2" => "219", "quantity2" => "3")   
    );
Edited by Alex (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...