5

Trying to add products to cart from pages which are totally outside Magento, on another CMS. The product information is available to these pages so technically I should be able to add to the cart via API.

I'm dealing with a developer who knows his CMS but not Magento, and the API information is not helpful.

All my products are structured as Grouped Products with associated simple products.

EDIT: I have managed do it programatically see at the very end - but that is not answering my question, just a test still no luck doing it over SOAP.

EDIT: I have other worries about SOAP->cart here SOAP API to build cart for frontend - should I be worried? session collision or cart loss?

It can be done in code / urls passing super_group[]

For example my group has ID 635 and one individual product in it is 122 I have been assuming that I need to pass to the cart:

product=635
super_group[122]=1

But how to do so via API? no hint of how. as if the documentation was added pre grouped products and never updated?

Found this which is full of questions, no answer http://www.opencart60s.com/magento/adding-grouped-product-via-magento-v2-api-21796.html

Looking at the API it seems the documentation covers adding: - simple products - simple products with options - bundles

but I am not seeing how to add a simple product which is hidden but only visible within a grouped product. I can add it direct as a simple product, and it gets added to the cart, but the connection to the group is lost, and there is also no link from the cart back to the product.

Clearly Magento needs both group and simple product information to be 100%

Within Magento itself, the way to do it is to pass the "parent" group sku and the component products as an array with quantity -eg super_group[399]=1

product=982&related_product=&super_group%5B399%5D=1

I don't see clear information how to pass super_group[399]=1 within the API

The API has the following possible fields

links An array of links (optional) options An array in the form of option_id => content (optional)

But there's no hint as to the format.

and then i see in several tickets (eg Unable to add configurable product to shopping cart using soap api) that maybe one needs to use "super_attribute" instead of "options" in some way

 'super_attribute' => array(
            0 => array(
            'key' => attribute-id,
            'value' => attribute-value
            ),
            1 => array(
            'key' => attribute-id,
            'value' => attribute-value
            )

TESTING

I have a very simple php file which tries to do the following:

$sess_id = $client->login($apiuser,$apikey);
$result = $client ->shoppingCartCreate($sess_id);
$arrProducts = array(); // trying different options, see below
$resultCartProductAdd = $client->shoppingCartProductAdd($sess_id, $result , $arrProducts);

NOT WORKING

    array(
        'product_id' => '982',
        'qty' => 1,
        'super_group' => array(
            0 => array(
            '399' => '1'
            )
        )

Error: Please specify the quantity of product(s).

NOT WORKING

        array(
            'product_id' => '982',
            'qty' => 1,
            'options' => array('399' => '1')
        )

Error: Please specify the quantity of product(s).

NOT WORKING

        array(
            'product_id' => '982',
            'qty' => 1,
            'super_group' => array(
                    '399' => '1'
            )
        )

Error: Please specify the quantity of product(s).

Programatically in a Module I managed to do it - but that brings me no closer to a structure in SOAP. Looking at alternatives now... sending the form_key to build form? creating a view that is just the "add to cart" bit for embedding? Is there an Ajax cart that works with external clients too?

How it works in a Module

public function addcartAction()
{
    // try a simple product: $productid = 122
    // try a grouped product: $productid = 635, $vintage = 122;
    // try another grouped product: $productid = 635, $vintage = 122;

    $productid = (int) $this->getRequest()->getParam('id'); 
    $vintage =  (int) $this->getRequest()->getParam('vintage'); 
    $qty = (int) $this->getRequest()->getParam('qty'); 

    $super_group = array();
    $params = array();
    $groupedTypeInstance = Mage::getModel('catalog/product_type_grouped');

    //Add  product to cart

    //Get object of main grouped product
    $productModel = Mage::getModel('catalog/product')->load($productid);

    if ($productModel->getTypeId() == 'simple') {

                 $params['product']=$productid;
                 $params['qty']=$qty;                    
             }

    if ($productModel->getTypeId() == 'grouped') {

            $params['product']=$productid;
            $params['super_group'] = array(
                    $vintage => $qty
                );
    }

    //Get cart object
    $cart = Mage::getModel('checkout/cart');
    $cart->init();

    $cart->addProduct($productModel->getId(), $params);
    $cart->save();
    echo $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($productModel->getName()));

}

(note: removed exception wrapping and tests to make the basics clearer)

  • could they really have made the API so that one product type cannot be added? – iphigenie Jul 5 '15 at 8:38
2

Having looked at it in some depth, I think this is simply not implemented in the Magento code and therefore the API would have to be extended for grouped products to work properly

Because I have found other concerns around soap - what I really needed is a front end solution - I am now writing a module that will "service" a javascript/ajax front end module (on a separate CMS)

Of course, even when writing native code, it seems not everything is fully implemented for Grouped products - eg: cart quantity changes seem iffy Update cart quantity for a grouped product

0

I am not fully testing with the API as I have my own front end controller to avoid the security of REST/SOAP. So I just forward the calls to to the SOAP methods directly. What I found is that this should have worked for you:

array(
 'product_id' => '982', //Group Parent ID
 'qty' => 1,            //irrelevant, can be omitted
 'super_group' => array(
    '399' => '1'        //Grouped Simple product ID => Qty
  )
)

If there are issues, you can debug this function:

 app/code/core/Mage/Checkout/Model/Cart/Product/Api.php:53
 public function add($quoteId, $productsData, $store = null)

enter image description here

$productData should be exactly as described above. Do remember that it's an array of array's. Your initial code should be:

$arrProducts = array( array('product_id'=> ...));
  • that's interesting actually. there are so many things that aren't finished around grouped products (and some still aren't in M2 either) but I wonder if maybe that was plugged at some point. Might try it if I can find my old test script again :) – iphigenie Sep 9 '17 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.