0

hello, I want to get custom attributes name as hsn_code from cart products,

I have added one product to the cart,

Now I try to get this cart products custom attribute on the same cart page,

But when I code the product name appears but not the custom attribute, I don't know why.

Visible on Catalog Pages on Storefront is Yes,

Used in Product Listing is Yes

please help me with this.

my code.

Data.php

protected $_checkoutSession;
protected $_cart;


$this->_cart = $cart;
$this->_checkoutSession = $checkoutSession; 

public function getCart()
    {       
        return $this->_cart;
    }
    
    public function getCheckoutSession()
    {
        return $this->_checkoutSession;
    }

newtax.php

if($Cartinfo = $this->helperData->getCheckoutSession()->getQuote()){
                $items = $Cartinfo->getAllItems();
                foreach($items as $item) {
                    $name = $item->getName();
                    $hsncode = $item->getData('hsn_code');
                    //$hsncode = $item->getHsnCode();
                    echo $name;
                    echo $hsncode;                
                }
            }

Product name is displayed but hsn_code was not displayed.

enter image description here

enter image description here

1 Answer 1

0

Object method

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
echo $product->getHsnCode();
  

Factory Method

<?php
namespace Test\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;  


  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader
        
    ) {

       
        $this->_productloader = $_productloader;
        parent::__construct($context);
    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }
    public function execute($id)
    {
        $product=$this->getLoadProduct($item->getProductId());
        $hsnCode = $product->getHsnCode(); 
    }


}    

In Magento 2.1

<?php
    namespace Test\Module\Block;
    use Magento\Catalog\Api\ProductRepositoryInterface;
    class Product extends \Magento\Framework\View\Element\Template
     {
          protected $productRepository; 
          protected $_storeManager; 

          public function __construct(
            \Magento\Framework\App\Action\Context $context,
            ProductRepositoryInterface $productRepository
          ) {
              parent::__construct($context);

              $this->productRepository = $productRepository;
          }
          public function getProduct()
          {

              $productId=1;
              return $product = $this->productRepository->getById($productId);
          }
      }

Reference

4
  • when i fetch this value $GstHsnCode = $this->helperData->execute(); echo $GstHsnCode;
    – rocky9310
    Commented Dec 21, 2021 at 7:22
  • ArgumentCountError: Too few arguments to function Ncode\GSTtax\Helper\Data::execute(), 0 passed in /var/www/html/m243new/app/code/Ncode/GSTtax/Model/Quote/Total/Fee.php on line 74 and exactly 1 expected in /var/www/html/m243new/app/code/Ncode/GSTtax/Helper/Data.php:153
    – rocky9310
    Commented Dec 21, 2021 at 7:26
  • please dont add code in execute function of your helper. remove it Commented Dec 21, 2021 at 8:16
  • 1
    now add $product=$this->helperData->getLoadProduct($item->getProductId()); echo $hsnCode = $product->getHsnCode(); Commented Dec 21, 2021 at 8:17

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.