Zend certified PHP/Magento developer

Redirect Simple Products to their Configurable Parent with Attributes Pre-selected

Hey guys I am trying to Redirect Simple Products to their Configurable Parent with Attributes Pre-selected and I followed Daan’s guide. The Observer is as follows:

< ?php

namespace DaanvdBRedirectSimpleProductsObserver;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;

class Predispatch implements ObserverInterface {

    protected $_redirect;
    protected $_productTypeConfigurable;
    protected $_productRepository;
    protected $_storeManager;

    public function __construct (
        MagentoFrameworkAppResponseHttp $redirect,
        MagentoConfigurableProductModelResourceModelProductTypeConfigurable $productTypeConfigurable,
        MagentoCatalogModelProductRepository $productRepository,
        MagentoStoreModelStoreManagerInterface $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() != MagentoCatalogModelProductType::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);
        }
    }
}

Unfortunately, I think a recent Magento update may have broken the code to actually pre-select the attributes. The redirect still works correctly. There were a few other folks have a similar issue on his site. Is there a simple fix we can use to patch this feature? Thanks so much.