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}()