Magento2:添加自定义引用扩展属性

时间:2017-03-18 19:35:42

标签: magento2

我正在尝试通过自定义REST API向引号添加扩展属性,我想稍后检索此信息。

但是当我尝试获取值时,总是为空。

代码会比我更好解释。

等/ di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

[...]

<preference for="Wow\Orderplatform\Api\Data\CartInterface"
               type="Wow\Orderplatform\Model\Cart" />
</config>

等/ extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\CartInterface">
    <attribute code="order_platform" type="Wow\Orderplatform\Api\Data\CartInterface"/>
</extension_attributes>
</config>

CartInterface

interface CartInterface extends ExtensibleDataInterface
{

const ORDER_PLATFORM = 'order_platform';

/**
 * Retrieve order platform
 *
 * @return string
 */
public function getOrderPlatform();

/**
 * Set order platform
 *
 * @param string $orderPlatform
 * @return $this
 */
public function setOrderPlatform($orderPlatform);
}

实施:

class Cart extends AbstractExtensibleModel implements CartInterface
{
/**
 * {inheritdoc}
 */
public function getOrderPlatform()
{
    return $this->getData(self::ORDER_PLATFORM);
}

/**
 * {inheritdoc}
 */
public function setOrderPlatform($orderPlatform)
{
    return $this->setData(self::ORDER_PLATFORM, $orderPlatform);
}
} 

我尝试在REST API中保存扩展属性:

  $quote = $this->quoteRepository->getActive($cartId);
  $extensions = $quote->getExtensionAttributes();
  $platformExt = $this->wowCartExtInterfaceFactory->create();
  $platformExt->setOrderPlatform($platform);
  $extensions->setOrderPlatform($platformExt);
  $quote->setExtensionAttributes($extensions);
  $this->quoteRepository->save($quote);

此过程中没有错误。

问题在于我无法从报价中取回值:

  $quote = $this->quoteRepository->getActive($cartId);    
  $extensions = $quote->getExtensionAttributes();
  $extensions->getOrderPlatform(); // Always null

任何提示?

由于

1 个答案:

答案 0 :(得分:2)

这是magento2的核心错误,试图为某些要求做同样的事情并找到

加入扩展程序属性不会添加到引用结果中 根据我的调试

,连接扩展属性似乎存在问题

离。

<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
    <attribute code="original_catalog_price" type="Magento\NegotiableQuote\Api\Data\NegotiableQuoteItemInterface" >
        <join reference_table="negotiable_quote_item" reference_field="quote_item_id" join_on_field="item_id">
            <field>original_catalog_price</field>
        </join>
    </attribute>
</extension_attributes>

希望补丁很快就会发布。