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?