4

I am trying to add additional attributes to the collection that is created when getting a grouped product's associated simple products:

# Magento\GroupedProduct\Model\Product\Type\Grouped

public function getAssociatedProducts($product)
{
    if (!$product->hasData($this->_keyAssociatedProducts)) {
        $associatedProducts = [];

        $this->setSaleableStatus($product);

        $collection = $this->getAssociatedProductCollection(
            $product
        )->addAttributeToSelect(
            ['name', 'price',  'special_price', 'special_from_date', 'special_to_date']
        )->addFilterByRequiredOptions()->setPositionOrder()->addStoreFilter(
            $this->getStoreFilter($product)
        )->addAttributeToFilter(
            'status',
            ['in' => $this->getStatusFilters($product)]
        );

        foreach ($collection as $item) {
            $associatedProducts[] = $item;
        }

        $product->setData($this->_keyAssociatedProducts, $associatedProducts);
    }
    return $product->getData($this->_keyAssociatedProducts);
}

The attributes I would like to add are image-related ('image', 'small_image', 'thumbnail').

I put together a plugin that will redo the collection, but I'm getting an error that I'm passing the wrong type:

Uncaught TypeError: Argument 1 passed to Vendor\\Swatches\\Plugin\\GroupedSwatches::afterGetAssociatedProducts() must be an instance of Magento\\Catalog\\Model\\Product, instance of Magento\\GroupedProduct\\Model\\Product\\Type\\Grouped\\Interceptor given

Here is my (very inefficient) plugin method:

public function afterGetAssociatedProducts(\Magento\Catalog\Model\Product $product, $result)
{
    $associatedProducts = array();
    $collection = $product->getAssociatedProductCollection(
        $product
    )->addAttributeToSelect(
        ['name', 'price', 'special_price', 'special_from_date', 'special_to_date', 'image', 'small_image', 'thumbnail']
    )->addFilterByRequiredOptions()->setPositionOrder()->addStoreFilter(
        $this->getStoreFilter($product)
    )->addAttributeToFilter(
        'status',
        ['in' => $this->getStatusFilters($product)]
    );

    foreach ($collection as $item) {
        $associatedProducts[] = $item;
    }

    $product->setData($this->_keyAssociatedProducts, $associatedProducts);

    return $result;
}

Is this even the right approach for this?

7

For adding additional attributes for child in grouped product, add code like this to a Plugin/Model/Product/Type/GroupedPlugin.php file in your extension (this example assumes your extension is VendorName_ModuleName)

namespace VendorName\ModuleName\Model\Product\Type;

use \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection;
use \Magento\GroupedProduct\Model\Product\Type\Grouped as Grouped;


class Grouped
{
    public function afterGetAssociatedProductCollection(Grouped $subject, Collection $result)
    {
        $result->addAttributeToSelect('attr1');
        $result->addAttributeToSelect('attr2');

        return $result;
    }
}

And add this code to an etc/frontend/di.xml file in your module:

<?xml version="1.0"?>
<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="1" name="moduleNamePluginGrouped" type="VendorName\ModuleName\Plugin\Model\Product\Type\GroupedPlugin"/>
    </type>
</config>
  • Hello! would you be able to lookup or remember what you used in di.xml for this plugin? I'm trying to make it work for me but as soon as I enable the plugin I get blank where the attribute information should be on the page, with no error. Similar to what magento.stackexchange.com/questions/232332/… they solved it by duplicating some of the Grouped core code but there must be a better way? I suspect the type name= in di is where I am going wrong – iphigenie Aug 17 '18 at 8:11
  • Hi Erik I've returned to this one again - it works. Though I had an odd collision of Grouped so changed the function line to public function afterGetAssociatedProductCollection(\Magento\GroupedProduct\Model\Product\Type\Grouped $subject, Collection $result) – iphigenie Jan 27 '20 at 22:01

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.