Skip to content
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
namespace Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogInventory\Model\Configuration;
use Magento\Eav\Model\Config;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Store\Model\ScopeInterface;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
Expand All @@ -29,6 +32,8 @@
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AttributeData extends AbstractAttributeData
{
Expand Down Expand Up @@ -56,14 +61,22 @@ class AttributeData extends AbstractAttributeData
*/
private $eavConfig;

/**
* Scope Config
*
* @var ScopeConfigInterface
*/
private ScopeConfigInterface $scopeConfig;

/**
* Constructor.
*
* @param ResourceConnection $resource Database adpater.
* @param ResourceConnection $resource Database adapter.
* @param StoreManagerInterface $storeManager Store manager.
* @param MetadataPool $metadataPool Metadata Pool.
* @param ProductType $catalogProductType Product type.
* @param Config $eavConfig Eav config.
* @param ScopeConfigInterface $scopeConfig Scope Config.
* @param string $entityType Product entity type.
*/
public function __construct(
Expand All @@ -72,11 +85,13 @@ public function __construct(
MetadataPool $metadataPool,
ProductType $catalogProductType,
Config $eavConfig,
ScopeConfigInterface $scopeConfig,
$entityType = ProductInterface::class
) {
parent::__construct($resource, $storeManager, $metadataPool, $entityType);
$this->catalogProductType = $catalogProductType;
$this->eavConfig = $eavConfig;
$this->scopeConfig = $scopeConfig;
}

/**
Expand Down Expand Up @@ -212,6 +227,7 @@ private function getRelationQuery($relation, $parentIds, $storeId)
$entityIdField = $this->getEntityMetaData($this->getEntityTypeId())->getIdentifierField();
$entityTable = $this->getTable($this->getEntityMetaData($this->getEntityTypeId())->getEntityTable());
$relationTable = $this->getTable($relation->getTable());
$inventoryTable = $this->getTable('cataloginventory_stock_item');
$parentFieldName = $relation->getParentFieldName();
$childFieldName = $relation->getChildFieldName();

Expand All @@ -229,6 +245,18 @@ private function getRelationQuery($relation, $parentIds, $storeId)
)
->where("parent.{$entityIdField} in (?)", $parentIds);

/**
* If Catalog - Inventory - Stock Options - Display of Stock Products is set to NO,
* then exclude this children from the query results.
*/
if (!$this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK)) {
$select->joinInner(
['stock' => $inventoryTable],
new \Zend_Db_Expr("child.{$entityIdField} = stock.product_id AND stock.is_in_stock = 1"),
[]
);
}

Comment on lines +248 to +259
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Bashev,

Wouldn't this piece of code only do what it is intended to do if

  • not using MSI
  • using MSI but the current storeId corresponds to a store using the default Stock ?

Regards,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably,
to be honest, i not test it with MSI which have multiple sources and stocks.

I'll try to check these scenarios and will come back.

if ($relation->getWhere() !== null) {
$select->where($relation->getWhere());
}
Expand Down