2016-10-21 12 views
1

デフォルトでMagentoのOrder REST APIは、注文に使用された店舗クレジット金額(DBのcustomer_balance_amount col)を提出しません。私はそれをAPIインターフェイスに公開する必要がありますが、現在は不可能です。オブザーバを使用して、それはAPIデータ上の任意の反射Magento 2のOrder REST APIにcustomer_balance_amountを追加

http://www.ipragmatech.com/extend-magento2-rest-api-easy-steps/持っていないようです - -

http://magehit.com/blog/how-to-get-value-of-custom-attribute-on-magento-2-rest-api/私は成功してみましたが、それは実際に作成に関する:私は2つのアプローチを試してみました現在のAPIをオーバーライド/拡張​​するのではなく、新しいエドポイントを作成します。

モジュール販売コアモジュール内のOrderInterfaceとOrderモデルを直接変更することで、実際にそれを再現することができましたが、コアを変更するのではなく「正しい」方法を実現したいと思います。

誰かが何らかの知識を共有してくれれば、感謝しています。

編集:ソリューションの作業をしたコードを追加しますが、目標はそのようなコアファイルを編集し、それ適切な方法ではないようにすることです:

ベンダー/ Magentoの/モジュール販売/ API /データ/ OrderInterface.php:

/* 
* Customer Balance Amount 
*/ 
const CUSTOMER_BALANCE_AMOUNT = 'customer_balance_amount'; 


/** 
* Returns customer_balance_amount 
* 
* @return float Customer Balance Amount 
*/ 
public function getCustomerBalanceAmount(); 


/** 
* Sets the customer_balance_amount for the order. 
* 
* @param float $amount 
* @return $this 
*/ 
public function setCustomerBalanceAmount($amount); 

ベンダー/ Magentoの/モジュール販売/モデル/ Order.php:

/** 
* Returns customer_balance_amount 
* 
* @return float 
*/ 
public function getCustomerBalanceAmount() 
{ 
    return $this->getData(OrderInterface::CUSTOMER_BALANCE_AMOUNT); 
} 


/** 
* Sets the customer_balance_amount for the order. 
* 
* @param float $amount 
* @return $this 
*/ 
public function setCustomerBalanceAmount($amount) 
{ 
    return $this->setData(OrderInterface::CUSTOMER_BALANCE_AMOUNT, $amount); 
} 

よろしく、 アレックス

Magentoのは、拡張子がベンダー/ Magentoの/モジュール-顧客バランスの/ etc/extension_attributes.xml

に属性としてGiftMessageモジュールを見てみると、バランスの列を定義しないのでそれはこのようになります

+1

リンクコードが得意ですが、あなたの質問に関連するコードを含める必要があります – Machavity

+0

@Machavityは発言に感謝します。私は実際に動作するサンプルコードを追加しましたが、コアを直接変更することで、私の質問は「正しい方法」にする方法です。 –

答えて

1

が、実際にはバグで、道これを行うには、プラグイン経由です。

ベンダー/ Magentoの/モジュール・ギフト・メッセージは/ etc/di.xml

<type name="Magento\Sales\Api\OrderRepositoryInterface"> 
    <plugin name="save_gift_message" type="Magento\GiftMessage\Model\Plugin\OrderSave"/> 
    <plugin name="get_gift_message" type="Magento\GiftMessage\Model\Plugin\OrderGet"/> 
</type> 

Magentoの\ GiftMessage \モデル\プラグイン\ OrderGet \あなたに

/** 
* Get gift message for order 
* 
* @param \Magento\Sales\Api\Data\OrderInterface $order 
* @return \Magento\Sales\Api\Data\OrderInterface 
*/ 
protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order) 
{ 
    $extensionAttributes = $order->getExtensionAttributes(); 
    if ($extensionAttributes && $extensionAttributes->getGiftMessage()) { 
     return $order; 
    } 

    try { 
     /** @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */ 
     $giftMessage = $this->giftMessageOrderRepository->get($order->getEntityId()); 
    } catch (NoSuchEntityException $e) { 
     return $order; 
    } 

    /** @var \Magento\Sales\Api\Data\OrderExtension $orderExtension */ 
    $orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create(); 
    $orderExtension->setGiftMessage($giftMessage); 
    $order->setExtensionAttributes($orderExtension); 

    return $order; 
} 
関連する問題