We are using uni commerce for ship products. our magento all price calculation are working correctly without any issue. but in magento orders rest api response items base price is coming including tax & they are using base price including tax in their system. I am getting the orders API using below code
$userData = array("username" => "uni", "password" => "admin@123");
$ch = curl_init("https://ezeperfumes.com/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: ".strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("https://ezeperfumes.com/rest/V1/orders/1706");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result);
In this code response for items coming like this
[base_original_price] => 695
[base_price] => 628.8095
[base_price_incl_tax] => 695
All other price are correct but base_price is comming including tax so I have to change it
for fix this issue I have created a custom module
my di.xml code is
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoSalesModelOrderRepository">
<plugin name="order_repository" type="SkOrderAPIPluginMagentoSalesModelOrderRepository" disabled="false" sortOrder="200" />
</type>
<type name="MagentoSalesModelOrderItemRepositoryInterface">
<plugin name="order_item_repository" type="SkOrderAPIPluginMagentoSalesModelOrderItemRepository" disabled="false" sortOrder="210" />
</type>
I have created both files inside app/code/Sk/OrderAPI/Plugin/Magento/Sales/Model OrderRepository.php & OrderItemRepository.php
By using OrderRepository.php I am able to change subtotal & all other order data but can’t change items price in both files not working
I am adding both files code
OrderRepository.php (its working and changing the subtotal as I want)
<?php
namespace SkOrderAPIPluginMagentoSalesModel;
use MagentoSalesApiDataOrderInterface;
use MagentoSalesModelOrderRepository as ModelOrderRepository;
use MagentoSalesApiDataOrderExtensionFactory;
use MagentoSalesApiDataOrderExtensionInterface;
use MagentoSalesApiDataOrderSearchResultInterface;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoFrameworkExceptionCouldNotSaveException;
class OrderRepository
{
protected $extensionFactory;
/**
* @param ModelOrderRepository $orderRepository
* @param OrderInterface $order
* @return OrderInterface
*/
public function __construct(OrderExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
public function afterGet(
ModelOrderRepository $orderRepository,
OrderInterface $order
) {
$order['subtotal'] = $order->getGrandTotal();
$order['base_subtotal'] = $order->getGrandTotal();
$order['subtotal_incl_tax'] = $order->getGrandTotal();
return $order;
}
public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
{
$orders = $searchResult->getItems();
$items = array();
foreach ($orders as &$order) {
$order->setSubTotal($order->getGrandTotal());
$order->setSubTotalInclTax($order->getGrandTotal());
$order->setBaseSubTotal($order->getGrandTotal());
$order->setBaseSubTotalInclTax($order->getGrandTotal());
}
return $searchResult;
}
}
but its not working OrderItemRepository.php file code
<?php
namespace SkOrderAPIPluginMagentoSalesModel;
use MagentoSalesApiDataOrderItemExtensionFactory;
use MagentoSalesApiDataOrderItemExtensionInterface;
use MagentoSalesApiDataOrderItemInterface;
use MagentoSalesApiDataOrderItemSearchResultInterface;
use MagentoSalesApiOrderItemRepositoryInterface;
/**
* Class OrderItemRepository
*/
class OrderItemRepository
{
protected $extensionFactory;
/**
* OrderItemRepositoryPlugin constructor
*
* @param OrderItemExtensionFactory $extensionFactory
*/
public function __construct(OrderItemExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
/**
*
* @param OrderItemRepositoryInterface $subject
* @param OrderItemInterface $orderItem
*
* @return OrderItemInterface
*/
public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem)
{
$extensionAttributes = $orderItem->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$extensionAttributes->setBasePrice(15);
$orderItem->setExtensionAttributes($extensionAttributes);
return $orderItem;
}
/**
*
* @param OrderItemRepositoryInterface $subject
* @param OrderItemSearchResultInterface $searchResult
*
* @return OrderItemSearchResultInterface
*/
public function afterGetList(OrderItemRepositoryInterface $subject, OrderItemSearchResultInterface $searchResult)
{
$orderItems = $searchResult->getItems();
foreach ($orderItems as &$item) {
$extensionAttributes = $item->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$extensionAttributes->setBasePrice(10);
$item->setExtensionAttributes($extensionAttributes);
}
return $searchResult;
}
}
I have tried to print the log files also but order api not hit any function from OrderItemRepository class.
I have created all files of module with extension_attributes.xml file as well . here I am checking the response https://magento-999921-3523455.cloudwaysapps.com/fetch_orders.php
If anyone can help me what I am doing wrong & why can’t change item base price that will help me a lot?