19
19
namespace Magento \Catalog \Pricing \Price ;
20
20
21
21
use Magento \Catalog \Api \Data \ProductInterface ;
22
- use Magento \Catalog \Model \Category ;
23
- use Magento \Catalog \Model \Product ;
24
22
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
25
23
use Magento \Framework \App \ResourceConnection ;
26
- use Magento \Framework \Cache \FrontendInterface ;
27
24
use Magento \Framework \EntityManager \MetadataPool ;
25
+ use Magento \Framework \Session \SessionManagerInterface ;
28
26
29
27
class SpecialPriceBulkResolver implements SpecialPriceBulkResolverInterface
30
28
{
@@ -39,31 +37,23 @@ class SpecialPriceBulkResolver implements SpecialPriceBulkResolverInterface
39
37
private MetadataPool $ metadataPool ;
40
38
41
39
/**
42
- * @var FrontendInterface
40
+ * @var SessionManagerInterface
43
41
*/
44
- private FrontendInterface $ cache ;
45
-
46
- /**
47
- * @var int
48
- */
49
- private int $ cacheLifeTime ;
42
+ private SessionManagerInterface $ customerSession ;
50
43
51
44
/**
52
45
* @param ResourceConnection $resource
53
46
* @param MetadataPool $metadataPool
54
- * @param FrontendInterface $cache
55
- * @param int $cacheLifeTime
47
+ * @param SessionManagerInterface $customerSession
56
48
*/
57
49
public function __construct (
58
50
ResourceConnection $ resource ,
59
51
MetadataPool $ metadataPool ,
60
- FrontendInterface $ cache ,
61
- int $ cacheLifeTime = self ::DEFAULT_CACHE_LIFE_TIME
52
+ SessionManagerInterface $ customerSession
62
53
) {
63
54
$ this ->resource = $ resource ;
64
55
$ this ->metadataPool = $ metadataPool ;
65
- $ this ->cache = $ cache ;
66
- $ this ->cacheLifeTime = $ cacheLifeTime ;
56
+ $ this ->customerSession = $ customerSession ;
67
57
}
68
58
69
59
/**
@@ -79,108 +69,48 @@ public function generateSpecialPriceMap(int $storeId, ?AbstractCollection $produ
79
69
if (!$ productCollection ) {
80
70
return [];
81
71
}
82
- //$cacheKey = $this->getCacheKey($storeId, $productCollection);
83
- //$cachedData = $this->getCachedData($cacheKey);
84
- if (true || $ cachedData === null ) {
85
- $ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
86
- $ connection = $ this ->resource ->getConnection ();
87
- $ select = $ connection ->select ()
88
- ->from (
89
- ['e ' => $ this ->resource ->getTableName ('catalog_product_entity ' )]
90
- )
91
- ->joinLeft (
92
- ['link ' => $ this ->resource ->getTableName ('catalog_product_super_link ' )],
93
- 'link.parent_id = e. ' . $ metadata ->getLinkField ()
94
- )
95
- ->joinLeft (
96
- ['product_website ' => $ this ->resource ->getTableName ('catalog_product_website ' )],
97
- 'product_website.product_id = link.product_id '
98
- )
99
- ->joinLeft (
100
- ['price ' => $ this ->resource ->getTableName ('catalog_product_index_price ' )],
101
- 'price.entity_id = COALESCE(link.product_id, e.entity_id) AND price.website_id = ' . $ storeId .
102
- ' AND price.customer_group_id = 0 '
103
- )
104
- ->where ('e.entity_id IN ( ' . implode (', ' , $ productCollection ->getAllIds ()) . ') ' )
105
- ->columns (
106
- [
107
- 'link.product_id ' ,
108
- '(price.final_price < price.price) AS hasSpecialPrice ' ,
109
- 'e. ' . $ metadata ->getLinkField () . ' AS identifier ' ,
110
- 'e.entity_id '
111
- ]
112
- );
113
- $ data = $ connection ->fetchAll ($ select );
114
- $ map = [];
115
- foreach ($ data as $ specialPriceInfo ) {
116
- if (!isset ($ map [$ specialPriceInfo ['entity_id ' ]])) {
117
- $ map [$ specialPriceInfo ['entity_id ' ]] = (bool ) $ specialPriceInfo ['hasSpecialPrice ' ];
118
- } else {
119
- if ($ specialPriceInfo ['hasSpecialPrice ' ] > $ map [$ specialPriceInfo ['entity_id ' ]]) {
120
- $ map [$ specialPriceInfo ['entity_id ' ]] = true ;
121
- }
122
- }
123
72
73
+ $ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
74
+ $ connection = $ this ->resource ->getConnection ();
75
+ $ select = $ connection ->select ()
76
+ ->from (
77
+ ['e ' => $ this ->resource ->getTableName ('catalog_product_entity ' )]
78
+ )
79
+ ->joinLeft (
80
+ ['link ' => $ this ->resource ->getTableName ('catalog_product_super_link ' )],
81
+ 'link.parent_id = e. ' . $ metadata ->getLinkField ()
82
+ )
83
+ ->joinLeft (
84
+ ['product_website ' => $ this ->resource ->getTableName ('catalog_product_website ' )],
85
+ 'product_website.product_id = link.product_id '
86
+ )
87
+ ->joinLeft (
88
+ ['price ' => $ this ->resource ->getTableName ('catalog_product_index_price ' )],
89
+ 'price.entity_id = COALESCE(link.product_id, e.entity_id) AND price.website_id = ' . $ storeId .
90
+ ' AND price.customer_group_id = ' . $ this ->customerSession ->getCustomerGroupId ()
91
+ )
92
+ ->where ('e.entity_id IN ( ' . implode (', ' , $ productCollection ->getAllIds ()) . ') ' )
93
+ ->columns (
94
+ [
95
+ 'link.product_id ' ,
96
+ '(price.final_price < price.price) AS hasSpecialPrice ' ,
97
+ 'e. ' . $ metadata ->getLinkField () . ' AS identifier ' ,
98
+ 'e.entity_id '
99
+ ]
100
+ );
101
+ $ data = $ connection ->fetchAll ($ select );
102
+ $ map = [];
103
+ foreach ($ data as $ specialPriceInfo ) {
104
+ if (!isset ($ map [$ specialPriceInfo ['entity_id ' ]])) {
105
+ $ map [$ specialPriceInfo ['entity_id ' ]] = (bool ) $ specialPriceInfo ['hasSpecialPrice ' ];
106
+ } else {
107
+ if ($ specialPriceInfo ['hasSpecialPrice ' ] > $ map [$ specialPriceInfo ['entity_id ' ]]) {
108
+ $ map [$ specialPriceInfo ['entity_id ' ]] = true ;
109
+ }
124
110
}
125
- //$this->saveCachedData($cacheKey, $map, array_column($data, 'identifier'));
126
-
127
- return $ map ;
128
- }
129
-
130
- return $ cachedData ;
131
- }
132
-
133
- /**
134
- * Generate cache key
135
- *
136
- * @param int $storeId
137
- * @param AbstractCollection $productCollection
138
- * @return string
139
- */
140
- private function getCacheKey (int $ storeId , AbstractCollection $ productCollection ): string
141
- {
142
- $ keyParts = $ productCollection ->getAllIds ();
143
- $ keyParts [] = 'store_id_ ' . $ storeId ;
144
111
145
- return hash ('sha256 ' , implode ('_ ' , $ keyParts ));
146
- }
147
-
148
- /**
149
- * Retrieve potential cached data
150
- *
151
- * @param string $cacheKey
152
- * @return array|null
153
- */
154
- private function getCachedData (string $ cacheKey ): ?array
155
- {
156
- $ data = $ this ->cache ->load ($ cacheKey );
157
- if (!$ data ) {
158
- return null ;
159
- }
160
-
161
- return json_decode ($ data , true );
162
- }
163
-
164
- /**
165
- * Store data in cache
166
- *
167
- * @param string $cacheKey
168
- * @param array $data
169
- * @param array $productTags
170
- * @return bool
171
- */
172
- private function saveCachedData (string $ cacheKey , array $ data , array $ productTags ): bool
173
- {
174
- $ tags = [
175
- Category::CACHE_TAG ,
176
- Product::CACHE_TAG ,
177
- 'price '
178
- ];
179
- $ productTags = array_unique ($ productTags );
180
- foreach ($ productTags as $ tag ) {
181
- $ tags [] = Product::CACHE_TAG . '_ ' . $ tag ;
182
112
}
183
113
184
- return $ this -> cache -> save ( json_encode ( $ data ), $ cacheKey , $ tags , $ this -> cacheLifeTime ) ;
114
+ return $ map ;
185
115
}
186
116
}
0 commit comments