首页 文章

如何使用Magento SOAP API2检索可配置产品的关联产品?

提问于
浏览
7

我正在尝试使用Magento SOAP API v2获取可配置产品的所有相关产品 . catalogProductLink调用看起来很接近,但不处理可配置类型 . 我没有看到任何其他调用包含产品的关联产品和可配置类型信息 . 其他人如何解决这个问题?

我正在使用Magento 1.6版和带有Java的SOAP API V2 .

2 回答

  • 1

    我深入研究了这个解决方案,并意识到您可能需要覆盖API模型(Mage_Catalog_Model_Product_Api)来实现您正在寻找的结果 .

    在items函数(第90行)中,您可以执行以下操作:

    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],
        );
    }
    
  • 1
    • 创建文件夹app / code / local / Mage / Catalog / Model / Product / Link

    • 将app / code / core / Mage / Catalog / Model / Product / Link / Api.php复制到此文件夹并进行编辑 . (我只对V1进行了更改,但我很确定它在V2中很容易)

    • 用以下内容替换items()函数的内容

    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;
    
    • 然后你可以这样调用这样的API:

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

    基本上我的代码是检查提供的类型,如果它“关联”它返回子产品 . 我很确定有更好的方法,但我认为Product Link API是最相关的地方 .

    请享用!

    (请注意:这段代码不是我的,我只是改编了它,并认为帮助你们出去是一个好主意)

相关问题