2

My store has many GroupProduct and each one has many virtual products. This virtual products have custom attributes and some are required and some aren't.

[MyModule]/Setup/InstallData.php

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'iesep_sigla',
        [
            'type' => 'text',
            'group' => 'IESE',
            'backend' => '',
            'frontend' => '',
            'label' => 'Sigla IESE',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => true,
            'filterable' => false,
            'filterable_in_search' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => false,
            'used_for_promo_rules' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'iesep_idioma',
        [
            'type' => 'varchar',
            'group' => 'IESE',
            'frontend' => '',
            'label' => 'Idioma',
            'input' => 'select',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => true,
            'filterable_in_search' => true,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => false,
            'unique' => false,
            'apply_to' => ''
        ]
    );

The goal is to save some of the product attributes in sales_order_item table.

product attribute -> quote_item -> sales_order_item

When the GroupProduct is added to the cart the observer assigned to the event sales_quote_item_set_product is being executed for each item.

Then when I try to get a product attribute that is not required with the method $observer->getProduct()->getData('iesep_idioma') I get a null value. If I try to get an attribute that is required I do get the value.

**

How do I load the attributes that I need without loading the whole object?

**

[MyModule]/etc/events.php

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_item_set_product">
    <observer
            name="product_point_quote"
            instance="Alfa9\CatalogPreview\Observer\salesQuoteItemSetCustomAttribute"/>
</event>
</config>

[MyModule]/Observer/salesQuoteItemSetCustomAttribute.php

<?php
namespace Alfa9\CatalogPreview\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;

class salesQuoteItemSetCustomAttribute implements ObserverInterface
{

protected $eavAttributeRepository;
private $_productRepo;

public function __construct(
    AttributeRepositoryInterface $eavAttributeRepository,
    \Magento\Catalog\Model\ProductRepository $productRepo
) {
    $this->eavAttributeRepository = $eavAttributeRepository;
    $this->_productRepo = $productRepo;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $iesep_idioma = $observer->getProduct()->getData('iesep_idioma'); //return null
    $iesep_sigla = $observer->getProduct()->getData('iesep_sigla'); //return value

    //More code here

}
}

enter image description here enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.