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
29 changes: 23 additions & 6 deletions Model/Product/Indexer/Fulltext/Datasource/OfferData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

namespace Smile\Offer\Model\Product\Indexer\Fulltext\Datasource;

use Magento\Catalog\Model\Product as ProductModel;
use Magento\Framework\App\ObjectManager;
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface;
use Smile\Offer\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\OfferData as ResourceModel;
use Magento\Catalog\Model\Product\TypeFactory as ProductTypeFactory;
use Magento\Customer\Api\Data\GroupInterface as CustomerGroupInterface;
use Magento\Framework\Indexer\CacheContext;

/**
* Datasource used to append prices data to product during indexing.
Expand All @@ -29,18 +31,27 @@
class OfferData implements DatasourceInterface
{
/**
* @var \Smile\Offer\Model\ResourceModel\Product\Indexer\Fulltext\Datasource\OfferData
* @var ResourceModel
*/
private $resourceModel;

/**
* @var CacheContext
*/
private $cacheContext;

/**
* Constructor.
*
* @param ResourceModel $resourceModel Resource model
* @param ResourceModel $resourceModel Resource model
* @param CacheContext|null $cacheContext Indexer cache context
*/
public function __construct(ResourceModel $resourceModel)
{
public function __construct(
ResourceModel $resourceModel,
CacheContext $cacheContext = null
) {
$this->resourceModel = $resourceModel;
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
}

/**
Expand All @@ -50,9 +61,10 @@ public function __construct(ResourceModel $resourceModel)
*/
public function addData($storeId, array $indexData)
{
$entitiesIds = [];
$offerData = $this->resourceModel->loadOfferData(array_keys($indexData));

foreach ($offerData as $productId => $offerDataRow) {
foreach ($offerData as $offerDataRow) {
$productId = (int) $offerDataRow['product_id'];
$offerDataRow = $this->processOfferPrices($offerDataRow, $indexData[$productId]);

Expand All @@ -61,9 +73,14 @@ public function addData($storeId, array $indexData)
$offerDataRow['seller_id'] = (int) $offerDataRow['seller_id'];
$offerDataRow['is_available'] = (bool) ($offerDataRow['is_available'] ?? false);

$entitiesIds[] = $productId;
$indexData[$productId]['offer'][] = $offerDataRow;
}

if ($entitiesIds) {
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, $entitiesIds);
}

return $indexData;
}

Expand Down