2
votes

By default Magento's Order REST API doesn't submit the store credit amount used for the order(customer_balance_amount col in the DB). I need to expose that to the API interface, but as of now am unable to. I tried two approaches:

http://magehit.com/blog/how-to-get-value-of-custom-attribute-on-magento-2-rest-api/ - using an observer, but that doesn't seem to have any reflection on the API data

and

http://www.ipragmatech.com/extend-magento2-rest-api-easy-steps/ - which I successfully tried, but it concerns actually creating a new ednpoint instead of overriding/extending the current API.

I was actually able to reproduce that by directly altering the OrderInterface and Order model inside the module-sales core module, but I want to achieve that the "proper" way instead of modifying core.

I would be thankful if anyone shares some knowledge how to do that.

Edit: adding the code that made the solution working, but the goal is to make it the proper way, not edit the core files like so:

vendor/magento/module-sales/Api/Data/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);

vendor/magento/module-sales/model/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);
}

Regards, Alex

1
Links to where you got code are nice, but you need to include the relevant code in your question - Machavity♦
@Machavity thanks for the remark. I added an example code that actually works, but through modifying the core directly, so my question is how to do it the "proper way". - alex.b.bg

1 Answers

1
votes

It looks like this is actually a bug, since Magento does define the balance columns as extension attributes in vendor/magento/module-customer-balance/etc/extension_attributes.xml

Looking at the GiftMessage module, the way to do this is via a plugin.

vendor/magento/module-gift-message/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\Model\Plugin\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;
}