Magento – How to redirect simple products to configurable product URL with options preselected in Magento 2.1

configurable-productmagento-2.1redirectsimple-product

How can I redirect an SEO URL Key of a Simple product of which the visibility is set to "Not Individually Visible" to the parent Configurable Product SEO URL Key?

There are two important conditions that need to be met:

  • the options on the Configurable Product page should be correctly preselected to display the attributes of the Simple product.

  • the price for the Single Product also needs to be displayed correctly.

Example

Configurable Product P1 has two attributes.
Each attribute has two options:

Attribute A1 has options O1 and O2

Attribute A2 has options O3 and O4

So Configurable Product P1 has 4 Configurations represented by 4 Simple Products with each a different price:

P2: O1 and O3 -> EUR 1

P3: O1 and O4 -> EUR 2

P4: O2 and O3 -> EUR 3

P5: O2 and O4 -> EUR 4

The problem that needs to be solved is that for each Configuration the parent Configurable Product URL Key is sent to Google Merchant Shopping as an XML feed.

But when Google is verifying the prices of the Configurations on the parent Configurable Product URL page the prices won't match for P3, P4, and P5 as by default the lowest price is shown by Magento. And this in turn will result in the following warning in Google Merchant:

"Insufficient match of microdata price information". In the above
example 75% of the data isn't matching.

I already took a look at How redirect simple products of Configurable product but it is referring to an event which to my understanding does not exist in Magento 2: controller_action_predispatch_catalog_product_view.

I also tried the suggestions at Magento2 Event Observer Redirect Simple Product to Configurable Product but it seems that the Plugin can only work if the product visibility is not set to "Not Individually Visible".

So I suspect that the canShow method defined in Magento/Catalog/Helper/Product.php is called before the Magento/Catalog/Controller/Product/View is executed but I have a hard time figuring out where this happens.

How does Magento determine that an URL key is representing a product?

And where are the product id, category id, and options parameters derived from the URL key to be passed on to the execute method in Magento/Catalog/Controller/Product/View.php?

Any help would be much appreciated.

Best Answer

You can use a plugin to achieve that. In your module's etc/di.xml file write the plugin declaration

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product\Url">
        <plugin name="My_Extension_Product_Redirect" type="Vendor\Module\Plugins\Url" />
    </type>
</config>

And then in the Vendor/Module/Plugins/Url.php file you can use a before plugin like so

<?php

namespace Vendor\Module\Plugins;
use Magento\Catalog\Model\ProductRepository;
use \Magento\ConfigurableProduct\Model\Product\Type\Configurable;

class Url{

    protected $_configurableProductInstance;
    protected $_productRepository;

    function __construct(
        Configurable $configurable,
        ProductRepository $productRepository,
    )
    {
        $this->_configurableProductInstance = $configurable;
        $this->_productRepository = $productRepository;
    }

    public function beforegetProductUrl(\Magento\Catalog\Model\Product\Url $subject, \Magento\Catalog\Model\Product $product, $useSid)
    {
        $parentId = $this->_configurableProductInstance->getParentIdsByChild($product->getId());
        if(count($parentId)){
            return [$this->_productRepository->getById($parentId[0]), $useSid];
        }
        return null;
    }
}
Related Topic