1

I have set up a Magento website where simple products are shown in the category listings and the configurable products for these items are in a hidden category. I need to add some functionality so when a customer clicks on 1 of these simple products it redirects them to the associated configurable product.

I tried making an extension as found in this link but it didn't work: How to redirect simple products to configurable product URL with options preselected in Magento 2.1

Then I tried the suggestion on this link: Redirect from simple product to parent product

This worked but it caused a bug where the images and choices wouldn't load on the configurable product page until the customer refreshed.

Does anyone know how to do this properly? Optimally I want the customer to be rerouted directly to the configurable product with no errors or bugs. Maybe this can be done by changing the links in the category product list?

Website Specs:
- Magento 2.2.5
- PHP 7.0.30
- MYSQL 10.0.34-MariaDB

3
  • Which one you have used? There are 2 options in the answer. Aug 22, 2018 at 15:17
  • Tried both answers on both these posts. Neither worked exactly how I needed it.
    – Jeremy F.
    Aug 22, 2018 at 15:19
  • Check my answer its working fine for me. Aug 22, 2018 at 15:30

2 Answers 2

1

I have tried the following method and its working pretty good. You need to create plugin like below. Add the following code to your module's di.xml:

<type name="Magento\Catalog\Controller\Product\View">
    <plugin name="sk_product_controller_view" type="Vendor\Module\Plugin\Catalog\Controller\Product\View" sortOrder="1"/>
</type>

And the code for View.php under following path:

app/code/Vendor/Module/Plugin/Catalog/Controller/Product/View.php

<?php
namespace Vendor\Module\Plugin\Catalog\Controller\Product;

class View
{
    protected $http;

    protected $productHelper;

    protected $configurable;

    public function __construct(
        \Magento\Framework\App\Response\Http $http,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
    ) {
        $this->http = $http;
        $this->productHelper =$productHelper;
        $this->configurable = $configurable;
    }

    public function aroundExecute(
        \Magento\Catalog\Controller\Product\View $subject,
        \Closure $proceed
    )
    {
        $productId = (int) $subject->getRequest()->getParam('id');
        $parentIds = $this->configurable->getParentIdsByChild($productId);
        $parentId = array_shift($parentIds);

        if($parentId) {
            $categoryId = (int)$subject->getRequest()->getParam('category', false);
            $productId = (int)$parentId;

            $params = new \Magento\Framework\DataObject();
            $params->setCategoryId($categoryId);

            /** @var \Magento\Catalog\Helper\Product $product */
            $product = $this->productHelper->initProduct($productId, $subject, $params);;
            $this->http->setRedirect($product->getProductUrl(), 301);
        }
        return $proceed();
    }
}

Reference: Redirect from simple product to parent product

1
  • 1
    $this->http->setRedirect($product->getProductUrl(), 301); this line is not working
    – Ashna
    Oct 8, 2018 at 9:49
0

To preselect a simple product in a configurable product, the address of a simple product should look like this for example:

/mona-pullover-hoodlie.html#143=167&93=53

where

/mona-pullover-hoodlie.html - configurable product URL

143, 93 - attributes IDs

167, 53 - option IDs.

In order to change the URL of simple products, you can create a Plugin (Interceptor) for Magento\Catalog\Model\Product::getProductUrl(), where to generate the URL as in the example above.

Product selection using URL parameters is already implemented in Magento out of the box.

I made VCT Simple Product URL module on Magento Marketplace that solves this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.