Extend product_media.list Api to retrieve images always from configurable product

March 18, 2014  |  No Comments  |  by Raj (MagePsycho)  |  Latest, Magento, Web Service

Don’t you think updating same contents like media images and other attributes in all simple products of a configurable products is an overhead? For an easier management, it’s better to update frontend visible data only in configurable product. But this may led missing of data if someone makes an API call for simple product, for example: product_media.list

But we have a solution i.e. to retrieve the data (ex. images) always from the configurable product.
1. Define XML Rewrite

<global>
    <models>        
        <catalog>
            <rewrite>
                <product_attribute_media_api>MagePsycho_Apiextender_Model_Catalog_Product_Attribute_Media_Api</product_attribute_media_api>
            </rewrite>
        </catalog>
    </models>
</global>    

2. Extend Mage_Catalog_Model_Product_Attribute_Media_Api

<?php

/**
 * @category   MagePsycho
 * @package    MagePsycho_Apiextender
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Apiextender_Model_Catalog_Product_Attribute_Media_Api extends Mage_Catalog_Model_Product_Attribute_Media_Api
{

    /**
     * Retrieve product
     *
     * @param int|string $productId
     * @param string|int $store
     * @param  string $identifierType
     * @return Mage_Catalog_Model_Product
     */
    protected function _initProduct($productId, $store = null, $identifierType = null)
    {
        $product = Mage::helper('catalog/product')->getProduct($productId, $this->_getStoreId($store), $identifierType);

        //tweak $product if product is simple and part of configurable product
        if ($product && $product->getId()) {
            if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
                $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
                if (isset($parentIds[0])) {
                    $product = Mage::helper('catalog/product')->getProduct($parentIds[0], $this->_getStoreId($store), $identifierType);
                }
            }
        }

        if (!$product->getId()) {
            $this->_fault('product_not_exists');
        }

        return $product;
    }
}

3. And you can test the API (using XML-RPC) as:

<?php
$mageFilename = '/path/to/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();

try{
    $client = new Zend_XmlRpc_Client('http://your-store.com/api/xmlrpc/');
    $session = $client->call('login', array('apiUser', 'apiPass'));

    $images = $client->call('call', array($session, 'product_media.list', array('some-sku')));
    Zend_Debug::dump($images);

    $client->call('endSession', array($session));
} catch (Exception $e) {
    echo 'API Error:: ' . $e->getMessage();
}

Hello world!

September 17, 2011  |  No Comments  |  by Raj (MagePsycho)  |  Javascript, jQuery, Latest, Linux, Magento, Mysql, PHP, Web Service

Dear All
I am trying to share my knowledge gained so far in web development fields from now onwards via this blog.
I will be sharing stuffs related to PHP/Mysql, Magento & more.
Well nothing more to say, just stay tuned with my blog 🙂

Lastly, your suggestion/feedback is always valuable and will make my blog alive.

This much for now.
Thanks