Skip to content

Commit

Permalink
Created Bulk ship actions
Browse files Browse the repository at this point in the history
  • Loading branch information
broopdias committed Mar 29, 2021
1 parent 002a419 commit 422c98b
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 9 deletions.
112 changes: 111 additions & 1 deletion Controller/Adminhtml/Order/MassOrderShip.php
Original file line number Diff line number Diff line change
@@ -1 +1,111 @@
test
<?php

namespace OuterEdge\BulkActions\Controller\Adminhtml\Order;

use Magento\Backend\App\Action\Context;
use Magento\Sales\Model\Order as ModelOrder;
use Magento\Sales\Model\Convert\Order as ConvertOrder;
use Magento\Framework\Message\ManagerInterface;
use Magento\Shipping\Model\ShipmentNotifier;

class MassOrderShip extends \Magento\Backend\App\Action
{
/**
* @var ModelOrder
*/
protected $orderModel;

/**
* @var ConvertOrder
*/
protected $convertOrder;

/**
* @var ManagerInterface
*/
protected $messageManager;

/**
* @var ShipmentNotifier
*/
protected $shipmentNotifier;

/**
* Preparation constructor.
* @param Context $context
* @param ManagerInterface $messageManager
* @param ModelOrder $orderModel
* @param ConvertOrder $convertOrder
* @param ShipmentNotifier $shipmentNotifier
*/
public function __construct(
Context $context,
ManagerInterface $messageManager,
ModelOrder $orderModel,
ConvertOrder $convertOrder,
ShipmentNotifier $shipmentNotifier
) {
parent::__construct($context);

$this->messageManager = $messageManager;
$this->orderModel = $orderModel;
$this->convertOrder = $convertOrder;
$this->shipmentNotifier = $shipmentNotifier;
}

public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();

$ordersId = $this->getRequest()->getParam('selected');
$notify = $this->getRequest()->getParam('notify');

if (!empty($ordersId)) {
// Ship Order
foreach ($ordersId as $orderId) {

$order = $this->orderModel->loadByAttribute('entity_id', $orderId);
if ($order->canShip()) {

$convertOrder = $this->convertOrder;
$shipment = $convertOrder->toShipment($order);

foreach ($order->getAllItems() AS $orderItem) {
// Check if order item has qty to ship or is virtual
if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
continue;
}
$qtyShipped = $orderItem->getQtyToShip();
// Create shipment item with qty
$shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
// Add shipment item to shipment
$shipment->addItem($shipmentItem);
}

// Register shipment
$shipment->register();
$shipment->getOrder()->setIsInProcess(true);

try {
// Save created shipment and order
$shipment->save();
$shipment->getOrder()->save();

// Send email
if ($notify) {
$this->shipmentNotifier->notify($shipment);
$shipment->save();
}
$this->messageManager->addSuccess(__("Shipment Succesfully Generated for order: #".$order->getIncrementId()));
} catch (\Exception $e) {
$this->messageManager->addError(__('Cannot ship order'. $e->getMessage()));
}

} else {
$this->messageManager->addError(__("Cannot ship order, becuase It's already created or something went wrong"));
}
}
}
return $resultRedirect->setPath('sales/order/index', [], ['error' => true]);
}
}
33 changes: 33 additions & 0 deletions Ui/Component/Action/AddParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace OuterEdge\BulkActions\Ui\Component\Action;

class AddParams extends \Magento\Ui\Component\Action
{
protected $urlBuilder;

public function __construct(
\Magento\Framework\View\Element\UiComponent\ContextInterface $context,
\Magento\Framework\UrlInterface $urlBuilder,
array $components = array(),
array $data = array(),
$actions = null
) {
parent::__construct($context, $components, $data, $actions);

$this->urlBuilder = $urlBuilder;
}

public function prepare()
{
parent::prepare();

$config = $this->getConfiguration();

$params = array('notify' => '1');

$config['url'] = $this->urlBuilder->getUrl('shipbulkactions/order/massordership/', $params);

$this->setData('config', $config);
}
}
8 changes: 8 additions & 0 deletions etc/adminhtml/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="shipbulkactions" frontName="shipbulkactions">
<module name="OuterEdge_BulkActions" before="Magento_Backend" />
</route>
</router>
</config>
16 changes: 8 additions & 8 deletions view/adminhtml/ui_component/sales_order_grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
<massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions">
<action name="set_order_to_in_production">
<action name="ship_and_notify" class="OuterEdge\BulkActions\Ui\Component\Action\AddParams">
<settings>
<url path="adminhtml/order/setOrderToInProduction"/>
<type>set_order_to_in_production</type>
<label translate="true">Set to In Production</label>
<url path="shipbulkactions/order/massordership"/>
<type>ship_and_notify</type>
<label translate="true">Ship (notify customer)</label>
</settings>
</action>
<action name="send_shipment_email_and_complete_order">
<action name="ship_and_dont_notify">
<settings>
<url path="adminhtml/order/massSendEmailAndComplete"/>
<type>send_shipment_email_and_complete_order</type>
<label translate="true">Send email &amp; complete order</label>
<url path="shipbulkactions/order/massordership"/>
<type>ship_and_dont_notify</type>
<label translate="true">Ship (don't notify customer)</label>
</settings>
</action>
</massaction>
Expand Down

0 comments on commit 422c98b

Please sign in to comment.