Created
March 30, 2020 08:14
-
-
Save SiarheyUchukhlebau/da8497a80b88845b9c85fe774e3c2c7b to your computer and use it in GitHub Desktop.
Edit order amounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright © MageWorx. All rights reserved. | |
* See LICENSE.txt for license details. | |
*/ | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
ini_set('memory_limit', '5G'); | |
error_reporting(E_ALL); | |
use Magento\Framework\App\Bootstrap; | |
use Magento\Framework\Exception\NoSuchEntityException; | |
require 'app/bootstrap.php'; | |
// Code | |
$bootstrap = Bootstrap::create(BP, $_SERVER); | |
$objectManager = $bootstrap->getObjectManager(); | |
/** @var \Magento\Framework\App\State $state */ | |
$state = $objectManager->get('Magento\Framework\App\State'); | |
try { | |
// Set STATE to adminhtml to imitate admin side actions | |
$state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML); | |
} catch (Exception $exception) { | |
echo $exception->getMessage(); | |
die(); | |
} | |
$orderId = 1319; | |
/** @var \MageWorx\OrderEditor\Model\Order\OrderRepository $mwOrderRepository */ | |
$mwOrderRepository = $objectManager->get('MageWorx\OrderEditor\Model\Order\OrderRepository'); | |
/** @var \MageWorx\OrderEditor\Model\Order\OrderItemRepository $mwOrderItemRepository */ | |
$mwOrderItemRepository = $objectManager->get('MageWorx\OrderEditor\Model\Order\OrderItemRepository'); | |
/** @var \MageWorx\OrderEditor\Model\Order $order */ | |
try { | |
$order = $mwOrderRepository->getById($orderId); | |
} catch (NoSuchEntityException $noSuchEntityException) { | |
echo __('No such order with id %1', $orderId); | |
die(); | |
} | |
$editItemId = 2197; | |
try { | |
$editItem = $mwOrderItemRepository->getById($editItemId); | |
} catch (NoSuchEntityException $noSuchEntityException) { | |
echo __('No such order item with id %1', $editItemId); | |
die(); | |
} catch (\Magento\Framework\Exception\LocalizedException $localizedException) { | |
echo $localizedException->getMessage(); | |
die(); | |
} | |
$editItemData = []; | |
$editItemData['item_id'] = $editItemId; | |
$editItemData['item_type'] = 'order'; | |
$editItemData['price'] = '200'; | |
$editItemData['price_incl_tax'] = '210'; | |
$editItemData['subtotal'] = '200'; | |
$editItemData['subtotal_incl_tax'] = '210'; | |
$editItemData['tax_amount'] = '10'; | |
$editItemData['tax_percent'] = '5'; | |
$editItemData['row_total'] = '210'; | |
$editItemData['tax_applied_rates'] = [ | |
'CUSTOMRATE' => '5' | |
]; | |
$editItemData['tax_rates'] = [ | |
'CUSTOMRATE' | |
]; | |
$editItemData['discount_amount'] = '0'; | |
$editItemData['discount_percent'] = '0'; | |
/* Items to edit */ | |
$items = [ | |
/* product id */ | |
$editItemId => $editItemData | |
]; | |
$params = [ | |
'order_id' => $orderId, | |
'item' => $items | |
]; | |
try { | |
$order->editItems($params); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
echo $e->getTraceAsString(); | |
die(); | |
} | |
echo __('Success!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment