Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Soap API Add Bundle Product to Cart

Tags:

php

soap

magento

I'm trying to create an order with Magento SOAP API v1 and having issue when adding bundled products to the cart. I'm able to get an order through correctly with simple products but I'm confused about adding bundled products.

// The Products Array with Bundle
$products = array(
        array(
            "product_id" => "38914",
            "qty" => "1",
            "bundle_option" => array(
                "18194" => "20360",
            ),
            "related_product" => null,
            "bundle_qty" => array("20360" => "1"),
            "options" => array(
                "0" => array(
                    "key" => "3328",
                    "value" => "4494",
                ),
                "1" => array(
                    "key" => "3329",
                    "value" => null,
                ),
                "2" => array(
                    "key" => "3339",
                    "value" => null,
                ),

            )
        )
    );

// Get an API session
$client = new \SoapClient('magentoinstallation/index.php/api/soap/?wsdl');
$session = $client->login('user', 'password');

//Create the Cart
$cart = $client->call( $session, 'cart.create');

// add the products
$resultCartProductsAdd = $client->call( $session, "cart_product.add", array(     $cart, $products ) );

I've tried many different formats and getting errors

Selected required options are not available

Please specify product option(s).

Any assistance or suggestions would be greatly appreciated.

like image 723
adamS Avatar asked Jun 12 '14 06:06

adamS


1 Answers

I figured out a way to add a bundle product to cart via SOAP.

The values within the key bundle_option must be the ids of the models for the options (bundle/selection) (not the product ids). The keys must be the id for the option (I assume that's already correct in your example)

$products = array(
    array(
        "product_id" => "38914",
        "qty" => "1",
        "bundle_option" => array(
            "18194" => "20360", // <-- THE VALUE MUST BE THE ID OF THE CORRESPONDING "bundle/selection" MODEL, INSTEAD OF THE PRODUCT'S ID
        ),
// ...
);

Also the key for the bundle quantities should be bundle_option_qty instead of bundle_qty.

Propably the availability of the bundled products will disturb your process, so ensure the products are all salable.


I tried it successfully with the sample data of magento and this snippet

$client = new \SoapClient('magentoinstallation/index.php/api/soap/?wsdl');
$session = $client->login('testuser', 'password');

$products = array(
    array(
        "product_id"    => 158,
        "qty"           => "1",
        "bundle_option" => array(
            1               => 2, // 1 is the option id, 2 is the bundle/selection id
        ),
    )
);

$cart = $client->call($session, 'cart.create', array('default'));
$resultCartProductsAdd = $client->call($session, "cart_product.add", array($cart, $products));

I retried my answer and found an important point to recognize.

SELECT A STORE ;-)

Just use

$cart = $client->call($session, 'cart.create', array('default'));

instead of

$cart = $client->call($session, 'cart.create');

(for more details please check the API specs: http://www.magentocommerce.com/api/soap/checkout/cart/cart.create.html)

After changing this, it was simple to add any bundle product, how described above

like image 97
bukart Avatar answered Nov 12 '22 14:11

bukart