diff --git a/Helper/Data.php b/Helper/Data.php index 08ac14c..7f7eb67 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -37,36 +37,26 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper /**@var \Magento\Catalog\Model\Product */ protected $_tempProduct = null; - /**@var \Magento\Eav\Model\Config */ - protected $_eavConfig; - - /**@var \Magento\Catalog\Model\ResourceModel\Product */ - protected $_resourceProduct; - - /**@var \Magento\Store\Model\StoreManagerInterface */ - protected $_storeManager; - + /**@var \Magento\Catalog\Api\ProductRepositoryInterface */ + protected $_productRepository; + public function __construct( \Magento\Framework\App\Helper\Context $context, \Mygento\Base\Model\Logger\LoggerFactory $loggerFactory, \Mygento\Base\Model\Logger\HandlerFactory $handlerFactory, \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Framework\HTTP\Client\Curl $curl, - \Magento\Eav\Model\Config $eavConfig, - \Magento\Catalog\Model\ResourceModel\Product $resourceProduct, - \Magento\Store\Model\StoreManagerInterface $storeManager + \Magento\Catalog\Model\ProductRepository $productRepository ) { parent::__construct($context); - $this->_loggerFactory = $loggerFactory; - $this->_handlerFactory = $handlerFactory; - $this->_encryptor = $encryptor; - $this->_curlClient = $curl; - $this->_eavConfig = $eavConfig; - $this->_resourceProduct = $resourceProduct; - $this->_storeManager = $storeManager; - + $this->_loggerFactory = $loggerFactory; + $this->_handlerFactory = $handlerFactory; + $this->_encryptor = $encryptor; + $this->_curlClient = $curl; + $this->_productRepository = $productRepository; + $this->_logger = $this->_loggerFactory->create(['name' => $this->_code]); - $handler = $this->_handlerFactory->create(['name' => $this->_code]); + $handler = $this->_handlerFactory->create(['name' => $this->_code]); $this->_logger->setHandlers([$handler]); } @@ -109,7 +99,12 @@ public function getConfig($configPath) \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } - + + public function getProduct($productId) + { + return $this->_productRepository->getById($productId); + } + /** * * @param type $url @@ -190,43 +185,28 @@ public function getCode() { return $this->_code; } - - public function getAttributeValue($param, $productId, $prefix = '') + + /**Fetch attribute code from $pathToParam and then get it from product + * @param $pathToParam config path like module/general/param + * @param $productId + * + * @return mixed attribute value + */ + public function getAttrValueByParam($pathToParam, $productId) { - $attributeCode = $this->getConfig($prefix . $param); - - //$this->addLog('attr for ' . $param . ' -> ' . $attributeCode); - - if ('0' != $attributeCode && 0 !== $attributeCode) { - $entityType = $this->_resourceProduct->getEntityType(); - $attribute = $this->_eavConfig->getAttribute($entityType, $attributeCode); - $attributeMode = $attribute->getFrontendInput(); - if ('select' == $attributeMode) { - //need to use product model - //TODO: Avoid ObjectManager - if (!$this->_tempProduct) { - $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); - $this->_tempProduct = $objectManager->get('Magento\Catalog\Model\Product') - ->load($productId); - } - $product = $this->_tempProduct; - $value = $product->getAttributeText($attributeCode); - } else { - //just raw DB data - $value = $this->_resourceProduct->getAttributeRawValue( - $productId, - $attributeCode, - $this->_storeManager->getStore() - ); - - if (is_array($value) && isset($value[$attributeCode])) { - $value = $value[$attributeCode]; - } - } - } else { - $value = $this->getConfig($prefix . $param . '_default'); + $attributeCode = $this->getConfig($pathToParam); + if ('0' == $attributeCode || 0 === $attributeCode) { + return $this->getConfig($pathToParam . '_default'); } - + + return $this->getAttributeValue($attributeCode, $productId); + } + + public function getAttributeValue($attributeCode, $productId) + { + $product = $this->getProduct($productId); + $value = $product->getAttributeText($attributeCode) ?: $product->getData($attributeCode); + return $value; } } diff --git a/Helper/Discount.php b/Helper/Discount.php index d7258a5..71e22fe 100644 --- a/Helper/Discount.php +++ b/Helper/Discount.php @@ -6,20 +6,15 @@ */ namespace Mygento\Base\Helper; +//TODO: FIgure out - do we need to extend this parent helper? + /** * Module helper */ class Discount extends \Mygento\Base\Helper\Data { - - /** @var \Magento\Catalog\Model\ResourceModel\Product */ - protected $_productResource; - - /** @var \Magento\Eav\Model\Config */ - protected $_eavConfig; - - /** @var \Magento\Store\Model\StoreManagerInterface */ - protected $_storeManager; + /**@var \Magento\Catalog\Model\ProductRepository */ + protected $_productRepository; /** * Constructor @@ -40,25 +35,20 @@ public function __construct( \Mygento\Base\Model\Logger\HandlerFactory $handlerFactory, \Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Framework\HTTP\Client\Curl $curl, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Catalog\Model\ResourceModel\Product $productResource, - \Magento\Eav\Model\Config $eavConfig - ) { - + \Magento\Catalog\Model\ProductRepository $productRepository + ) + { + parent::__construct( $context, $loggerFactory, $handlerFactory, $encryptor, $curl, - $eavConfig, - $productResource, - $storeManager + $productRepository ); - $this->_productResource = $productResource; - $this->_eavConfig = $eavConfig; - $this->_storeManager = $storeManager; + $this->_productRepository = $productRepository; } /** @@ -280,6 +270,7 @@ protected function addTaxValue($taxAttributeCode, $entity, $item) $store = $storeId ? $this->_storeManager->getStore($storeId) : $this->_storeManager->getStore(); + //TODO: Use parent helper here $taxValue = $this->_productResource->getAttributeRawValue( $item->getProductId(), $taxAttributeCode, diff --git a/composer.json b/composer.json index c1e4dc0..3524791 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "mygento/base", "type": "magento2-module", - "version": "2.0.8", + "version": "2.0.9", "license": "OSL-3.0", "homepage": "https://github.com/mygento/base", "description": "Mygento Base", diff --git a/etc/module.xml b/etc/module.xml index 9fa353b..f3b569a 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -8,5 +8,5 @@ --> - + \ No newline at end of file