0

I have grouped products "containing" simple products. I have had to make the simple products visible but while I am ok for them to appear in certain lists, I want the link to go to the parent page not to the (very incomplete) simple product page.

I started by creating a helper with a new URL function and overriding the various templates to use it , but there are a lot of templates and it will be a pain to maintain.

It seems to always be $product->getUrlModel()->getUrl($product, $additional) behind all the getProductUrl so Catalog\Model\Product\Url I should be able to create a "before" plugin for getUrl that swaps to the parent when it detects particular conditions.

But it seems my plugin is not happening - even when I just make it log a line then pass on things unchanged. So either I am missing an understanding or it is another class that cannot be modified?

In app/code/Mystuff/FixGrouped/etc/frontend/di.xml

<type name="Catalog\Model\Product\Url">
    <plugin sortOrder="1" name="FixGroupedPluginModelProductUrl" 
     type="Mystuff\FixGrouped\Plugin\Model\Product\Url" disabled="false" />
</type>

and in app/code/Mystuff/FixGrouped/Plugin/Model/Product/Url.php

namespace Mystuff\FixGrouped\Plugin\Model\Product;

use \Magento\Catalog\Model\Product\Url;

class Url 
{
       public function beforeGetUrl(\Magento\Catalog\Model\Product\Url $subject, \Magento\Catalog\Model\Product $product, array $params)
     {
          $logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
          $logger->info('Mystuff plugin beforeGetUrl active');
          // here it would swap for parent if require
          return [$product, $params];
    }

}

So it changes nothing but puts a line in the log. No lines ever appear in the log.

So what am I missing? - i've not written it right? - getUrl is not called in a context that works with plugin?

I couldnt find any beforeGetUrl on this class out there so it is not reassuring

1 Answer 1

1

The namespace that you have in your di.xml and in your class is slightly incorrect, especially with di.xml, I don't think Magento 2 can resolve that type.

  • Add Magento to start of type:

    <type name="Magento\Catalog\Model\Product\Url">

  • Remove \ from \Magento use (All use in Magento are like that);

    use Magento\Catalog\Model\Product\Url;

4
  • thanks - for all my staring I did not spot that. and i have other plugin entries in that file that should have made it obvious! Now to see what the collateral damage of messing with url is...
    – iphigenie
    Jun 3, 2020 at 8:22
  • It happens to all of us! ( I just wonder about the sortorder=1... if something else might not change your result later on, just consider that) Jun 3, 2020 at 8:28
  • 1
    I'd changed sort order up and down trying - at 10 it's solidly active now
    – iphigenie
    Jun 3, 2020 at 8:37
  • Great news, I sometimes forget sortOrder. Jun 3, 2020 at 8:50

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.