Skip to content

Instantly share code, notes, and snippets.

@SiarheyUchukhlebau
Last active April 8, 2020 14:19
Show Gist options
  • Save SiarheyUchukhlebau/8279581ebfde5b76a336df5bce43e775 to your computer and use it in GitHub Desktop.
Save SiarheyUchukhlebau/8279581ebfde5b76a336df5bce43e775 to your computer and use it in GitHub Desktop.
OrderEditor - Edit Shipping Method
<?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 = 1334;
/** @var \MageWorx\OrderEditor\Model\Order\OrderRepository $mwOrderRepository */
$mwOrderRepository = $objectManager->get('MageWorx\OrderEditor\Model\Order\OrderRepository');
/** @var \MageWorx\OrderEditor\Model\Shipping $mwOrderShipping */
$mwOrderShipping = $objectManager->get('MageWorx\OrderEditor\Model\Shipping');
/** @var \MageWorx\OrderEditor\Model\Order $order */
try {
$order = $mwOrderRepository->getById($orderId);
} catch (NoSuchEntityException $noSuchEntityException) {
echo __('No such order with id %1', $orderId);
die();
}
// Set order id
$mwOrderShipping->setOrderId($orderId);
// Set desired params
$shippingParams = [
// required params
'order_id' => $orderId,
'shipping_method' => 'flatrate_flatrate',
'price_excl_tax' => 20,
'price_incl_tax' => 40,
'tax_percent' => 100,
'description' => 'Some description text',
// optional params
'discount_amount' => 3,
'tax_rates' => [
[
'code' => 'BLR50',
'percent' => '80'
],
[
'code' => 'SHIP10',
'percent' => '20'
]
]
];
// Init params (save it in corresponding fields)
$mwOrderShipping->initParams($shippingParams);
try {
$mwOrderShipping->updateShippingMethod();
} 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