0

I create an observer to intercept when a product is added to cart. The event that i used is "salesrule_validator_process".

The problem is that i create an eav attribute, but i can't get the value of that attribute related with the observable product. Here is my code:

public function execute( Observer $observer ) {
  $item_attribute = $observer->getItem()->getProduct()->getResource()->getAttribute('discount_elegible')
       ->getData();
}

Which retrieve something like these:

[attribute_id] => 139 [entity_type_id] => 4 [attribute_code] => club_discount_elegible [attribute_model] => Magento\Catalog\Model\ResourceModel\Eav\Attribute [backend_model] => Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend [backend_type] => int

And continue...

0
$item_attribute = $observer->getItem()->getProduct()->getData('discount_elegible')

UPDATE:

based on discussion with OP.

try that:

...
private $productRepository; 
...
public function __construct(
...
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
...
) {
   ...
   $this->productRepository = $productRepository;
   ...
}
public function execute(\Magento\Framework\Event\Observer $observer ) {
    $product = $this->productRepository->getById($observer->getItem()->getProduct()->getId());
    $attr = $product->getData('discount_elegible');
}
  • Hi Konstantin, i already tried that. But i tried it again. Now i have a log line regardin $item_attribute in my debug.log. And it says: [2017-03-29 19:59:49] main.DEBUG: {"is_exception":false} [] – Franco Mar 29 '17 at 20:05
  • see my updated answer. – Konstantin Gerasimov Mar 29 '17 at 20:13
  • you are very welcome sir – Konstantin Gerasimov Mar 29 '17 at 20:26

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.