0

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="\Magento\Sales\Model\OrderRepository">
    <plugin name="order_repository" type="Sk\OrderAPI\Plugin\Magento\Sales\Model\OrderRepository" disabled="false" sortOrder="200" />
</type>
<type name="\Magento\Sales\Model\OrderItemRepositoryInterface">
    <plugin name="order_item_repository" type="Sk\OrderAPI\Plugin\Magento\Sales\Model\OrderItemRepository" 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 Sk\OrderAPI\Plugin\Magento\Sales\Model;

use Magento\Sales\Api\Data\OrderInterface; 
use Magento\Sales\Model\OrderRepository as ModelOrderRepository;


use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;


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 Sk\OrderAPI\Plugin\Magento\Sales\Model;

use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;


/**
 * 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?

1 Answer 1

0

I have found a solution. hope it will help for someone.

No need for OrderItemRepository.php this file so I removed it from di.xml and folder

I changed my OrderRepository.php file like below & its working for me.

<?php

namespace Sk\OrderAPI\Plugin\Magento\Sales\Model;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\OrderRepository as ModelOrderRepository;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;


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) {
        foreach ($order->getItems() as $key => $item) {
            $item->setBasePrice($item->getBaseOriginalPrice());
            $item->setPrice($item->getBaseOriginalPrice());
            
        }
            $order->setBaseTaxAmount(200); 
            $order->setTaxAmount(200); 
            $order->setSubTotal($order->getGrandTotal()); 
            $order->setSubTotalInclTax($order->getGrandTotal()); 
            $order->setBaseSubTotal($order->getGrandTotal()); 
            $order->setBaseSubTotalInclTax($order->getGrandTotal()); 
    }

    return $searchResult;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.