Patch elastic suite without msi

October 22, 2022 ยท View on GitHub

Removed dependency on MSI modules

@package smile/elasticsuite @link https://github.com/Smile-SA/elasticsuite/issues/1222

diff --git src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/InventoryData.php src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/InventoryData.php index ca4557d..f07e059 100644 --- src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/InventoryData.php +++ src/module-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/InventoryData.php @@ -14,7 +14,6 @@ namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource;

use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface; -use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\InventoryDataInterface; use \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\Deprecation\InventoryData as Deprecation;

/** @@ -27,7 +26,7 @@ use \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Data class InventoryData implements DatasourceInterface { /**

  • * @var InventoryDataInterface
    
  • * @var Deprecation
    */
    
    private $resourceModel;

@@ -75,31 +74,15 @@ class InventoryData implements DatasourceInterface }

 /**
  • * Init proper resource model.
    
  • *
    
  • * Should be default implementation of InventoryDataInterface if MSI modules are enabled.
    
  • *
    
  • * Otherwise we fallback to old-style CatalogInventory indexing.
    *
    * @deprecated To be removed with Magento 2.4 and the dismantlement of legacy CatalogInventory module.
    *
    
  • * @return InventoryDataInterface
    
  • * @return Deprecation
    */
    
    private function getResourceModel() { if ($this->resourceModel === null) {
  •        $resourceName = InventoryDataInterface::class;
    
  •        try {
    
  •            // Will try to fetch default implementation and fail in case of missing MSI modules or dependencies.
    
  •            $stockResolver = $this->config->getPreference(\Magento\InventorySalesApi\Api\StockResolverInterface::class);
    
  •            if (ltrim($stockResolver, '\\') === ltrim(\Magento\InventorySalesApi\Api\StockResolverInterface::class, '\\')) {
    
  •                $resourceName = Deprecation::class;
    
  •            }
    
  •        } catch (\Exception $exception) {
    
  •            ; // Nothing to do, it's already kinda hacky to allow this deprecation fallback to happen.
    
  •        }
    
  •        $resourceName = Deprecation::class;
           $this->resourceModel = $this->objectManager->get($resourceName);
       }
    

diff --git src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php deleted file mode 100644 index 5ee7d1e..0000000 --- src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/InventoryData.php +++ /dev/null @@ -1,134 +0,0 @@ -<?php -/**

    • DISCLAIMER
    • Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
    • versions in the future.
    • @category Smile
    • @package Smile\ElasticsuiteCatalog
    • @copyright 2020 Smile
    • @license Open Software License ("OSL") v. 3.0
  • */ -namespace Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource;

-use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer; -use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\InventoryDataInterface; -use Magento\Framework\App\ResourceConnection; -use Magento\Framework\EntityManager\MetadataPool; -use Magento\Store\Model\StoreManagerInterface; -use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; -use Magento\InventorySalesApi\Api\StockResolverInterface; -use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; -use Magento\InventoryIndexer\Indexer\IndexStructure;

-/**

    • Multi Source Inventory Catalog Inventory Data source resource model
    • @category Smile
    • @package Smile\ElasticsuiteCatalog
  • */ -class InventoryData extends Indexer implements InventoryDataInterface -{

  • /**

  • * @var StockResolverInterface
    
  • */
    
  • private $stockResolver;

  • /**

  • * @var StockIndexTableNameResolverInterface
    
  • */
    
  • private $stockIndexTableProvider;

  • /**

  • * @var int[]
    
  • */
    
  • private $stockIdByWebsite = [];

  • /**

  • * InventoryData constructor.
    
  • *
    
  • * @param ResourceConnection                   $resource                Database adapter.
    
  • * @param StoreManagerInterface                $storeManager            Store manager.
    
  • * @param MetadataPool                         $metadataPool            Metadata Pool
    
  • * @param StockResolverInterface               $stockResolver           Stock resolver.
    
  • * @param StockIndexTableNameResolverInterface $stockIndexTableProvider Stock index table provider.
    
  • */
    
  • public function __construct(

  •    ResourceConnection $resource,
    
  •    StoreManagerInterface $storeManager,
    
  •    MetadataPool $metadataPool,
    
  •    StockResolverInterface $stockResolver,
    
  •    StockIndexTableNameResolverInterface $stockIndexTableProvider
    
  • ) {

  •    $this->stockResolver = $stockResolver;
    
  •    $this->stockIndexTableProvider = $stockIndexTableProvider;
    
  •    parent::__construct($resource, $storeManager, $metadataPool);
    
  • }

  • /**

  • * Load inventory data for a list of product ids and a given store.
    
  • * Expected rows structure : ['product_id', 'stock_status', 'qty'].
    
  • *
    
  • * @param integer $storeId    Store id.
    
  • * @param array   $productIds Product ids list.
    
  • *
    
  • * @return array
    
  • */
    
  • public function loadInventoryData(storeId,storeId, productIds)

  • {

  •    $websiteId = $this->getWebsiteId($storeId);
    
  •    $stockId   = $this->getStockId($websiteId);
    
  •    $tableName = $this->stockIndexTableProvider->execute($stockId);
    
  •    $select = $this->getConnection()->select()
    
  •        ->from(['product' => $this->getTable('catalog_product_entity')], [])
    
  •        ->join(
    
  •            ['stock_index' => $tableName],
    
  •            'product.sku = stock_index.' . IndexStructure::SKU,
    
  •            [
    
  •                'product_id'    => 'product.entity_id',
    
  •                'stock_status'  => 'stock_index.' . IndexStructure::IS_SALABLE,
    
  •                'qty'           => 'stock_index.' . IndexStructure::QUANTITY,
    
  •            ]
    
  •        )
    
  •        ->where('product.entity_id IN (?)', $productIds)
    
  •        ->group('product.entity_id');
    
  •    return $this->getConnection()->fetchAll($select);
    
  • }

  • /**

  • * Retrieve stock_id by website
    
  • *
    
  • * @param int $websiteId The website Id
    
  • *
    
  • * @return int
    
  • */
    
  • private function getStockId($websiteId)

  • {

  •    if (!isset($this->stockIdByWebsite[$websiteId])) {
    
  •        $websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
    
  •        $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
    
  •        $stockId = (int) $stock->getStockId();
    
  •        $this->stockIdByWebsite[$websiteId] = $stockId;
    
  •    }
    
  •    return $this->stockIdByWebsite[$websiteId];
    
  • }

  • /**

  • * Retrieve Website Id by Store Id
    
  • *
    
  • * @param int $storeId The store id
    
  • *
    
  • * @return int
    
  • */
    
  • private function getWebsiteId($storeId)

  • {

  •    return $this->storeManager->getStore($storeId)->getWebsiteId();
    
  • } -} diff --git src/module-elasticsuite-catalog/etc/di.xml src/module-elasticsuite-catalog/etc/di.xml index 8121b1d..67d6ff0 100644 --- src/module-elasticsuite-catalog/etc/di.xml +++ src/module-elasticsuite-catalog/etc/di.xml @@ -275,9 +275,6 @@

  • <preference for="Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\InventoryDataInterface"

  •            type="Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\InventoryData" />