Saturday, November 3, 2012

Magento Extention API for Bundle Products

Recently when i working Magento data migration team, some sort of reason we use magento SOAP API for migrate products details. one of the major issue that we face was migrate Bundle product via soap API. At the moment magento core API doesn't support create bundle product via API.

 There for i decided to create Custom extension for Create Bundle product via soap API. After spend few hours on web found the possible way to implement it. hope this will help any one who want create Bundle product in Magento via SOAP API.

Extension Files

/www/magento/app/etc/Company_Bundleapi.xml
/www/magento/app/code/local/Company/Bundleapi/Model/Objectmodel/Api.php
/www/magento/app/code/local/Company/Bundleapi/etc/api.xml
/www/magento/app/code/local/Company/Bundleapi/etc/config.xml

Source Code

/www/magento/app/etc/Company_Bundleapi.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Bundleapi>
            <active>true</active>
            <codePool>local</codePool>
        </Company_Bundleapi>
    </modules>
</config>

 

/www/magento/app/code/local/Company/Bundleapi/etc/api.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <bundle_link translate="title" module="company_bundleapi">
            <title>Title Of Bundle link</title>
            <model>bundleapi/objectmodel_api</model>
            <methods>
                <createlink translate="title" module="company_bundleapi">
                    <title>Title Of Create link method</title>
                </createlink>
            </methods>
            </bundle_link>
        </resources>
    </api>
</config>




 /www/magento/app/code/local/Company/Bundleapi/etc/config.xml 

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Company_Bundleapi>
            <version>0.1.0</version>
        </Company_Bundleapi>
    </modules>
    <global>
        <models>
            <bundleapi>
                <class>Company_Bundleapi_Model</class>
            </bundleapi>
        </models>
    </global>
</config>

 

/www/magento/app/code/local/Company/Bundleapi/Model/Objectmodel/Api.php

<?php
class Company_Bundleapi_Model_ObjectModel_Api extends Mage_Api_Model_Resource_Abstract
{

 public function createlink($pitems,$pselectionRawData, $pproductId, $storeid)
    {
        $selections = array();
          
            $selections = $pselectionRawData;

            $productId = $pproductId;
            $product    = Mage::getModel('catalog/product')->setStoreId($storeid);
           
            if ($productId) {
                $product->load($productId);
            }
            Mage::register('product', $product);
            Mage::register('current_product', $product);
            $product->setCanSaveConfigurableAttributes(false);
            $product->setCanSaveCustomOptions(true);

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

            $product->save();
            return $product->getName();
    }

}
?>





Usage

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

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

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