0

I have this Extension StoreViewPricing, you can set product price in store views, but in Layered Navigation when I'm applying price filter it's not showing proper results as expected. Here's the screenshot.

https://prnt.sc/peto54

I'm trying to set the product price in store views but It's showing below error. I've made the plugin for Magento\Catalog\Model\Layer class.

Screenshot

di.xml

<type name="Magento\Catalog\Model\Layer">
        <plugin name="custom_product_model_layer" type="Vendor\Module\Plugin\Layer" />
    </type>

Layer.php

<?php

use Magento\Framework\App\RequestInterface;

class Layer
{
    /**
     * @param \Magento\Catalog\Model\Layer $subject,
     * @param \Closure $proceed
     */
    public function aroundGetProductCollection(
        \Magento\Catalog\Model\Layer $subject,
        \Closure $proceed
    ) {
        //Get Object Manager Instance
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $request = $objectManager->get('\Magento\Framework\App\Request\Http');
        $result = $proceed();

        //Price Filter
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productCollection= $objectManager->create('Magento\Catalog\Model\Product')->getCollection();            
        $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
        $storeId = $storeManager->getStore()->getId();

        $joinConditions = 'u.entity_id = e.entity_id';
        $productCollection->getSelect()->join(
            ['u' => $productCollection->getTable('custom_view_pricing')],
            $joinConditions,
            ['*']
        )->where('u.store_id = '.$storeId);
        try {
            foreach ($productCollection as $customProducts) {
                $data = $customProducts->getData();
                foreach ($result->getData() as $resultProducts) {
                    $productId = $resultProducts['entity_id'];
                    if ($data['entity_id'] == $productId) {
                        $price = $data['price'];
                        $productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
                        $product = $productFactory->create()->setStoreId($storeId)->load($productId);
                        $product->setData('price', $price);
                        $product->save();
                    }
                }
            }

        } catch (Exception $e) {
            $e->getMessage();
        }

        return $result;
    }
}

I've tried below code as well it's giving the same error as shown in the image.

$product->setPrice($price); 
$resultProducts['price'] = $price;
$product->setData('price', $price);

If anyone has a solution let me know.

0

Good day,
the easiest way is creating different stores for you different pricings. We did that on our own a while ago to have different pricings per country.

Example enter image description here

I dont quite get why you want to set the differnt price on a store-view level. The customer could just change the view and get another price that way. If you are attempting to do something with currency translations, it would be another thing. Mageplaza got a nice article about that: https://www.mageplaza.com/kb/how-to-configure-currency-magento-2.html

  • I want to show different product prices for different stores that's why I want to set the price. I've already created multiple stores and made currency setup. – Mohit Rane Oct 4 at 12:31

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.