1

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;
    }
}
1
  • updated question added code. Jul 23, 2021 at 13:30

1 Answer 1

1
  1. create di.xml in to the following location

app/code/Vendor/Extension/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\Extension\Model\Adapter\BatchDataMapper\ProductDataMapper" />
</config>
  1. create ProductDataMapper.php file on this location.

app/code/Vendor/Extension/Model/Adapter/BatchDataMapper/ProductDataMapper.php

namespace Vendor\Extension\Model\Adapter\BatchDataMapper;

class ProductDataMapper extends \Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper
{
    public function __construct()
    {
        echo "Model Rewrite Working"; die();

    }
}
7
  • Thanks sir & how to override convertToProductData method in the same class ? Jul 23, 2021 at 13:21
  • if you are able to see "Model Rewrite Working" then copy paste vendor's method in your overrided file and add your custom code as per your need Jul 23, 2021 at 13:24
  • no need of any constructor & properties define above constructor ? i have added file content in question. Jul 23, 2021 at 13:25
  • simple you can first copt the code of that file here except the file namespace, then modify the code according to your requirement. Jul 23, 2021 at 13:27
  • I have done that, please have a look at the file code which i have added in question. but its not working. Jul 23, 2021 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.