0

I've created a multiselect custom eav attribute based on a flat table, so I used a dataSource, for a product everything is working fine, API (SOAP & REST), display on the product page etc but I'd like to get the data inside a collection for the page_list.

Right now it's what i'm doing on the product page but if I have 500 products I have to do 500 requests to get the eav attributes data on the page_list.

    $product = $this->productRepository->getById($id);
        $attr = $product->getCustomAttribute("Flags");
        $flagContent = [];
        if ($attr && $attrValues = $attr->getValue()) {
            $flag = $this->flagsCollectionFactory->create();
            $flagContentObject = $flag->addFieldToFilter("flags_id", explode(",", $attrValues))->toArray();
            if ($flagContentObject["items"]) {
                $flagContent = $flagContentObject["items"];
            }
        }
        return $flagContent;

Is there any way to load this "Flags" EAV attribute when I load the product collection so I don't have to do a request for every loaded products on the page_list ?

Thanks

0

You can create {MODULE_NAME}/etc/catalog_attributes.xml with such content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="catalog_product">
        <attribute name="MY_PRODUCT_ATTRIBUTE"/>
    </group>
</config>

Please see the following file for more info:

/vendor/magento/module-catalog/etc/catalog_attributes.xml

  • I already have that in my "catalog_attributes.xml" but the only data loaded are the ids in a string like : "1,2,3" and what I want is to get the data of those flags_ids inside my collection producsts – Shot Jun 6 '18 at 15:00
  • try using $product->getAttributeText('flag_ids'); – Vincent Hornikx Jun 6 '18 at 20:08
0

You can do it by following these steps, in your module you need to add catalog_attribute in etc/catalog_attributes.xml by this attribute will be loaded by default.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="catalog_product">
        <attribute name="attribute_code"/>
    </group>
</config>
  • see my comment on the answer above – Shot Jun 6 '18 at 15:00

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.