Вопрос

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

Это было полезно?

Решение

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;

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top