3

I want to display all the associated simple products on the category- pages and link them to the their parent product.

How this can be achieved?

One simple product is associated with only one parent.

2 Answers 2

3

Here I use configurable product as an example. You can pluginize Product View controller.

Try following way:

SR/StackExchange/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Controller\Product\View">
        <plugin name="sr_product_controller_view" type="SR\StackExchange\Plugin\Catalog\Controller\Product\View" sortOrder="1"/>
    </type>
</config>

If you redirect to Parent Product use following code:

SR/StackExchange/Plugin/Catalog/Controller/Product/View.php

namespace SR\StackExchange\Plugin\Catalog\Controller\Product;

class View
{
    /**
     * @var \Magento\Framework\App\Response\Http
     */
    protected $http;

    /**
     * @var \Magento\Catalog\Helper\Product
     */
    protected $productHelper;

    /**
     * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
     */
    protected $configurable;

    /**
     * @param \Magento\Framework\App\Response\Http $http
     * @param \Magento\Catalog\Helper\Product $productHelper
     * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $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;
    }

    /**
     * Product view action
     *
     * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
     */
    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();
    }
}

[Update]

Instead of redirecting, can I just show the parent product url.

SR/StackExchange/Plugin/Catalog/Controller/Product/View.php

namespace SR\StackExchange\Plugin\Catalog\Controller\Product;

class View
{
    /**
     * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
     */
    protected $configurable;

    /**
     * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
     */
    public function __construct(
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
    ) {
        $this->configurable = $configurable;
    }

    /**
     * Product view action
     *
     * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
     */
    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) {
            $subject->getRequest()->setParam('id', $parentId);
        }

        return $proceed();
    }
}

Overwrite following template

Magento_Swatches::product/view/renderer.phtml Magento_ConfigurableProduct::product/view/type/options/configurable.phtml

Add following code

<script>
    history.pushState({}, '', '<?php echo $block->getProduct()->getProductUrl();?>');
</script>
8
  • The code is showing parent product page but url is of the child product, I want the redirect i.e url should be of parent product only. Feb 2, 2017 at 6:00
  • Check updated answer.
    – Sohel Rana
    Feb 2, 2017 at 6:18
  • Instead of redirecting, can I just show the parent product url on the child product on product list page. Feb 2, 2017 at 7:53
  • @AnshuMishra if you assign parent product url in product list page, then system will be slow. Check updated answer, may be it will help you.
    – Sohel Rana
    Feb 3, 2017 at 7:53
  • Can you explain how to overwrite the templates and where to add the script in more detail? Aug 11, 2018 at 11:51
0

You can create a Plugin (Interceptor) for Magento\Catalog\Model\Product::getProductUrl() in order to link simple products with configurable parent product. For this, the new URL of simple products should look like 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.


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.