Question

I'm developing a system that integrates an existing Django application with a Magento store. I've managed, using this library, to create bundle-type products, along with attributes and categories as expected. The problem is that I can't find a way, using the API (either through the aforementioned library or calling the xmlrpc methods directly), to add products to a previously created bundle. Calling the catalog_product.info method doesn't return anything useful about the bundled objects.

Researching online, I've only found solutions that involve either:

  • PHP scripts that use the Magento code base
  • Straight-to-database insertion

Neither of these is currently an option for me right now, and I don't think being able to do this by using just the API (either REST or SOAP) isn't an irrealistic expectation to have.

Was it helpful?

Solution

Background

It is not possible with the out-of-box API alone; documentation seems pretty clear on this as it documents no way of establishing selections, options for a bundle

Aside from that, the lack of an api.xml file in the Mage_Bundle package is telling that there is no support. Contrast that with another product type which arrived after 1.0, such as Downloadable, and it is apparent that the lack of support seems to be intentional.

enter image description here

So, why would they intentionally omit support here? The core team can probably chime in to that end. My guess is that the number of options available and complexity around designing an API like this just didn't pan out in the cost/benefit calculations.

So, wut do?

As Oleksii pointed out already you will have to extend the Magento API to allow this to happen.

A barebones standalone script which creates a bundle selection/option would look something like this:

<?php

require('app/Mage.php');
Mage::app();


$items[] = array(
    'title'     => 'test title',
    'option_id' => '',
    'delete'    => '',
    'type'      => 'radio',
    'required'  => 1,
    'position'  => 0
);

$selections = array();

$selectionRawData[] = array(
    'selection_id'             => '',
    'option_id'                => '',
    'product_id'               => '159',
    'delete'                   => '',
    'selection_price_value'    => '10',
    'selection_price_type'     => 0,
    'selection_qty'            => 1,
    'selection_can_change_qty' => 0,
    'position'                 => 0
);

$selections[] = $selectionRawData;

$product   = Mage::getModel('catalog/product')->setStoreId(0);
$product->load(182);

if (!$product) {
    //bail
    throw new Exception('Product loaded does not exist');
}

Mage::register('product', $product);
Mage::register('current_product', $product);

$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);

$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);

$product->save();

So at the most all we would have to do is provide an API interface to the required options. Create a new module with a model that extends the Magento API abstract model, and a method that takes the $items and $selectionRawData as parameters:

<?php
class YourCompany_YourModule_Model_Api extends Mage_Api_Model_Resource_Abstract
{

    public function createSelectionLink($items, $selectionRawData, $productId, $storeid)
    {

        $selections = array();

        //check if product id in selection data is valid
        $optionProduct = Mage::getModel('catalog/product')->load($selectionRawData['product_id']);

        if(!$optionProduct->getId()){
            throw new Exception('Selection product provided does not reference a valid product');
        }

        $selections[] = $selectionRawData;

        $product   = Mage::getModel('catalog/product')->setStoreId($storeid);
        $product->load($productId);

        if (!$product->getId()) {
            //bail
            throw new Exception('Product loaded does not exist');
        }

        Mage::register('product', $product);
        Mage::register('current_product', $product);

        $product->setCanSaveConfigurableAttributes(false);
        $product->setCanSaveCustomOptions(true);

        $product->setBundleOptionsData($items);
        $product->setBundleSelectionsData($selections);
        $product->setCanSaveCustomOptions(true);
        $product->setCanSaveBundleSelections(true);

        $product->save();
    }

}

Remember that $items and $selectionRawData are arrays (examples are listed above in the standalone script code block).

Now, create an api.xml file in your etc directory of your module with the following contents:

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <bundle_link translate="title" module="yourcompany_bundleapi">
                <title>Bundle creation extension</title>
                <model>bundleapi/api</model>
                <methods>
                    <createSelectionLink translate="title" module="yourcompany_bundleapi">
                        <title>Create a selection link</title>
                    </createSelectionLink>
                </methods>
            </bundle_link>
        </resources>
    </api>
</config>

That's it.

And to call the new API we use:

$proxy->call($sessionId, 'bundle_link.createSelectionLink', array($items, $selectionRawData, $productId, $storeid));

Caveats/prerequisites

  • You have to have already created a shell bundle product
  • You have to already have a simple product to link into the selection
  • You need to understand the format of the API array parameters or else the save call will choke, throwing exceptions
  • This only uses a radio button type - there are others: dropdown, checkbox for instance.
  • The $items array creates a fixed-price bundle item. This may be ignored or may choke for a dynamic price bundle.
  • The above code is an example only and shortened for brevity's sake; if you were building this you'd want to code it to be more fully featured/resilient so as not to drive your developers crazy when trying to create interfaces that make products.

References:

http://kavinduthundeniya.blogspot.de/2012/11/magento-extention-api-for-bundle.html

https://stackoverflow.com/questions/3108775/programmatically-add-bundle-products-in-magento-using-the-sku-id-of-simple-it

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top