Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSI: #302 (Refactoring CatalogSearch Module) #304

Closed
wants to merge 19 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MSI: Extend StockStatusFilter in order to filter for sub products
vadimjustus committed Dec 29, 2017
commit cc7e3fdd5275908ef3830cdf51e67e0e395d683f
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@

use Magento\Framework\DB\Select;
use Magento\CatalogSearch\Model\Search\FilterMapper\StockStatusFilter as OriginStockStatusFilter;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Search\Adapter\Mysql\ConditionManager;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
@@ -20,9 +22,13 @@
class StockStatusFilter
{
/**
*
* Stock table names and aliases
*/
const STOCK_TABLE_BASE_NAME = 'inventory_stock_stock_';
const TABLE_ALIAS_PRODUCT = 'product';
const TABLE_ALIAS_SUB_PRODUCT = 'sub_product';
const TABLE_ALIAS_STOCK_INDEX = 'stock_index';
const TABLE_ALIAS_SUB_PRODUCT_STOCK_INDEX = 'sub_product_stock_index';

/**
* @var ConditionManager
@@ -62,6 +68,8 @@ public function __construct(
* @param string $type
* @param bool $showOutOfStockFlag
* @return Select
* @throws \InvalidArgumentException
* @throws LocalizedException
*/
public function aroundApply(
OriginStockStatusFilter $subject,
@@ -71,7 +79,6 @@ public function aroundApply(
$type,
$showOutOfStockFlag
) {

if ($type !== OriginStockStatusFilter::FILTER_JUST_ENTITY &&
$type !== OriginStockStatusFilter::FILTER_ENTITY_AND_SUB_PRODUCTS
) {
@@ -84,6 +91,11 @@ public function aroundApply(
$this->addProductEntityJoin($select, $mainTableAlias);
$this->addInventoryStockJoin($select, $showOutOfStockFlag);

if ($type === OriginStockStatusFilter::FILTER_ENTITY_AND_SUB_PRODUCTS) {
$this->addSubProductEntityJoin($select, $mainTableAlias);
$this->addSubProductInventoryStockJoin($select, $showOutOfStockFlag);
}

return $select;
}

@@ -94,35 +106,81 @@ public function aroundApply(
private function addProductEntityJoin(Select $select, $mainTableAlias)
{
$select->joinInner(
['product' => 'catalog_product_entity'],
sprintf('product.entity_id = %s.entity_id', $mainTableAlias),
[static::TABLE_ALIAS_PRODUCT => 'catalog_product_entity'],
sprintf('%s.entity_id = %s.entity_id', static::TABLE_ALIAS_PRODUCT, $mainTableAlias),
[]
);
}

/**
* @param Select $select
* @param string $mainTableAlias
*/
private function addSubProductEntityJoin(Select $select, $mainTableAlias)
{
$select->joinInner(
[static::TABLE_ALIAS_SUB_PRODUCT => 'catalog_product_entity'],
sprintf('%s.entity_id = %s.source_id', static::TABLE_ALIAS_SUB_PRODUCT, $mainTableAlias),
[]
);
}

/**
* @param Select $select
* @param bool $showOutOfStockFlag
* @throws LocalizedException
*/
private function addInventoryStockJoin(Select $select, $showOutOfStockFlag)
{
$select->joinInner(
['stock_index' => $this->getStockTableName()],
'stock_index.sku = product.sku',
[static::TABLE_ALIAS_STOCK_INDEX => $this->getStockTableName()],
sprintf(
'%s.sku = %s.sku',
static::TABLE_ALIAS_STOCK_INDEX,
static::TABLE_ALIAS_PRODUCT
),
[]
);

if ($showOutOfStockFlag === false) {
$select->where($this->conditionManager->generateCondition(
sprintf('%s.quantity', static::TABLE_ALIAS_STOCK_INDEX),
'>',
0
));
}
}

/**
* @param Select $select
* @param bool $showOutOfStockFlag
* @throws LocalizedException
*/
private function addSubProductInventoryStockJoin(Select $select, $showOutOfStockFlag)
{
$select->joinInner(
[static::TABLE_ALIAS_SUB_PRODUCT_STOCK_INDEX => $this->getStockTableName()],
sprintf(
'%s.sku = %s.sku',
static::TABLE_ALIAS_SUB_PRODUCT_STOCK_INDEX,
static::TABLE_ALIAS_SUB_PRODUCT
),
[]
);

if ($showOutOfStockFlag === false) {
$select->where($this->conditionManager->generateCondition(
'stock_index.quantity',
sprintf('%s.quantity', static::TABLE_ALIAS_SUB_PRODUCT_STOCK_INDEX),
'>',
0
));
}
}

/*
/**
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function getStockTableName(): string
{