Skip to content

Commit

Permalink
Adds Plugin to ensure shipment can be sent with qty is 0, solves issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Waleed committed Apr 18, 2023
1 parent be5fa4c commit ce503e8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Ampersand\DisableStockReservation\Model\InventoryApi;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;

class GetInStockSourceItemsBySkusAndSortedSource
{
/**
* @var SourceItemRepositoryInterface
*/
private $sourceItemRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @param SourceItemRepositoryInterface $sourceItemRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
SourceItemRepositoryInterface $sourceItemRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->sourceItemRepository = $sourceItemRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* Retrieve source items for a defined set of SKUs and sorted source codes
*
* @param array $skus
* @param array $sortedSourceCodes
* @return SourceItemInterface[]
*/
public function execute(array $skus, array $sortedSourceCodes): array
{
$skus = array_map('strval', $skus);
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, $skus, 'in')
->addFilter(SourceItemInterface::SOURCE_CODE, $sortedSourceCodes, 'in')
//->addFilter(SourceItemInterface::STATUS, SourceItemInterface::STATUS_IN_STOCK)
->create();

$items = $this->sourceItemRepository->getList($searchCriteria)->getItems();

$itemsSorting = [];
foreach ($items as $item) {
$itemsSorting[] = array_search($item->getSourceCode(), $sortedSourceCodes, true);
}

array_multisort($itemsSorting, SORT_NUMERIC, SORT_ASC, $items);
return $items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Ampersand\DisableStockReservation\Plugin\InventoryInStorePickupSales\Model\Order;

class IsFulfillablePlugin
{
public function aroundExecute()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Ampersand\DisableStockReservation\Plugin\InventoryShippingAdminUi\Ui\DataProvider;

use Magento\InventoryShippingAdminUi\Ui\DataProvider\SourceSelectionDataProvider;

class SourceSelectionDataProviderPlugin
{
public function afterGetData(SourceSelectionDataProvider $subject, $result)
{
foreach ($result as &$data) {
foreach ($data['items'] as &$item) {
$item['isManageStock'] = 0;
}
}

return $result;
}
}
14 changes: 14 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@

<preference for="Ampersand\DisableStockReservation\Api\SourcesRepositoryInterface"
type="Ampersand\DisableStockReservation\Model\SourcesRepository"/>
<!-- Show all assigned sources for all items during shipment creation (regardless of in/out of stock -->
<preference
for="Magento\InventorySourceSelectionApi\Model\GetInStockSourceItemsBySkusAndSortedSource"
type="Ampersand\DisableStockReservation\Model\InventoryApi\GetInStockSourceItemsBySkusAndSortedSource" />

<!-- Pretend All Items are not Manage Stock for shipping purposes -->
<type name="Magento\InventoryShippingAdminUi\Ui\DataProvider\SourceSelectionDataProvider">
<plugin name="disable_manage_stock_for_shipping" type="Ampersand\DisableStockReservation\Plugin\InventoryShippingAdminUi\Ui\DataProvider\SourceSelectionDataProviderPlugin"/>
</type>

<!-- Allow sending pickup notif at 0 qty/out of stock -->
<type name="Magento\InventoryInStorePickupSales\Model\Order\IsFulfillable">
<plugin name="allow_pickup_notif" type="Ampersand\DisableStockReservation\Plugin\InventoryInStorePickupSales\Model\Order\IsFulfillablePlugin"/>
</type>
</config>

0 comments on commit ce503e8

Please sign in to comment.