0

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.

0

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>
  • 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. – Anshu Mishra Feb 2 '17 at 6:00
  • Check updated answer. – Sohel Rana Feb 2 '17 at 6:18
  • Instead of redirecting, can I just show the parent product url on the child product on product list page. – Anshu Mishra Feb 2 '17 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 '17 at 7:53
  • Can you explain how to overwrite the templates and where to add the script in more detail? – Will Cousin Aug 11 '18 at 11:51

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.