1

I have created one custom attribute on the admin product details page, the attribute name is hsn_code. now I want to get this attribute value in

Block/Adminhtml/Sales/Igst.php

I need in Igst.php file,

here is the code that I have tried but it's not working.

Block/Adminhtml/Sales/Igst.php

<?php
namespace Ncode\GSTtax\Block\Adminhtml\Sales;
class Igst extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
    //protected $collection
    /**
     * @var \Ncode\GSTtax\Helper\Data
     */
    protected $_dataHelper;
   
    /**
     * @var \Magento\Directory\Model\Currency
     */
    protected $_currency;
    protected $_order; 
    //protected $_source;    

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Ncode\GSTtax\Helper\Data $dataHelper,
        \Magento\Directory\Model\Currency $currency,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_dataHelper = $dataHelper;
        $this->_currency = $currency;        
    }

     /**
     * Retrieve current order model instance
     *
     * @return \Magento\Sales\Model\Order
     */
    public function getOrder()
    {
        return $this->getParentBlock()->getOrder();
    }

    /**
     * @return mixed
     */
    public function getSource()
    {
        return $this->getParentBlock()->getSource();
    }

    /**
     * @return string
     */
    public function getCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }

    public function getProductCollection()
    {
        $productCollection = $this->_productCollectionFactory->create();
        $productCollection->addAttributeToSelect('*');
        //$productCollection->setPageSize(3); 
        return $productCollection;
       
        /*foreach ($productCollection as $product){
             echo 'Name  =  '.$product->getName();            
        } */
    }

    /**
     *
     *
     * @return $this
     */
    public function initTotals() 
    {           
        $this->getParentBlock();
        $this->getOrder();
        $this->getSource();
        $this->getProductCollection();

        if(!$this->getSource()->getFee()) {
                return $this;
        }

        $productCollection = $this->getProductCollection();
        foreach ($productCollection as $productHsn) {
            //print_r($product->getHsnCode());            
            echo $productHsn->getHsnCode();
        }

        $state   = $this->getOrder()->getShippingAddress()->getRegion();
        $productionState = $this->_scopeConfig->getValue('GSTtax/GSTtax/gstregion', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

        if(!($state == $productionState && $productHsn)){
            $total = new \Magento\Framework\DataObject(
                [
                    'code' => 'igst',
                    'value' => $this->getSource()->getFee(),                                       
                    'label' => "IGST",
                ]
            );
            $this->getParentBlock()->addTotalBefore($total, 'grand_total');        
            return $this;
        }            
    } 
}

Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::getName() in /var/www/html/m243new/app/code/Test/Extension/Block/Adminhtml/Sales/Igst.php:98 Stack trace: #0 /var/www/html/m243new/vendor/magento/module-sales/Block/Order/Totals.php(64): Ncode\GSTtax\Block\Adminhtml\Sales\Igst->initTotals() #1 /var/www/html/m243new/vendor/magento/framework/View/Element/AbstractBlock.php(1113): Magento\Sales\Block\Order\Totals->_beforeToHtml() #2 /var/www/html/m243new/vendor/magento/framework/View/Element/AbstractBlock.php(1118): Magento\Framework\View\Element\AbstractBlock->Magento\Framework\View\Element{closure}()

4
  • try using below code: $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->setPageSize(3); // fetching only 3 products return $collection; Dec 16, 2021 at 13:19
  • i have try this code, but when i fetch value its give me error , $collection = $this->getProductCollection()->getName();
    – rocky9310
    Dec 16, 2021 at 13:27
  • Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::getName()
    – rocky9310
    Dec 16, 2021 at 13:27
  • $productCollection = $block->getProductCollection(); foreach ($productCollection as $product) { echo $product->getName(); } Dec 16, 2021 at 13:43

1 Answer 1

1

Try this code:

$_helper = $this->helper('Magento\Catalog\Helper\Output');
echo $_attributeValue = $block->getProduct()->getResource()->getAttribute('your_attribute')->getFrontend()->getValue($block->getProduct());

Your Answer

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

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