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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityHere 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>
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.