-
-
Save Dan0sz/8253ddc626d054b7eca2225712de8cd7 to your computer and use it in GitHub Desktop.
Redirect Simple Products to their Configurable Parent with Attributes Pre-selected in Magento 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | |
<event name="controller_action_predispatch_catalog_product_view"> | |
<observer name="daanvdb_redirectsimple_products_observer_predispatch" instance="DaanvdB\RedirectSimpleProducts\Observer\Predispatch"/> | |
</event> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | |
<module name="DaanvdB_RedirectSimpleProducts" setup_version="1.0.0" /> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DaanvdB\RedirectSimpleProducts\Observer; | |
use Magento\Framework\Event\Observer; | |
use Magento\Framework\Event\ObserverInterface; | |
class Predispatch implements ObserverInterface { | |
protected $_redirect; | |
protected $_productTypeConfigurable; | |
protected $_productRepository; | |
protected $_storeManager; | |
public function __construct ( | |
\Magento\Framework\App\Response\Http $redirect, | |
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $productTypeConfigurable, | |
\Magento\Catalog\Model\ProductRepository $productRepository, | |
\Magento\Store\Model\StoreManagerInterface $storeManager | |
) { | |
$this->_redirect = $redirect; | |
$this->_productTypeConfigurable = $productTypeConfigurable; | |
$this->_productRepository = $productRepository; | |
$this->_storeManager = $storeManager; | |
} | |
public function execute(Observer $observer) | |
{ | |
$pathInfo = $observer->getEvent()->getRequest()->getPathInfo(); | |
/** If it's not a product view we don't need to do anything. */ | |
if (strpos($pathInfo, 'product') === false) { | |
return; | |
} | |
$request = $observer->getEvent()->getRequest(); | |
$simpleProductId = $request->getParam('id'); | |
if (!$simpleProductId) { | |
return; | |
} | |
$simpleProduct = $this->_productRepository->getById($simpleProductId, false, $this->_storeManager->getStore()->getId()); | |
if (!$simpleProduct || $simpleProduct->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) { | |
return; | |
} | |
$configProductId = $this->_productTypeConfigurable->getParentIdsByChild($simpleProductId); | |
if (isset($configProductId[0])) { | |
$configProduct = $this->_productRepository->getById($configProductId[0], false, $this->_storeManager->getStore()->getId()); | |
$configType = $configProduct->getTypeInstance(); | |
$attributes = $configType->getConfigurableAttributesAsArray($configProduct); | |
$options = []; | |
foreach ($attributes as $attribute) { | |
$id = $attribute['attribute_id']; | |
$value = $simpleProduct->getData($attribute['attribute_code']); | |
$options[$id] = $value; | |
} | |
$options = http_build_query($options); | |
$hash = $options ? '#' . $options : ''; | |
$configProductUrl = $configProduct->getUrlModel() | |
->getUrl($configProduct) . $hash; | |
$this->_redirect->setRedirect($configProductUrl, 301); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
\Magento\Framework\Component\ComponentRegistrar::register( | |
\Magento\Framework\Component\ComponentRegistrar::MODULE, | |
'DaanvdB_RedirectSimpleProducts', | |
__DIR__ | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment