Skip to content

Commit

Permalink
Merge pull request #2839 from magento/MSI-2826-Hide-store-pickup-duri…
Browse files Browse the repository at this point in the history
…ng-order-submissions-if-one-of-the-products-is-not-available-for-pickup

MSI-2826-Hide-store-pickup-during-order-submissions-if-one-of-the-products-is-not-available-for-pickup.
  • Loading branch information
sidolov authored Mar 12, 2020
2 parents 95104be + 87365be commit bac01be
Show file tree
Hide file tree
Showing 14 changed files with 542 additions and 9 deletions.
61 changes: 61 additions & 0 deletions InventoryInStorePickup/Model/ProductInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model;

use Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoExtensionInterface;
use Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoInterface;

/**
* @inheritdoc
*/
class ProductInfo implements ProductInfoInterface
{
/**
* @var string
*/
private $sku;

/**
* @var ProductInfoExtensionInterface
*/
private $productInfoExtension;

/**
* @param string $sku
* @param ProductInfoExtensionInterface|null $productInfoExtension
*/
public function __construct(string $sku, ?ProductInfoExtensionInterface $productInfoExtension = null)
{
$this->sku = $sku;
$this->productInfoExtension = $productInfoExtension;
}

/**
* @inheritdoc
*/
public function getSku(): string
{
return $this->sku;
}

/**
* @inheritdoc
*/
public function getExtensionAttributes(): ?ProductInfoExtensionInterface
{
return $this->productInfoExtension;
}

/**
* @inheritdoc
*/
public function setExtensionAttributes(ProductInfoExtensionInterface $extension): void
{
$this->productInfoExtension = $extension;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\ResourceModel;

use Magento\Framework\DB\Select;
use Magento\Framework\DB\Sql\ExpressionFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Inventory\Model\ResourceModel\Source;
use Magento\Inventory\Model\ResourceModel\SourceItem;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;

/**
* Provides list of Source Codes which have all requested products assigned.
*/
class GetPickupLocationIntersectionForSkus
{
/**
* @var SourceItem
*/
private $sourceItemResource;

/**
* @var Source
*/
private $sourceResource;

/**
* @var ExpressionFactory
*/
private $expressionFactory;

/**
* @param SourceItem $sourceItemResource
* @param Source $sourceResource
* @param ExpressionFactory $expressionFactory
*/
public function __construct(
SourceItem $sourceItemResource,
Source $sourceResource,
ExpressionFactory $expressionFactory
) {
$this->sourceItemResource = $sourceItemResource;
$this->sourceResource = $sourceResource;
$this->expressionFactory = $expressionFactory;
}

/**
* Provide intersection of products availability in sources.
*
* @param string[] $skus
*
* @return array
* @throws LocalizedException
*/
public function execute(array $skus): array
{
$select = $this->sourceItemResource->getConnection()->select();
$expression = $this->expressionFactory->create(['expression' => 'COUNT(' . SourceItemInterface::SKU . ')']);
$select->from($this->sourceItemResource->getMainTable())
->joinInner(
$this->sourceResource->getMainTable(),
$this->sourceItemResource->getMainTable() . '.' . SourceItemInterface::SOURCE_CODE . '=' .
$this->sourceResource->getMainTable() . '.' . SourceInterface::SOURCE_CODE
)
->where(SourceItemInterface::SKU . ' in (?) ', $skus)
->where(SourceInterface::ENABLED . ' = 1')
->where(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE . ' = 1')
->reset(Select::COLUMNS)
->columns([SourceItemInterface::SOURCE_CODE])
->group($this->sourceItemResource->getMainTable() . '.' . SourceItemInterface::SOURCE_CODE)
->having($expression . ' = ' . count($skus));
$result = $this->sourceItemResource->getConnection()->fetchAssoc($select);
$sourceCodes = [];
foreach ($result as $row) {
$sourceCodes[] = $row[SourceItemInterface::SOURCE_CODE];
}

return $sourceCodes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\SearchCriteria;

use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryInStorePickup\Model\ResourceModel\GetPickupLocationIntersectionForSkus;
use Magento\InventoryInStorePickupApi\Api\Data\SearchRequestInterface;
use Magento\InventoryInStorePickupApi\Model\SearchCriteria\BuilderPartsResolverInterface;
use Magento\InventoryInStorePickupApi\Model\SearchCriteria\SearchCriteriaBuilderDecorator;

/**
* Find intersection of assignments of products between Pickup Locations and filter by codes.
*/
class ResolveIntersection implements BuilderPartsResolverInterface
{
/**
* @var GetPickupLocationIntersectionForSkus
*/
private $getPickupLocationIntersectionForSkus;

/**
* @param GetPickupLocationIntersectionForSkus $getPickupLocationIntersectionForSkus
*/
public function __construct(
GetPickupLocationIntersectionForSkus $getPickupLocationIntersectionForSkus
) {
$this->getPickupLocationIntersectionForSkus = $getPickupLocationIntersectionForSkus;
}

/**
* Search intersection of assignments of products between Pickup Locations.
*
* @param SearchRequestInterface $searchRequest
* @param SearchCriteriaBuilderDecorator $searchCriteriaBuilder
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function resolve(
SearchRequestInterface $searchRequest,
SearchCriteriaBuilderDecorator $searchCriteriaBuilder
): void {
if (!$searchRequest->getExtensionAttributes()
|| !$searchRequest->getExtensionAttributes()->getProductsInfo()
) {
return;
}

$extensionAttributes = $searchRequest->getExtensionAttributes();
$skus = [];
foreach ($extensionAttributes->getProductsInfo() as $item) {
$skus[] = $item->getSku();
}

$codes = $this->getPickupLocationIntersectionForSkus->execute($skus);
$codes = implode(',', $codes);
$searchCriteriaBuilder->addFilter(SourceInterface::SOURCE_CODE, $codes, 'in');
}
}
3 changes: 2 additions & 1 deletion InventoryInStorePickup/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"magento/module-inventory-source-selection-api": "*",
"magento/module-inventory-sales-api": "*",
"magento/module-inventory-catalog-api": "*",
"magento/module-directory": "*"
"magento/module-directory": "*",
"magento/module-inventory": "*"
},
"type": "magento2-module",
"license": [
Expand Down
2 changes: 2 additions & 0 deletions InventoryInStorePickup/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<item name="resolve-distance-filter" xsi:type="object">Magento\InventoryInStorePickup\Model\SearchCriteria\ResolveArea</item>
<item name="resolve-scope-filter" xsi:type="object">Magento\InventoryInStorePickup\Model\SearchCriteria\ResolveScopeFilter</item>
<item name="resolve-meta" xsi:type="object">Magento\InventoryInStorePickup\Model\SearchCriteria\ResolveMeta</item>
<item name="resolve-products-assignment-intersection" xsi:type="object">Magento\InventoryInStorePickup\Model\SearchCriteria\ResolveIntersection</item>
</argument>
</arguments>
</type>
Expand Down Expand Up @@ -69,4 +70,5 @@
</argument>
</arguments>
</type>
<preference for="Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoInterface" type="Magento\InventoryInStorePickup\Model\ProductInfo" />
</config>
1 change: 1 addition & 0 deletions InventoryInStorePickup/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module name="Magento_InventorySourceSelectionApi" />
<module name="Magento_InventorySalesApi" />
<module name="Magento_Directory" />
<module name="Magento_Inventory" />
</sequence>
</module>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\InventoryInStorePickupApi\Api\Data\SearchRequest;

use Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\FiltersExtensionInterface;

/**
* Filter to filter by Fields.
* Each field may be filtered with different condition type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickupApi\Api\Data\SearchRequest;

use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoExtensionInterface;

/**
* Product Info Data Transfer Object.
* @api
*/
interface ProductInfoInterface extends ExtensibleDataInterface
{
/**
* Get Product SKU.
*
* @return string
*/
public function getSku(): string;

/**
* Get Product Info Extension.
*
* @return \Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoExtensionInterface|null
*/
public function getExtensionAttributes(): ?ProductInfoExtensionInterface;

/**
* Set Product Info Extension.
*
* @param \Magento\InventoryInStorePickupApi\Api\Data\SearchRequest\ProductInfoExtensionInterface $extension
* @return void
*/
public function setExtensionAttributes(ProductInfoExtensionInterface $extension): void;
}
Loading

0 comments on commit bac01be

Please sign in to comment.