0

Currently after 2.3.1 magento upgrade I start to use elastic search 6. On my magento application i have multiple websites and for some reason, except the default website the price filter was not showing at all. I start to debug this issue and i found a solution.

I will post my solution, because maybe in the future someone will have exact same issue.

0
<preference for="Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\Price"
            type="Vendor\MOdule\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\Price"/>

and Price.php 

class Price extends \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\Price
{
    /**
     * @var CustomerSession
     */
    private $customerSession;

    /**
     * @var StoreManager
     */
    private $storeManager;

    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $_request;

    /**
     * Price constructor.
     * @param CustomerSession|null $customerSession
     * @param StoreManager|null $storeManager
     * @param \Magento\Framework\App\RequestInterface $request
     */
    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        CustomerSession $customerSession = null,
        StoreManager $storeManager = null
    ) {
        $this->_request = $request;
        $this->storeManager = $storeManager ?: ObjectManager::getInstance()
            ->get(StoreManager::class);
        $this->customerSession = $customerSession ?: ObjectManager::getInstance()
            ->get(CustomerSession::class);
    }

    /**
     * @inheritdoc
     */
    public function getFieldName(AttributeAdapter $attribute, $context = []): ?string
    {
        if ($attribute->getAttributeCode() === 'price') {
            $path = $this->_request->getPathInfo();
            $customerGroupId = !empty($context['customerGroupId'])
                ? $context['customerGroupId']
                : $this->customerSession->getCustomerGroupId();
            $websiteId = !empty($context['websiteId'])
                ? $context['websiteId']
                : $this->storeManager->getStore()->getWebsiteId();

            if ($path) {
                $storeId = $this->storeManager->getStore()->getStoreId();
                return 'price_' . $customerGroupId . '_' . $storeId;
            } else {
                return 'price_' . $customerGroupId . '_' . $websiteId;
            }
        }
        return null;
    }
}

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.