0

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

  • Which one you have used? There are 2 options in the answer. – Sukumar Gorai Aug 22 '18 at 15:17
  • Tried both answers on both these posts. Neither worked exactly how I needed it. – Jeremy F. Aug 22 '18 at 15:19
  • Check my answer its working fine for me. – Sukumar Gorai Aug 22 '18 at 15:30
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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