Skip to content

Commit

Permalink
Merge pull request magento#1274 from magento-engcom/MSI-1248
Browse files Browse the repository at this point in the history
Cannot cancel order with deleted product.
  • Loading branch information
maghamed authored May 31, 2018
2 parents 33e7856 + dfeb05e commit 22f7828
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\ItemToSellInterfaceFactory;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\InputException;

class CancelOrderItemObserver implements ObserverInterface
{
Expand Down Expand Up @@ -87,18 +88,27 @@ public function __construct(
/**
* @param EventObserver $observer
* @return void
* @throws LocalizedException
*/
public function execute(EventObserver $observer)
{
/** @var \Magento\Sales\Model\Order\Item $item */
/** @var OrderItem $item */
$item = $observer->getEvent()->getItem();
$children = $item->getChildrenItems();
$qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled();
if ($item->getId() && $item->getProductId() && empty($children) && $qty) {
$productSku = $item->getSku() ?: $this->getSkusByProductIds->execute(
[$item->getProductId()]
)[$item->getProductId()];
$qty = $item->getQtyToCancel();
if ($this->canCancelOrderItem($item) && $qty) {
try {
$productSku = $this->getSkusByProductIds->execute(
[$item->getProductId()]
)[$item->getProductId()];
} catch (InputException $e) {
/**
* As it was decided the Inventory should not use data constraints depending on Catalog
* (these two systems are not highly coupled, i.e. Magento does not sync data between them, so that
* it's possible that SKU exists in Catalog, but does not exist in Inventory and vice versa)
* it is necessary for Magento to have an ability to process placed orders even with
* deleted or non-existing products
*/
return;
}

$itemToSell = $this->itemsToSellFactory->create([
'sku' => $productSku,
Expand Down Expand Up @@ -126,4 +136,16 @@ public function execute(EventObserver $observer)

$this->priceIndexer->reindexRow($item->getProductId());
}

/**
* @param OrderItem $orderItem
* @return bool
*/
private function canCancelOrderItem(OrderItem $orderItem): bool
{
if ($orderItem->getId() && $orderItem->getProductId() && !$orderItem->isDummy()) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\Framework\Exception\InputException;

/**
* Class provides around Plugin on \Magento\CatalogInventory\Model\StockManagement::backItemQty
Expand Down Expand Up @@ -86,13 +87,29 @@ public function __construct(
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundBackItemQty(StockManagement $subject, callable $proceed, $productId, $qty, $scopeId = null)
{
public function aroundBackItemQty(
StockManagement $subject,
callable $proceed,
$productId,
$qty,
$scopeId = null
): bool {
if (null === $scopeId) {
throw new LocalizedException(__('$scopeId is required'));
}

$productSku = $this->getSkusByProductIds->execute([$productId])[$productId];
try {
$productSku = $this->getSkusByProductIds->execute([$productId])[$productId];
} catch (InputException $e) {
/**
* As it was decided the Inventory should not use data constraints depending on Catalog
* (these two systems are not highly coupled, i.e. Magento does not sync data between them, so that
* it's possible that SKU exists in Catalog, but does not exist in Inventory and vice versa)
* it is necessary for Magento to have an ability to process placed orders even with
* deleted or non-existing products
*/
return true;
}
$productType = $this->getProductTypesBySkus->execute([$productSku])[$productSku];

if (true === $this->isSourceItemManagementAllowedForProductType->execute($productType)) {
Expand Down

0 comments on commit 22f7828

Please sign in to comment.