0

Following are the code show Premium or Non-Premium along with product name using observer,

<?php
namespace Gta\CustomerMsgPdp\Observer\Product;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Data implements ObserverInterface
    {
        /**
         * Below is the method that will fire whenever the event runs!
         *
         * @param Observer $observer
         */
        public function execute(Observer $observer)
        {
            $product = $observer->getProduct();
            $originalName = $product->getName();
            $price = $product->getPrice();

            if($price >= '50')
            {
                $modifiedName = $originalName . ' - Premium Products';
            }
            elseif($price <= '50')
            {
                $modifiedName = $originalName . ' - Non Premium Products';
            }

            // $modifiedName = $originalName . ' - Modified by Magento 2 Events and Observers';
            $product->setName($modifiedName);
        }
    }

I want to add another observer to check if customer logged-in or not if customer logged-in then only show wheater it is premium or non-premium products.

1 Answer 1

1

You can use below code to check Customer is Logged in Or Not ?

Using Object Manager :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
}

In Controller :

$this->_objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
}

If it's helpful please accept it.Thanks

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.