@@ -51,50 +51,65 @@ public function execute(Observer $observer)
51
51
if (!$ simpleProduct || $ simpleProduct ->getTypeId () != \Magento \Catalog \Model \Product \Type::TYPE_SIMPLE ) {
52
52
return ;
53
53
}
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
+ }
54
68
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 ();
65
73
}
66
- } catch (NoSuchEntityException $ e ) {
67
- continue ;
68
- }
74
+ $ query = $ query ? '? ' . http_build_query ($ query ) : '' ;
69
75
70
- $ configType = $ configProduct -> getTypeInstance ();
71
- $ attributes = $ configType -> getConfigurableAttributesAsArray ( $ configProduct ) ;
76
+ // Generate hash for selected product options.
77
+ $ hash = $ options ? ' # ' . http_build_query ( $ options ) : '' ;
72
78
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 ;
78
83
}
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 ;
93
87
}
94
88
}
95
89
96
90
public function isModuleEnabled () {
97
91
$ storeScope = \Magento \Store \Model \ScopeInterface::SCOPE_STORE ;
98
92
return $ this ->scopeConfig ->getValue ('techyouknowredirectsimpleproducts/general/enable ' , $ storeScope );
99
93
}
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
+ }
100
115
}
0 commit comments