-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin now un-cancels an order when the order is canceled and a trans…
…action is paid. This can happen when a merchant uses second chance
- Loading branch information
Andy Pieters
committed
Feb 7, 2017
1 parent
3c93da9
commit aed3fdd
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
namespace Paynl\Payment\Observer; | ||
|
||
use Magento\CatalogInventory\Api\StockManagementInterface; | ||
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockProcessor; | ||
use Magento\Framework\Event\Observer as EventObserver; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class SubtractInventoryObserver implements ObserverInterface | ||
{ | ||
/** | ||
* @var StockManagementInterface | ||
*/ | ||
protected $stockManagement; | ||
|
||
/** | ||
* @var \Magento\CatalogInventory\Observer\ItemsForReindex | ||
*/ | ||
protected $itemsForReindex; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* SubtractInventoryObserver constructor. | ||
* @param StockManagementInterface $stockManagement | ||
* @param StockProcessor $stockIndexerProcessor | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
StockManagementInterface $stockManagement, | ||
StockProcessor $stockIndexerProcessor, | ||
LoggerInterface $logger | ||
) | ||
{ | ||
$this->stockManagement = $stockManagement; | ||
$this->stockIndexerProcessor = $stockIndexerProcessor; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Subtract items qtys from stock related with uncancel products. | ||
* | ||
* @param EventObserver $observer | ||
* @return $this | ||
*/ | ||
public function execute(EventObserver $observer) | ||
{ | ||
$order = $observer->getEvent()->getOrder(); | ||
$productQty = $observer->getEvent()->getProductQty(); | ||
|
||
if ($order->getInventoryProcessed()) { | ||
return $this; | ||
} | ||
|
||
/** | ||
* Reindex items | ||
*/ | ||
$itemsForReindex = $this->stockManagement->registerProductsSale( | ||
$productQty, | ||
$order->getStore()->getWebsiteId() | ||
); | ||
|
||
|
||
$productIds = []; | ||
foreach ($itemsForReindex as $item) { | ||
$item->save(); | ||
$productIds[] = $item->getProductId(); | ||
} | ||
if (!empty($productIds)) { | ||
$this->stockIndexerProcessor->reindexList($productIds); | ||
} | ||
|
||
$order->setInventoryProcessed(true); | ||
return $this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="sales_order_uncancel_inventory"> | ||
<observer name="inventory" instance="Paynl\Payment\Observer\SubtractInventoryObserver"/> | ||
</event> | ||
</config> |