-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fe366fe
Showing
8 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Techyouknow\RedirectSimpleProducts\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
class Predispatch implements ObserverInterface { | ||
|
||
protected $_redirect; | ||
protected $_productTypeConfigurable; | ||
protected $_productRepository; | ||
protected $_storeManager; | ||
protected $scopeConfig; | ||
|
||
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, | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
) { | ||
$this->_redirect = $redirect; | ||
$this->_productTypeConfigurable = $productTypeConfigurable; | ||
$this->_productRepository = $productRepository; | ||
$this->_storeManager = $storeManager; | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
$pathInfo = $observer->getEvent()->getRequest()->getPathInfo(); | ||
|
||
/** If the module is disabled, we don't need to do anything. */ | ||
if (!$this->isModuleEnabled()) { | ||
return; | ||
} | ||
|
||
/** 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; | ||
} | ||
|
||
$configProductIds = $this->_productTypeConfigurable->getParentIdsByChild($simpleProductId); | ||
|
||
foreach ($configProductIds as $configProductId) { | ||
try { | ||
$configProduct = $this->_productRepository->getById($configProductId, false, $this->_storeManager->getStore()->getId()); | ||
} catch (NoSuchEntityException $e) { | ||
continue; | ||
} | ||
|
||
$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; | ||
} | ||
|
||
// Pass on any query parameters to the configurable product's URL. | ||
$query = $request->getQuery(); | ||
if (is_object($query)) { | ||
$query = $query->toArray(); | ||
} | ||
$query = $query ? '?' . http_build_query($query) : ''; | ||
|
||
// Generate hash for selected product options. | ||
$hash = $options ? '#' . http_build_query($options) : ''; | ||
|
||
$configProductUrl = $configProduct->getUrlModel() | ||
->getUrl($configProduct) . $query . $hash; | ||
$this->_redirect->setRedirect($configProductUrl, 301); | ||
} | ||
} | ||
|
||
public function isModuleEnabled() { | ||
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; | ||
return $this->scopeConfig->getValue('techyouknowredirectsimpleproducts/general/enable', $storeScope); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
# Redirect Simple products to its parent configurable For Magento 2 | ||
Redirect simple products to its parent configurable product with pre-selected configurable options. | ||
|
||
This extension is configurable on the backend. | ||
|
||
![docs](https://i.imgur.com/uq58SGt.png) | ||
|
||
## Usage Instructions | ||
|
||
TechYouKnow RedirectSimpleProducts Extension - Installation steps | ||
|
||
INSTALL TechYouKnow RedirectSimpleProducts EXTENSION FROM ZIP FILE ON YOUR DEV INSTANCE. TEST THAT THE EXTENSION | ||
WAS INSTALLED CORRECTLY BEFORE SHIPPING THE CODE TO PRODUCTION | ||
|
||
### INSTALLATION | ||
|
||
#### Composer Installation | ||
* Go to your magento root path | ||
* Execute command `cd /var/www/Magento` or | ||
`cd /var/www/html/Magento` based on your server Centos or Ubuntu. | ||
* run composer command: `composer require techyouknow/module-redirect-simple-products` | ||
- To enable module execute `php bin/magento module:enable Techyouknow_RedirectSimpleProducts` | ||
- Execute `php bin/magento setup:upgrade` | ||
- Execute `php bin/magento setup:di:compile` | ||
- Optional `php bin/magento setup:static-content:deploy` | ||
- Execute `php bin/magento cache:clean` | ||
|
||
#### Manual Installation | ||
* extract files from an archive. | ||
* Execute command `cd /var/www/Magento/app/code` or | ||
`cd /var/www/html/Magento/app/code` based on your server Centos or Ubuntu. | ||
* Move files into Magento2 folder `app/code/Techyouknow/RedirectSimpleProducts`. If you downloaded zip file on github, you need to | ||
create directory `app/code/Techyouknow/RedirectSimpleProducts`. | ||
- To enable module execute `php bin/magento module:enable Techyouknow_RedirectSimpleProducts` | ||
- Execute `php bin/magento setup:upgrade` | ||
- Optional `php bin/magento setup:static-content:deploy` | ||
- Execute `php bin/magento setup:di:compile` | ||
- Execute `php bin/magento cache:clean` | ||
|
||
## Requirements | ||
|
||
Techyouknow RedirectSimpleProducts Extension For Magento2 requires | ||
* Magento version 2.0 and above | ||
* PHP 7.0 or greater |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "techyouknow/module-redirect-simple-products", | ||
"description": "Redirect simple products to its parent configurable product with pre-selected configurable options.", | ||
"require": { | ||
"php": "7.0.2|7.0.4|~7.0.6|^7.1.0|^8.0" | ||
}, | ||
"type": "magento2-module", | ||
"version": "1.0.0", | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "TechYouKnow", | ||
"role": "Owner" | ||
}, | ||
{ | ||
"name": "Amjad M", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Techyouknow\\RedirectSimpleProducts\\": "" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<tab id="techyouknow" translate="label" sortOrder="10"> | ||
<label>Techyouknow</label> | ||
</tab> | ||
<section id="techyouknowredirectsimpleproducts" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<class>separator-top</class> | ||
<label>Redirect Simple Products</label> | ||
<tab>techyouknow</tab> | ||
<resource>Techyouknow_RedirectSimpleProducts::redirectsimpleproducts_config</resource> | ||
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>General Configuration</label> | ||
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Module Enable</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> | ||
<default> | ||
<redirectsimpleproducts> | ||
<general> | ||
<enable>1</enable> | ||
</general> | ||
</redirectsimpleproducts> | ||
</default> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?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="techyouknow_redirectsimple_products_observer_predispatch" instance="Techyouknow\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?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="Techyouknow_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'Techyouknow_RedirectSimpleProducts', | ||
__DIR__ | ||
); |