0

I am trying to fetch product attribute on cart page but it is giving me a null value below is my code

path:-V\M\Plugin;

class GetAllRates{
    protected $_logger;
    protected $cart;
    private $productRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    )
    {
        $this->_logger = $logger;
        $this->cart = $cart;
        $this->productRepository = $productRepository;
    }

    public function afterGetAllRates($subject, $result)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        
        foreach ($result as $key => $rate) {
            $price = $rate->getCost();
           // $logger->info("cost".json_encode($price)); //log print

            $carrier = $rate->getCarrier();
            $price2 = $rate->getPrice();
           // $logger->info("price".json_encode($price2)); //log print
            
            //changing price fedexseconddaydelivery
            if($carrier == 'fedexseconddaydelivery'){
                if($rate->getCost()==$rate->getPrice()){
                    $shippingCost= 550 + $price;
                    $rate->setPrice($shippingCost);
                    
                }
            }
     
            if ($rate->getIsDisabled()) {
                unset($result[$key]);
            }   
        }
        
        $cart = $this->cart->getQuote()->getAllItems();
        foreach($cart as $item) {
            echo 'ID: '.$item->getProductId().'<br />';
            echo 'Sku: '.$item->getSku().'<br />';
            echo 'Price: '.$item->getFixedShippingCost().'<br />';

            $myVar = $item->getSku();
            $logger->info("sku".json_encode($myVar));
            $myVar2 = $item->getProductId();
            $logger->info("productid".json_encode($myVar2));
            $fixedShippingCost = $item->getFixedShippingCost();
            $logger->info("fixedshippingcost".json_encode($fixedShippingCost));
        }


        return $result;
    }
}

2021-09-14T10:43:43+00:00 INFO (6): sku"test1" 2021-09-14T10:43:43+00:00 INFO (6): productid"8089" 2021-09-14T10:43:43+00:00 INFO (6): fixedshippingcostnull

1

You need to load product by id

private $quoteItemFactory;
private $itemResourceModel;
public function __construct(
  .....
  \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
  \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel
  ......
) {
   ....
   $this->quoteItemFactory = $quoteItemFactory;
   $this->itemResourceModel = $itemResourceModel;
   ...
}

and in one of the methods you can do this:

$itemId = $item->getProductId();
$quoteItem = $this->quoteItemFactory->create();
$this->itemResourceModel->load($quoteItem, $itemId);
8
  • $itemId or product id; can you be more specific
    – Pramod
    Sep 14 at 11:20
  • updated answer. Sep 14 at 12:49
  • small update you missed a collon while creating object please update it
    – Pramod
    Sep 14 at 14:06
  • may i use it something like this :0----- $itemId = $item->getProductId(); $quoteItem = $this->quoteItemFactory->create(); $MyVar = $this->itemResourceModel->load($quoteItem, $itemId); $productTest = $MyVar->getName(); // i am trying to get product name here
    – Pramod
    Sep 14 at 14:08
  • yes you can use like that Sep 15 at 4:14

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.