Is their any way to override "Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper
" class in module ?
I want to override method in it.
private function convertToProductData(int $productId, array $indexData, int $storeId): array
I have tried Vendor\Elasticsearch\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper" type="Vendor\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper"/>
</config>
app\code\Vendor\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper
<?php
namespace Vendor\Elasticsearch\Model\Adapter\BatchDataMapper;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Elasticsearch\Model\Adapter\Document\Builder;
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
use Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface;
use Magento\Elasticsearch\Model\Adapter\FieldType\Date as DateFieldType;
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
class ProductDataMapper extends \Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper
{
/**
* @var AttributeOptionInterface[]
*/
private $attributeOptionsCache;
/**
* @var Builder
*/
private $builder;
/**
* @var FieldMapperInterface
*/
private $fieldMapper;
/**
* @var DateFieldType
*/
private $dateFieldType;
/**
* @var array
*/
private $excludedAttributes;
/**
* @var AdditionalFieldsProviderInterface
*/
private $additionalFieldsProvider;
/**
* @var DataProvider
*/
private $dataProvider;
/**
* List of attributes which will be skipped during mapping
*
* @var string[]
*/
private $defaultExcludedAttributes = [
'price',
'media_gallery',
'tier_price',
'quantity_and_stock_status',
'media_gallery',
'giftcard_amounts',
];
/**
* @var string[]
*/
private $attributesExcludedFromMerge = [
'status',
'visibility',
'tax_class_id',
];
/**
* @var string[]
*/
private $sortableAttributesValuesToImplode = [
'name',
];
/**
* @var string[]
*/
private $filterableAttributeTypes;
/**
* @var string[]
*/
private $sortableCaseSensitiveAttributes = [
'name',
];
/**
* @param Builder $builder
* @param FieldMapperInterface $fieldMapper
* @param DateFieldType $dateFieldType
* @param AdditionalFieldsProviderInterface $additionalFieldsProvider
* @param DataProvider $dataProvider
* @param array $excludedAttributes
* @param array $sortableAttributesValuesToImplode
* @param array $filterableAttributeTypes
* @param array $sortableCaseSensitiveAttributes
*/
public function __construct(
Builder $builder,
FieldMapperInterface $fieldMapper,
DateFieldType $dateFieldType,
AdditionalFieldsProviderInterface $additionalFieldsProvider,
DataProvider $dataProvider,
array $excludedAttributes = [],
array $sortableAttributesValuesToImplode = [],
array $filterableAttributeTypes = [],
array $sortableCaseSensitiveAttributes = []
) {
$this->builder = $builder;
$this->fieldMapper = $fieldMapper;
$this->dateFieldType = $dateFieldType;
$this->excludedAttributes = array_merge($this->defaultExcludedAttributes, $excludedAttributes);
$this->sortableAttributesValuesToImplode = array_merge(
$this->sortableAttributesValuesToImplode,
$sortableAttributesValuesToImplode
);
$this->additionalFieldsProvider = $additionalFieldsProvider;
$this->dataProvider = $dataProvider;
$this->attributeOptionsCache = [];
$this->filterableAttributeTypes = $filterableAttributeTypes;
$this->sortableCaseSensitiveAttributes = array_merge(
$this->sortableCaseSensitiveAttributes,
$sortableCaseSensitiveAttributes
);
}
/**
* Convert raw data retrieved from source tables to human-readable format.
*
* @param int $productId
* @param array $indexData
* @param int $storeId
* @return array
*/
private function convertToProductData(int $productId, array $indexData, int $storeId): array
{
echo'override working'; exit;
$productAttributes = [];
if (isset($indexData['options'])) {
// cover case with "options"
// see \Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider::prepareProductIndex
$productAttributes['options'] = $indexData['options'];
unset($indexData['options']);
}
foreach ($indexData as $attributeId => $attributeValues) {
$attribute = $this->dataProvider->getSearchableAttribute($attributeId);
if (in_array($attribute->getAttributeCode(), $this->excludedAttributes, true)) {
continue;
}
if (!\is_array($attributeValues)) {
$attributeValues = [$productId => $attributeValues];
}
$attributeValues = $this->prepareAttributeValues($productId, $attribute, $attributeValues, $storeId);
$productAttributes += $this->convertAttribute($attribute, $attributeValues, $storeId);
}
return $productAttributes;
}
}