【问题标题】:How to retrieve associated products for configurable product with Magento SOAP API2?如何使用 Magento SOAP API2 检索可配置产品的关联产品?
【发布时间】:2012-08-27 18:12:42
【问题描述】:

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

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

【问题讨论】:

  • 据我所知 Magento Community v1.6 它没有检索你想要的东西的句柄。您必须为此案例创建自定义 API v2。
  • Josua Marcel - 请您详细说明一下?如果可以使用自定义 API,PHP 代码会是什么样子?我已经尝试过与上面类似的 Java API 客户端,“相关”和“分组”都返回空列表,尽管我可以在管理客户端中看到产品。

标签: java magento soap


【解决方案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],
    );
}

【讨论】:

    【解决方案2】:
    • 创建文件夹 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 是最相关的地方。

    享受吧!

    (请注意:这段代码不是我的,我只是对其进行了改编,并认为这将是一个帮助你们的好主意)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-18
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多