Skip to content

Commit f190d89

Browse files
committed
multiwebsite - url fix
1 parent 2bce825 commit f190d89

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

Observer/Predispatch.php

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,65 @@ public function execute(Observer $observer)
5151
if (!$simpleProduct || $simpleProduct->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) {
5252
return;
5353
}
54+
try {
55+
$childId = $simpleProductId;
56+
$parentId = $this->getParentId($childId);
57+
if($childId != $parentId) {
58+
$configProduct =$this->_productRepository->getById($parentId);
59+
$configType = $configProduct->getTypeInstance();
60+
$attributes = $configType->getConfigurableAttributesAsArray($configProduct);
61+
62+
$options = [];
63+
foreach ($attributes as $attribute) {
64+
$id = $attribute['attribute_id'];
65+
$value = $simpleProduct->getData($attribute['attribute_code']);
66+
$options[$id] = $value;
67+
}
5468

55-
$configProductIds = $this->_productTypeConfigurable->getParentIdsByChild($simpleProductId);
56-
57-
foreach ($configProductIds as $configProductId) {
58-
try {
59-
$currentWebsiteId = $this->_storeManager->getStore()->getWebsiteId();
60-
$websiteIds = $this->_productRepository->getById($configProductId)->getWebsiteIds();
61-
if(in_array($currentWebsiteId, $websiteIds)){
62-
$configProduct = $this->_productRepository->getById($configProductId);
63-
} else {
64-
return;
69+
// Pass on any query parameters to the configurable product's URL.
70+
$query = $request->getQuery();
71+
if (is_object($query)) {
72+
$query = $query->toArray();
6573
}
66-
} catch (NoSuchEntityException $e) {
67-
continue;
68-
}
74+
$query = $query ? '?' . http_build_query($query) : '';
6975

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

73-
$options = [];
74-
foreach ($attributes as $attribute) {
75-
$id = $attribute['attribute_id'];
76-
$value = $simpleProduct->getData($attribute['attribute_code']);
77-
$options[$id] = $value;
79+
$configProductUrl = $configProduct->getUrlModel()->getUrl($configProduct) . $query . $hash;
80+
$this->_redirect->setRedirect($configProductUrl, 301);
81+
} else {
82+
return;
7883
}
79-
80-
// Pass on any query parameters to the configurable product's URL.
81-
$query = $request->getQuery();
82-
if (is_object($query)) {
83-
$query = $query->toArray();
84-
}
85-
$query = $query ? '?' . http_build_query($query) : '';
86-
87-
// Generate hash for selected product options.
88-
$hash = $options ? '#' . http_build_query($options) : '';
89-
90-
$configProductUrl = $configProduct->getUrlModel()
91-
->getUrl($configProduct) . $query . $hash;
92-
$this->_redirect->setRedirect($configProductUrl, 301);
84+
85+
} catch (NoSuchEntityException $noSuchEntityException) {
86+
return;
9387
}
9488
}
9589

9690
public function isModuleEnabled() {
9791
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
9892
return $this->scopeConfig->getValue('techyouknowredirectsimpleproducts/general/enable', $storeScope);
9993
}
94+
95+
public function getParentId($childId) {
96+
$currentWebsiteId = $this->_storeManager->getStore()->getWebsiteId();
97+
$parentProduct = $this->_productTypeConfigurable->getParentIdsByChild($childId);
98+
if(count($parentProduct) > 0){
99+
foreach ($parentProduct as $parentProductId) {
100+
if(isset($parentProductId)){
101+
$websiteIds = $this->_productRepository->getById($parentProductId)->getWebsiteIds();
102+
if(in_array($currentWebsiteId, $websiteIds)){
103+
return $parentProductId;
104+
}
105+
}
106+
}
107+
} else {
108+
if(isset($parentIds[0])){
109+
return $parentIds[0];
110+
}
111+
}
112+
113+
return $childId;
114+
}
100115
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"php": "7.0.2|7.0.4|~7.0.6|^7.1.0|^8.0"
66
},
77
"type": "magento2-module",
8-
"version": "1.0.1",
8+
"version": "1.0.2",
99
"license": [
1010
"OSL-3.0",
1111
"AFL-3.0"

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Techyouknow_RedirectSimpleProducts" setup_version="1.0.1" />
3+
<module name="Techyouknow_RedirectSimpleProducts" setup_version="1.0.2" />
44
</config>

0 commit comments

Comments
 (0)