Magento 2 Plugin Not Extending Model - Finally Solved It
-
17-01-2021 - |
Question
I'm trying to use a plugin from the answer on this question
Add additional select attributes to grouped product's child collection
I would go back and ask the person who answered but I don't have a high enough reputation score yet to add comments.
I don't use composer, so have gone the old route of app/code.
My plugin looks like this
<?php
namespace mynamespace\Plugingrouped\Plugin;
use \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection;
use \Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped;
class Grouped
{
public function afterGetAssociatedProductCollection(TypeGrouped $subject, Collection $result)
{
$result->addAttributeToSelect('my_attribute_weight');
$result->addAttributeToSelect('my_attribute_height');
return $result;
}
}
?>
I've added the plugin code as Grouped.php into a folder app/code/mynamespace/Plugingrouped/Plugin
My di.xml file in my module looks like this
<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">
<plugin name="Grouped" type="Mynamespace\Plugingrouped\Plugin\Grouped" />
</type>
And then I tried calling a custom attribute in
app/design/frontend/Mynamespace/base/Magento_GroupedProduct/templates/product/view/type/grouped.html
<strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
Weight : <?php echo $_item->getmy_attribute_height() ?>
To add custom attributes into the grouped product grid.
However, they don't render, I don't get an error, but I can't figure out how to make sure the plugin is working.
La solution
After many many days of banging my head on a wall, I've finally solved it.
I had two modules set up originally:
- Mynamespace\Grouped\
- Mynamespace\Plugingrouped
Nothing I did would make the plugin show up until I combined the two into module.
I now have the Model override and plugin in one module and it's working.
In case anyone else is having the same nightmare:
My di.xml looks like this
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\GroupedProduct\Model\Product\Type\Grouped" type="Mynamespace\Grouped\Model\Rewrite\Product\Type\Grouped" />
<type name="Mynamespace\Grouped\Model\Rewrite\Product\Type\Grouped">
<plugin name="Mynamespace_grouped_groupedproduct_model" type="Mynamespace\Grouped\Plugin\Grouped" />
</type>
</config>
Folder Structure looks like:
Mynamespace
->Grouped
->etc (di.xml & module.xml)
->Model->Rewrite->Product->Type->Grouped.php (copy of core)
->Plugin->Grouped.php (containing plugin code)
On the frontend I've added the attributes into overriden copy of
app/design/frontend/Mynamespace/base/Magento_GroupedProduct/templates/product/view/type/grouped.html
and hey presto it worked.
A big thank you to those who replied and tried to help and I hope this answers helps others.
Now all I need to do is get them to show up in the cart and throughout checkout
:-)
Autres conseils
Please try below
<type name="Magento\GroupedProduct\Model\Product\Type\Grouped">
<plugin name="Grouped" type="Mynamespace\Plugingrouped\Plugin\Grouped" />
</type>