4

How can I get the custom attribute value of an associated product in a grouped product in .phtml file in Magento 2.3.5?

2 Answers 2

4

getAssociatedProducts does not get you full product objects but product links as a custom collection with standard attributes

Two options:

  1. You add the attributes you want to the getAssociatedProducts collection, by extending them. This needs a custom module but they are then available everywhere associated products are used

  2. You fully load a product object for the child in your template before calling the other attributes

Which option you pick depends on context - if you are in a single product context with its children and you only need this in one block, then #2 is easier - but for large lists of products there's a performance impact.

If you are going to want these extra attributes many places, product page, product listing, related products, emails etc, then #1 is your friend

Extending collection

This makes sense if you'll want to show the same attributes in a lot of places - for example anything you want added to a product list view in widgets, modules, catalog search results etc.

Assuming you have a created a custom module MyModule_FixGrouped

in etc/frontend/di.xml

<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 sortOrder="10" name="FixModelProductTypeGrouped" type="MyModule\FixGrouped\Plugin\Model\Product\Type\Grouped" disabled="false" />
    </type>
</config>

and MyModule\FixGrouped\Plugin\Model\Product\Type\Grouped

<?php

namespace MyModule\FixGrouped\Plugin\Model\Product\Type;
// was \Magento\GroupedProduct\Model\Product\Type\Grouped ;
use \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection;


class Grouped
{
/**
 * Retrieve collection of associated products
 *
 * @param \Magento\GroupedProduct\Model\Product\Type\Grouped $subject
 * @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
 */
public function afterGetAssociatedProductCollection(\Magento\GroupedProduct\Model\Product\Type\Grouped $subject, Collection $result)
{
    $result->addAttributeToSelect('whatever');
    $result->addAttributeToSelect('whatever');
    $result->addAttributeToSelect('whatever');
    return $result;
}

}

?>

Fully Loading

This is just added to class or even block code.

Might make sense if it is not used all the time but occasionally so adding the attributes to every product collection across the site would be a waste.

Example: a special catalog view that is not used all the time. I have one used to create a PDF once a month for example

in the constructor
@param \Magento\Catalog\Model\ProductFactory $productFactory;
$this->_productFactory = $productFactory;


in the method  
$children = $parentproduct->getTypeInstance()->getAssociatedProducts($parentproduct);
foreach ($children as $childlink) {
     $child = $this->_productFactory->create()->load($childlink->getId());
...
}

After that every product attribute and every product method is available for the $child object

3
  • Should it be MyModule\FixGrouped\Plugin\Model\Product\Type\Grouped.php or without the .php extension?
    – KaMZaTa
    Jul 6, 2020 at 16:30
  • Thanks, the first option seems to work well (It should be MyModule\FixGrouped\Plugin\Model\Product\Type\Grouped.php). The second one I didn't understand how to implement it.
    – KaMZaTa
    Jul 6, 2020 at 16:43
  • since i just found this again as I had forgotten what to do - I might as well clarify. Associated products are not full products, only shells. The first solution adds missing data to the "associated products" collection. The second solution creates a full product object for each associated product, which means everything a product normally has is available. It will be slower but have everything
    – iphigenie
    Nov 11, 2021 at 18:59
0

I presume that you're in the product/view/type/grouped.phtml template, or some PHTML file included from that; this is basically how it goes:

<?php $_associatedProducts = $block->getAssociatedProducts();
$_hasAssociatedProducts = count($_associatedProducts) > 0; ?>

<!-- The lines above go near the top, and the lines below go where you need to refer to the associated products. -->

<?php if ($_hasAssociatedProducts) {
          foreach ($_associatedProducts as $_item) { ?>
<!-- Now you can get a custom attribute of each $_item in the same way as the main product. -->
<?php }
} ?>

Among the ways to get the value of an attribute (let's say it's called shirt_size and it's selected via drop-down) are $_item->getShirtSize(); or $_item->getAttribute('shirt_size'); for the numeric value of the index (which often isn't useful), or $_item->getAttributeText('shirt_size'); for the textual value.

4
  • Thanks but it doesn't show anything. It does work for "standard" attributes but not for my custom one $_item->getAttribute('format'); or $_item->getAttributeText('format');
    – KaMZaTa
    Jun 10, 2020 at 22:58
  • $_item->getAttribute('format'); or $_item->getAttributeText('format'); work for the main grouped product but not for associated (simple and downloadable in my case).
    – KaMZaTa
    Jun 11, 2020 at 0:04
  • Did you make sure to try it inside the foreach loop? Also, did you try the variant $_item->getFormat();? Jun 22, 2020 at 19:21
  • Yes, does it work for you? I have no problem to get attribute label and value for the main product in this way: $attribute = $_product->getResource()->getAttribute('brand'); Label: $attribute->getStoreLabel() and Value: $attr_value = $attribute ->getFrontend()->getValue($_product); Furthermore, I have no problem to get any system attributes of the associate products.
    – KaMZaTa
    Jun 22, 2020 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.