How to retrieve associated products for configurable product with Magento SOAP API2?
Asked Answered
M

2

7

I'm trying to get all the associated products for a configurable product using the Magento SOAP API v2. The catalogProductLink call looks close, but doesn't handle configurable types. I don't see any other calls that contain the associated product and configurable type information for a product. How have others solved this issue?

I'm using Magento version 1.6 and the SOAP API V2 with Java.

Malonylurea answered 27/8, 2012 at 18:12 Comment(2)
As far as i know for Magento Community v1.6 it doesn't have the handle to retrieve what you wanted. You must create customized API v2 for this case.Scintillometer
Josua Marcel - please could you elaborate? If it is possible using a custom API what would the PHP code look like? I have tried a similar Java API client as above, with both 'related' and 'grouped' both return empty list though I can see the products in the admin client.Bea
Y
1

I looked deeper into this solution and realized that you may need to override the API model (Mage_Catalog_Model_Product_Api) to achieve the results you're looking for.

In the items function (around line 90), you can do something like this:

foreach ($collection as $product) {
    $childProductIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
    $result[] = array(
        'product_id' => $product->getId(),
        'sku'        => $product->getSku(),
        'name'       => $product->getName(),
        'set'        => $product->getAttributeSetId(),
        'type'       => $product->getTypeId(),
        'category_ids' => $product->getCategoryIds(),
        'website_ids'  => $product->getWebsiteIds(),
        'children'  => $childProductIds[0],
    );
}
Yakutsk answered 3/9, 2013 at 23:19 Comment(0)
L
1
  • create the folder app/code/local/Mage/Catalog/Model/Product/Link
  • copy the app/code/core/Mage/Catalog/Model/Product/Link/Api.php into this folder and edit it. (I’ve only done the change for the V1 but I’m pretty sure it’s as easy in V2)
  • replace the content of the the items() function with the following

        if($type == "associated"){
        $product = $this->_initProduct($productId);
        try
        {
            $result = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
        }
        catch (Exception $e)
        {
            $this->_fault('data_invalid', Mage::helper('catalog')->__('The product is not configurable.'));
        }
    
    }
    else
    {
        $typeId = $this->_getTypeId($type);
    
        $product = $this->_initProduct($productId, $identifierType);
    
        $link = $product->getLinkInstance()
            ->setLinkTypeId($typeId);
    
        $collection = $this->_initCollection($link, $product);
    
        $result = array();
    
        foreach ($collection as $linkedProduct) {
            $row = array(
                'product_id' => $linkedProduct->getId(),
                'type'       => $linkedProduct->getTypeId(),
                'set'        => $linkedProduct->getAttributeSetId(),
                'sku'        => $linkedProduct->getSku()
            );
    
            foreach ($link->getAttributes() as $attribute) {
                $row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
            }
    
            $result[] = $row;
        }
    }
    return $result;
    
  • then you can call the API like this now:

    $client->call($sessionId, 'product_link.list', array('associated', $id_of_your_configurable_product));

Basically my code is checking the type provided and if it’s “associated” it returns the child products. I’m pretty sure there’s better way of doing it but I thought that the Product Link API was the most relevant place to do it.

Enjoy!

(please note: this code is not mine, i just adapted it and thought it would be a nice idea to help you guys out)

Liar answered 9/7, 2014 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.