How to extend Magento API catalog_product.list to include more product information XML-RPC

淺唱寂寞╮ 提交于 2019-12-03 16:42:15

Go to \app\code\core\Mage\Catalog\Model\Product\Api.php, find items method and look at next piece of code (line 80 in my CE 1.6)

        $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids'       => $product->getCategoryIds()
        );

Add needed attributes here or even write $result[] = $product->getData(); to fetch all standard attributes. If you need some custom attribute there, look at the code

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

above (line 58 in my CE 1.6) and add line ->addAttributeToSelect('<your_attribute_code>').

It is a bad practice to modify code in app/code/core. You will have to extend it.

I had the same issue and here is what I did to get it working with API V2 in Magento CE 1.9.0.1 :

  • Create a new module in your app/code/local. Don't forget to add the module in your app/etc/modules directory.

  • In your config.xml, add the following rewrite rules (replace with your class names) :

    <global> <models> <catalog> <rewrite> <product_api_v2>NS_Catalog_Model_Product_Api_V2</product_api_v2> </rewrite> </catalog> </models> </global>

Then, create the class :

class NS_Catalog_Model_Product_Api_V2 extends Mage_Catalog_Model_Product_Api_V2
{
    /**
     * Retrieve list of products with basic info (id, sku, type, set, name)
     *
     * @param null|object|array $filters
     * @param string|int $store
     * @return array
     */
    public function items($filters = null, $store = null)
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addStoreFilter($this->_getStoreId($store))
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('custom_attribute_1')
            ->addAttributeToSelect('custom_attribute_2') //and so on...
        ;

        /** @var $apiHelper Mage_Api_Helper_Data */
        $apiHelper = Mage::helper('api');
        $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
        try {
            foreach ($filters as $field => $value) {
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        $result = array();
        foreach ($collection as $product) {
            /** @var Mage_Catalog_Model_Product $product */

            $result[] = array(
                'product_id'   => $product->getId(),
                'price'        => $product->getPrice(),
                'attr_1'       =>  $product->getData('custom_attribute_1'),
                'sku'          => $product->getSku(),
                'name'         => $product->getName(),
                'set'          => $product->getAttributeSetId(),
                'type'         => $product->getTypeId(),
                'category_ids' => $product->getCategoryIds(),
                'website_ids'  => $product->getWebsiteIds()
            );
        }
        return $result;
    }
}

But it is not enough... you will have to overload the wsdl.xml and wsi.xml.

  • Copy the files from app/code/core/Mage/Catalog/etc/(wsdl|wsi).xml in your module's etc directory.

  • Locate the entity : complexType name="catalogProductEntity"

  • Add your custom attributes to the list

  • clear the caches (if you use a PHP client, keep in mind that PHP stores a copy of the WSDL, by default in /tmp)

@Zyava has pointed out the file to modify, which is very useful. But I used another way to change this file:

$result[] = array( // Basic product data
    'product_id' => $product->getId(),
    'sku'        => $product->getSku(),
    'name'       => $product->getName(),
    'set'        => $product->getAttributeSetId(),
    'type'       => $product->getTypeId(),
    'category_ids'       => $product->getCategoryIds(),
    '<your_attribute_code>' => $product->getData('<your_attribute_code>')
);

Just add a line for an attribute you want to retrieve. This worked on my CE 1.6.1. But there is a little problem with it: if the the attribute code is changed in Magento admin panel, the API code will break. So take extra care not to change the attribute code which is added here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!