I've created a few product attributes with these settings:
- Scope: Store View
- Add to Column Options: Yes
- Visible on Catalog Pages on Storefront: Yes
- Used in Product Listing: Yes
- Used for sorting in Product Listing: Yes
In my grouped product, I have a value:
[![enter image description here][1]][1]
But, in my custom /design/frontend/Vendor/Theme/Magento_GroupedProduct/templates/product/view/type/grouped.phtml
file, I've got this:
<?php
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/trey.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
foreach ($_item->getAttributes() as $attr)
{
$logger->info(
$attr->getAttributeCode(). ': '. $attr->getFrontend()->getValue($_item)
);
}
?>
Which produces these (skimmed) results in the log file:
2019-12-03T10:23:01+00:00 INFO (6): entity_id: 228
2019-12-03T10:23:02+00:00 INFO (6): type_id: simple
2019-12-03T10:23:02+00:00 INFO (6): attribute_set_id: 4
2019-12-03T10:23:02+00:00 INFO (6): status: Enabled
2019-12-03T10:23:02+00:00 INFO (6): name: Bulb 501 w5w 12v
2019-12-03T10:23:02+00:00 INFO (6): sku: BULB-501-SIN001
2019-12-03T10:23:02+00:00 INFO (6): price: 1.1900
2019-12-03T10:23:02+00:00 INFO (6): ref_code:
2019-12-03T10:23:02+00:00 INFO (6): image_ref:
2019-12-03T10:23:02+00:00 INFO (6): required:
As you can see, ref_code
, image_ref
and required
is empty, but I'm not sure why as the values are there in the database. I've tried re-indexing fully and clearing cache via rm -rf /var/cache/*
The methods listed in:
- Magento 2: How to display custom attribute in catalog
- Magento 2: Get custom attribute value of product
didn't work.
I didn't try the Object Manager method as I don't wish to use it as I know it's not the right way to go.
Edit:
snappy of my attribute:
Edit 2:
I created a module as advised, but couldn't get it 100%. This is the code I implemented:
|-app
|-----code
|---------Vendor
|-------------Module
|-----------------etc
|---------------------frontend
|-------------------------di.xml
|---------------------module.xml
|-----------------Plugin
|---------------------Model
|-------------------------Product
|-----------------------------Type
|---------------------------------Grouped.php
|-----------------registration.php
di.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\GroupedProduct\Model\Product\Type\Grouped">
<plugin name="addAttributes" type="Vendor\Module\Model\Plugin\Product\Type\Grouped" />
</type>
</config>
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="0.0.1">
<sequence>
<module name="Magento_Catalog" />
<module name="Magento_GroupedProduct" />
</sequence>
</module>
</config>
Grouped.php
namespace Vendor\Module\Model\Plugin\Product\Type;
class Grouped
{
/**
* @param $subject
* @param $result
*
* @return mixed
*/
public function afterGetAssociatedProductCollection($subject, $result)
{
$result->addAttributeToSelect('*');
return $result;
}
}
registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Best Answer
Create
di.xml
file here in your custom moduleContent for this file is..
Now you need to create Model file here in your custom module..
Content for this file is..
Hope this will help you!