1

How to get Product.

I am adding my code here...

<?php
/**
 * Webkul Hello CustomPrice Observer
 *
 * @category    Webkul
 * @package     Webkul_Hello
 * @author      Webkul Software Private Limited
 *
 */
namespace Deep\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class CustomPrice implements ObserverInterface
{

 public function execute(\Magento\Framework\Event\Observer $observer) {
        $item = $observer->getEvent()->getData('quote_item');
        $price1 = $item->getPrice();
        $product = $item->getProduct();


         if (null !== $product->getCustomAttribute('deep_sample_price')) {
             $price = 5; //set your price here
             $item->setCustomPrice($price);
             $item->setOriginalCustomPrice($price);
             $item->getProduct()->setIsSuperMode(true);

        } 
        else if(null == $product->getCustomAttribute('deep_sample_price')){
            echo $product->getCustomAttribute('deep_sample_price')->getValue();


        }
    }

}

4
  • Please explain more actually want to you want. At where you want, which type of product you want get etc. – Rohan Hapani Oct 30 '18 at 5:06
  • Can u give some more details – surbhi agr Oct 30 '18 at 5:06
  • Where do you want to get product? – surbhi agr Oct 30 '18 at 5:09
  • what problem are you getting there ? – Shashank Kumrawat Oct 30 '18 at 5:26
1

You can get all quote items like this:

$quoteItems = $this->_checkoutSession->getQuote()->getAllVisibleItems(); 

And you can get product id from quote item like this :

$item->getProductId();

After that load that product by id and do your stuff

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