Skip to content

Commit

Permalink
multiwebsite - url fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amjadm61 committed Nov 5, 2022
1 parent 2bce825 commit f190d89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
83 changes: 49 additions & 34 deletions Observer/Predispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,65 @@ public function execute(Observer $observer)
if (!$simpleProduct || $simpleProduct->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) {
return;
}
try {
$childId = $simpleProductId;
$parentId = $this->getParentId($childId);
if($childId != $parentId) {
$configProduct =$this->_productRepository->getById($parentId);
$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;
}

$configProductIds = $this->_productTypeConfigurable->getParentIdsByChild($simpleProductId);

foreach ($configProductIds as $configProductId) {
try {
$currentWebsiteId = $this->_storeManager->getStore()->getWebsiteId();
$websiteIds = $this->_productRepository->getById($configProductId)->getWebsiteIds();
if(in_array($currentWebsiteId, $websiteIds)){
$configProduct = $this->_productRepository->getById($configProductId);
} else {
return;
// Pass on any query parameters to the configurable product's URL.
$query = $request->getQuery();
if (is_object($query)) {
$query = $query->toArray();
}
} catch (NoSuchEntityException $e) {
continue;
}
$query = $query ? '?' . http_build_query($query) : '';

$configType = $configProduct->getTypeInstance();
$attributes = $configType->getConfigurableAttributesAsArray($configProduct);
// Generate hash for selected product options.
$hash = $options ? '#' . http_build_query($options) : '';

$options = [];
foreach ($attributes as $attribute) {
$id = $attribute['attribute_id'];
$value = $simpleProduct->getData($attribute['attribute_code']);
$options[$id] = $value;
$configProductUrl = $configProduct->getUrlModel()->getUrl($configProduct) . $query . $hash;
$this->_redirect->setRedirect($configProductUrl, 301);
} else {
return;
}

// 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);

} catch (NoSuchEntityException $noSuchEntityException) {
return;
}
}

public function isModuleEnabled() {
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
return $this->scopeConfig->getValue('techyouknowredirectsimpleproducts/general/enable', $storeScope);
}

public function getParentId($childId) {
$currentWebsiteId = $this->_storeManager->getStore()->getWebsiteId();
$parentProduct = $this->_productTypeConfigurable->getParentIdsByChild($childId);
if(count($parentProduct) > 0){
foreach ($parentProduct as $parentProductId) {
if(isset($parentProductId)){
$websiteIds = $this->_productRepository->getById($parentProductId)->getWebsiteIds();
if(in_array($currentWebsiteId, $websiteIds)){
return $parentProductId;
}
}
}
} else {
if(isset($parentIds[0])){
return $parentIds[0];
}
}

return $childId;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"php": "7.0.2|7.0.4|~7.0.6|^7.1.0|^8.0"
},
"type": "magento2-module",
"version": "1.0.1",
"version": "1.0.2",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +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.1" />
<module name="Techyouknow_RedirectSimpleProducts" setup_version="1.0.2" />
</config>

0 comments on commit f190d89

Please sign in to comment.