Created
December 4, 2019 12:21
-
-
Save SiarheyUchukhlebau/49bbb57478bc71d6e6be594334f1628b to your computer and use it in GitHub Desktop.
Edit order items; Add new items to order;
This file contains hidden or 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(); | |
} | |
$orderId = 1205;//1195; | |
/** @var \MageWorx\OrderEditor\Model\Order\OrderRepository $mwOrderRepository */ | |
$mwOrderRepository = $objectManager->get('MageWorx\OrderEditor\Model\Order\OrderRepository'); | |
/** @var \MageWorx\OrderEditor\Model\Edit\Quote $mwQuoteProcessor */ | |
$mwQuoteProcessor = $objectManager->create('MageWorx\OrderEditor\Model\Edit\Quote'); | |
/** @var \MageWorx\OrderEditor\Model\Order $order */ | |
try { | |
$order = $mwOrderRepository->getById($orderId); | |
} catch (NoSuchEntityException $noSuchEntityException) { | |
echo __('No such order with id %1', $orderId); | |
} | |
/* Items to add */ | |
$items = [ | |
/* product id */ | |
2252 => [ | |
/* requested item params (buyRequest) */ | |
'qty' => 1 | |
] | |
]; | |
try { | |
$orderItems = $mwQuoteProcessor->createNewOrderItems($items, $order); | |
} catch (Exception $e) { | |
echo __('Unable to add items to the quote, error: %1', $e->getMessage()); | |
} | |
/* | |
* Example request from magento admin: | |
* q prefix is for newly added items (q - quote) | |
* another items - regular items in order (existing items) | |
* the "action" field could be used if you wish to remove an item. | |
form_key: 8N11LLf4Q6Se5YwR | |
order_id: 1204 | |
block_id: order_items | |
item[1796][item_id]: 1796 | |
item[1796][item_type]: order | |
item[1796][product_id]: 2259 | |
item[1796][discount_tax_compensation_amount]: 0.00 | |
item[1796][weee_tax_applied_row_amount]: 0.00 | |
item[1796][price]: 50.50 | |
item[1796][price_incl_tax]: 50.50 | |
item[1796][fact_qty]: 1 | |
item[1796][subtotal]: 50.50 | |
item[1796][subtotal_incl_tax]: 50.50 | |
item[1796][tax_amount]: 0.00 | |
item[1796][tax_percent]: 0.00 | |
item[1796][discount_amount]: 0.00 | |
item[1796][discount_percent]: 0.00 | |
item[1796][row_total]: 50.50 | |
item[1796][action]: | |
item[1796][back_to_stock]: 1 | |
item[1797][item_id]: 1797 | |
item[1797][item_type]: order | |
item[1797][product_id]: 2258 | |
item[1797][discount_tax_compensation_amount]: 0.00 | |
item[1797][weee_tax_applied_row_amount]: 0.00 | |
item[1797][price]: 49.50 | |
item[1797][price_incl_tax]: 49.50 | |
item[1797][fact_qty]: 1 | |
item[1797][subtotal]: 49.50 | |
item[1797][subtotal_incl_tax]: 49.50 | |
item[1797][tax_amount]: 0.00 | |
item[1797][tax_percent]: 0.00 | |
item[1797][discount_amount]: 0.00 | |
item[1797][discount_percent]: 0.00 | |
item[1797][row_total]: 49.50 | |
item[1797][action]: | |
item[1797][back_to_stock]: 1 | |
item[q3011][item_id]: 3011 | |
item[q3011][item_type]: quote | |
item[q3011][product_id]: 2253 | |
item[q3011][discount_tax_compensation_amount]: 0.00 | |
item[q3011][weee_tax_applied_row_amount]: 0.00 | |
item[q3011][price]: 1.00 | |
item[q3011][price_incl_tax]: 1.00 | |
item[q3011][fact_qty]: 1 | |
item[q3011][subtotal]: 1.00 | |
item[q3011][subtotal_incl_tax]: 1.00 | |
item[q3011][tax_amount]: 0.00 | |
item[q3011][tax_percent]: 0.00 | |
item[q3011][discount_amount]: 0.00 | |
item[q3011][discount_percent]: 0.00 | |
item[q3011][row_total]: 1.00 | |
item[q3011][action]: | |
*/ | |
$requestItems = []; | |
foreach ($orderItems as $item) { | |
$requestItems['q' . $item->getId()] = [ | |
'item_id' => $item->getId(), | |
'item_type' => 'quote', | |
'product_id' => $item->getProductId(), | |
'discount_tax_compensation_amount' => $item->getData('discount_tax_compensation_amount'), | |
'weee_tax_applied_row_amount' => $item->getData('weee_tax_applied_row_amount'), | |
'price' => $item->getData('price'), | |
'price_incl_tax' => $item->getData('price_incl_tax'), | |
'fact_qty' => $item->getQtyOrdered(), | |
'subtotal' => $item->getData('subtotal'), | |
'subtotal_incl_tax' => $item->getData('subtotal_incl_tax'), | |
'tax_amount' => $item->getData('tax_amount'), | |
'tax_percent' => $item->getData('tax_percent'), | |
'discount_amount' => $item->getData('discount_amount'), | |
'discount_percent' => $item->getData('discount_percent'), | |
'row_total' => $item->getData('row_total'), | |
'action' => $item->getData('action'), | |
]; | |
} | |
$params = [ | |
'order_id' => $orderId, | |
'item' => $requestItems | |
]; | |
try { | |
$order->editItems($params); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
echo $e->getTraceAsString(); | |
} | |
echo __('Success!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment