0

I want to change the price in the Catalog View Page, I use a price attribute with the name rrp, and I need when customers are logged to display this price attribute otherwise display the normal price. I do this in Category Page like this:

                <?php
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerSession = $objectManager->get('Magento\Customer\Model\Session');
                if($customerSession->isLoggedIn()) {
                ?>
                    <?= /* @noEscape */ $block->getProductPrice($_product) ?>
                <?php } else { ?>
                    <div class="price-box">
                        <span class="regular-price" id="product-price-<?php $_product->getId() ?>">
                            <span class="price">
                                <?php $pricingHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class); ?>
                                <?php echo $pricingHelper->currency($_product->getData('rrp'), true, false); ?>
                            </span> 
                        </span> 
                    </div>
                <?php } ?>

But how I can do the same for Product View?

Thank you

4
  • What price should be when product will be added to cart ? Is this for display purpose only ? Feb 27, 2020 at 13:07
  • @AnilSuthar in the cart must be the correct price, for example if the customer is logged then in the Cart will be the rrp price, otherwise the normal price.
    – Robert
    Feb 27, 2020 at 13:09
  • Is that code done to change price based on logged in and guest customers ? From the code you posted will just show the price but wont change it upon adding to cart Feb 27, 2020 at 13:10
  • @AnilSuthar sorry but I don't understand very well, but for product list the code work fine, I need to do the same in the Product View
    – Robert
    Feb 27, 2020 at 13:11

1 Answer 1

1

You should create a plugin that changes the price after product loaded.

1 > Create di.xml file :

<config>
    <type name="Magento\Catalog\Model\Product">
        <plugin name="change_product_price" type="VENDOR_NAME\MODULE_NAME\Plugin\Product" sortOrder="1"/>
    </type>
</config>

2 > Change the product price :

<?php

namespace VENDOR_NAME\MODULE_NAME\Plugin;

class Product
{
    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        // Create your logic here to change product price and return it
        return $result + 100;
    }
}

3 > Flush Cache and Test

For more check How to change product price


Your logic should be (Just for demo. Not Tested and don't use ObjectManager directly, use dependency instead.) :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    return $subject->getData('rrp');
}else{
    return $result;
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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