Skip to content

Instantly share code, notes, and snippets.

@Dan0sz Dan0sz/Predispatch.php Secret
Last active Feb 11, 2019

Embed
What would you like to do?
Redirect Simple Products to their Configurable Parent with Attributes Pre-selected in Magento 2
<?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>
<?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>
<?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);
}
}
}
<?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
You can’t perform that action at this time.