Skip to content

Instantly share code, notes, and snippets.

@bmxrosn
Created February 17, 2016 13:50
Show Gist options
  • Save bmxrosn/d017830c2dc8254bb747 to your computer and use it in GitHub Desktop.
Save bmxrosn/d017830c2dc8254bb747 to your computer and use it in GitHub Desktop.

This module extends Magento API and adds some extra functionality for bundle and configurable products (SOAP v1, v2 and REST).

Bundle products

Resource: bundle

Methods:

  - bundle.createLink (SOAP V1)
  - bundleCreateLink  (SOAP V2)
  - /bundles/itemsbatch/:product_id/store/:store (REST)

Allows to add new options to the bundle product.

Arguments:

Type Name Description
string sessionId Session ID
int productId ID of existing bundle product
array optionsData Array of bundleProductItem
int storeId Store ID

Returns:

Type Name Description
int productId Bundle productID

bundleProductItem structure:

Type Name Description
string title Option title
string type Option type (radio, checkbox etc.)
int required Option is required
int position Option position
array selection Array of bundleProductItemSelection

bundleProductItemSelection structure:

Type Name Description
int product_id ID of the linked product
int selection_qty Default product quantity
int is_default Product is selected by default
int selection_can_change_qty User can change quantity
int position Selection product position

Examples

Request Example SOAP V1

$client = new SoapClient('http://<your_domain>/api/soap/?wsdl');
$session = $client->login('your_api_user', 'your_api_key');

$items = array();
$items[] = array(
    'title' => 'First item Radio',
    'type' => 'radio',
    'required' => 1,
    'position' => 0,
    'selection' => array(
        array(
            'product_id' => 896,
            'selection_qty' => 1,
            'is_default' => 1,
            'selection_can_change_qty' => 0,
            'position' => 10
        ),
        array(
            'product_id' => 897,
            'selection_qty' => 0,
            'is_default' => 0,
            'selection_can_change_qty' => 1,
            'position' => 20
        )
    )
);
$items[] = array(
    'title' => 'SECOND',
    'type' => 'checkbox',
    'required' => 0,
    'position' => 0,
    'selection' => array(
        array(
            'product_id' => 898,
            'selection_qty' => 1,
            'is_default' => 1,
            'selection_can_change_qty' => 0,
            'position' => 30
        )
    )
);

$result = $client->call($session, 'bundle.createLink', array(912, $items, 0));
var_dump($result);

Request Example SOAP V2

// incoming data is the same as for the SOAP V1 call
$result = $client->bundleCreateLink($session, 912, $items, 0);

Request Example REST

// incoming data is the same as for the SOAP V1 call
$resourceUrl = "$apiUrl/bundles/itemsbatch/916/store/0";
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, json_encode($items), OAUTH_HTTP_METHOD_POST, $headers);

Methods:

  - bundle.getOptions (SOAP V1)
  - bundleGetOptions  (SOAP V2)
  - /bundles/:product_id/options (REST)

Gets bundle product options.

Arguments:

Type Name Description
string sessionId Session ID
int productId Bundle product ID

Returns:

Type Name Description
array itemsData Array of bundleProductItem

Request Example SOAP V1

$result = $client->call($session, 'bundle.getOptions', array(912));

Request Example SOAP V2

$result = $client->bundleGetOptions($session, 912);

Request Example REST

$resourceUrl = "$apiUrl/bundles/912/options";
$oauthClient->fetch($resourceUrl);
echo '<pre>'; print_r( json_decode($oauthClient->getLastResponse()) );

Methods:

  - bundle.removeOption (SOAP V1)
  - bundleRemoveOption  (SOAP V2)
  - /bundles/options/:option_id (REST)

Removes bundle option by option ID.

Arguments:

Type Name Description
string sessionId Session ID
int optionId Option ID

Returns:

Type Name Description
bool result Result

Request Example SOAP V1

$result = $client->call($session, 'bundle.removeOption', array(52));

Request Example SOAP V2

$result = $client->bundleRemoveOption($session, 52);

Request Example REST

$resourceUrl = "$apiUrl/bundles/options/52";
$oauthClient->fetch($resourceUrl, array(), OAUTH_HTTP_METHOD_DELETE, array());

Methods:

  - bundle.getSelections (SOAP V1)
  - bundleGetSelections  (SOAP V2)
  - /bundles/:product_id/selections (REST)

Get bundle product selections (products, associated to the bundle options).

Arguments:

Type Name Description
string sessionId Session ID
int productId Bundle product ID

Returns:

Type Name Description
array itemsData Array of bundleProductItem

Request Example SOAP V1

$result = $client->call($session, 'bundle.getSelections', array(912));

Request Example SOAP V2

$result = $client->bundleGetSelections($session, 912);

Request Example REST

$resourceUrl = "$apiUrl/bundles/912/selections";
$oauthClient->fetch($resourceUrl);

Methods:

  - bundle.removeSelection (SOAP V1)
  - bundleRemoveSelection  (SOAP V2)
  - /bundles/selections/:id (REST)

Get bundle product selection by selection ID.

Arguments:

Type Name Description
string sessionId Session ID
int selectionId Selection ID

Returns:

Type Name Description
bool result Result

Request Example SOAP V1

$result = $client->call($session, 'bundle.removeSelection', array(143));

Request Example SOAP V2

$result = $client->bundleRemoveSelection($session, 143);

Request Example REST

$resourceUrl = "$apiUrl/bundles/selections/143";
$oauthClient->fetch($resourceUrl, array(), OAUTH_HTTP_METHOD_DELETE, array());

Configurable products

You can create a configurable product using standard Magento SOAP v1 method catalog_product.create method. The only one difference - it is needed to pass configurable attributes information:

$productData = array(
    'websites' => array(1),
    'name' => 'Product Name',
    'description' => 'Product description',
    'short_description' => 'Product short description',
    'status' => '1',
    'url_key' => 'product-url-key1' . rand(),
    'url_path' => 'product-url-path1' . rand(),
    'visibility' => '4',
    'price' => '200',
    'tax_class_id' => 0,
    'configurable_attributes_data' => array(
        array(
            'label'          => 'Color Custom Frontend Label',
            'attribute_code' => 'color'
        )
    )
);

$result = $client->call($session, 'catalog_product.create', array('configurable', $attributeSetId, 'configurable-sku', $productData));

Resource: configurable

Methods:

  - configurable.assign (SOAP V1)
  - configurableAssign  (SOAP V2)
  - /configurables/:product_id/items (REST)

Allows to add new associated products to configurable product.

Arguments:

Type Name Description
string sessionId Session ID
int configurableId ID of existing configurable product
array simpleIds Array of simple product IDs

Returns:

Type Name Description
bool result Result

Examples

Request Example SOAP V1

$result = $client->call($session, 'configurable.assign', array(919, array(920, 921)));

Request Example SOAP V2

$result = $client->configurableAssign($session, 919, array('920', '921'));

Request Example REST

$data = json_encode(array(
    'simple_id' => array(920, 921)
));
$resourceUrl = "$apiUrl/configurables/919/items";
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $data, OAUTH_HTTP_METHOD_POST, $headers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment