File tree Expand file tree Collapse file tree 3 files changed +57
-20
lines changed Expand file tree Collapse file tree 3 files changed +57
-20
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Copyright 2025 Adobe
4+ * All Rights Reserved.
5+ */
6+
7+ namespace Magento \Catalog \Plugin \Model ;
8+
9+ use Magento \Catalog \Model \Config as Subject ;
10+ use Magento \Catalog \Model \Product ;
11+ use Magento \Eav \Model \Config as EavConfig ;
12+
13+ /**
14+ * Plugin to ensure special_price attribute is always loaded in product listings
15+ * even when used_in_product_listing = 0
16+ */
17+ class ConfigPlugin
18+ {
19+ private const SPECIAL_PRICE_ATTR_CODE = 'special_price ' ;
20+
21+ /**
22+ * @param EavConfig $eavConfig
23+ */
24+ public function __construct (
25+ private readonly EavConfig $ eavConfig
26+ ) {
27+ }
28+
29+ /**
30+ * Add special_price attribute to the list of attributes used in product listing
31+ * regardless of its used_in_product_listing setting
32+ *
33+ * @param Subject $subject
34+ * @param array $result
35+ * @return array
36+ */
37+ public function afterGetAttributesUsedInProductListing (Subject $ subject , array $ result ): array
38+ {
39+ // Check if special_price is already in the result
40+ if (!isset ($ result [self ::SPECIAL_PRICE_ATTR_CODE ])) {
41+ // Get the special_price attribute
42+ $ specialPriceAttribute = $ this ->eavConfig ->getAttribute (
43+ Product::ENTITY ,
44+ self ::SPECIAL_PRICE_ATTR_CODE
45+ );
46+
47+ if ($ specialPriceAttribute && $ specialPriceAttribute ->getId ()) {
48+ // Add it to the result
49+ $ result [self ::SPECIAL_PRICE_ATTR_CODE ] = $ specialPriceAttribute ;
50+ }
51+ }
52+ return $ result ;
53+ }
54+ }
Original file line number Diff line number Diff line change @@ -84,11 +84,6 @@ public function getSpecialPrice()
8484 {
8585 $ specialPrice = $ this ->product ->getSpecialPrice ();
8686
87- // If special_price is not loaded (null and attribute not loaded), load it from resource
88- if ($ specialPrice === null && $ this ->product ->getTypeId () === 'simple ' ) {
89- $ specialPrice = $ this ->getSpecialPriceFromResource ();
90- }
91-
9287 if ($ specialPrice !== null && $ specialPrice !== false && !$ this ->isPercentageDiscount ()) {
9388 $ specialPrice = $ this ->priceCurrency ->convertAndRound ($ specialPrice );
9489 }
@@ -137,19 +132,4 @@ public function isPercentageDiscount()
137132 return false ;
138133 }
139134
140- /**
141- * Get special price from resource
142- *
143- * @return float|null
144- */
145- private function getSpecialPriceFromResource (): ?float
146- {
147- $ resource = $ this ->product ->getResource ();
148- $ specialPrice = $ resource ->getAttributeRawValue (
149- $ this ->product ->getId (),
150- 'special_price ' ,
151- $ this ->product ->getStoreId ()
152- );
153- return !is_array ($ specialPrice ) ? $ specialPrice : null ;
154- }
155135}
Original file line number Diff line number Diff line change 124124 </argument >
125125 </arguments >
126126 </type >
127+ <type name =" Magento\Catalog\Model\Config" >
128+ <plugin name =" special_price_listing_plugin" type =" Magento\Catalog\Plugin\Model\ConfigPlugin" sortOrder =" 10" />
129+ </type >
127130</config >
You can’t perform that action at this time.
0 commit comments