Skip to content

Commit 633b493

Browse files
committed
AC-13594: Special price is not showing correctly for Configurable product's child product (simple product)
Fixed special price display when used_in_product_listing is disabled Added lazy loading for special price attributes and integration test coverage
1 parent e885088 commit 633b493

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

app/code/Magento/Catalog/Pricing/Price/SpecialPrice.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,20 @@ public function getValue()
8383
public function getSpecialPrice()
8484
{
8585
$specialPrice = $this->product->getSpecialPrice();
86+
87+
// If special_price is not loaded (null and attribute not loaded), load it from resource
88+
if ($specialPrice === null) {
89+
$resource = $this->product->getResource();
90+
$specialPrice = $resource->getAttributeRawValue(
91+
$this->product->getId(),
92+
'special_price',
93+
$this->product->getStoreId()
94+
);
95+
}
96+
8697
if ($specialPrice !== null && $specialPrice !== false && !$this->isPercentageDiscount()) {
8798
$specialPrice = $this->priceCurrency->convertAndRound($specialPrice);
99+
$this->product->setData('special_price', $specialPrice);
88100
}
89101
return $specialPrice;
90102
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Pricing\Price;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
14+
use Magento\Framework\Pricing\PriceInfo\Base as PriceInfo;
15+
use Magento\TestFramework\Fixture\DataFixture;
16+
use Magento\TestFramework\Fixture\DbIsolation;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Integration test for SpecialPrice pricing class
22+
*/
23+
#[
24+
DbIsolation(true)
25+
]
26+
class SpecialPriceTest extends TestCase
27+
{
28+
/**
29+
* @var \Magento\Framework\ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var CollectionFactory
35+
*/
36+
private $productCollectionFactory;
37+
38+
/**
39+
* Set up
40+
*/
41+
protected function setUp(): void
42+
{
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->productCollectionFactory = $this->objectManager->get(CollectionFactory::class);
45+
}
46+
47+
/**
48+
* Test that special price is loaded from database when attribute is not in product collection
49+
*
50+
* This tests the fix for the issue where special_price doesn't display when
51+
* the attribute has "Used in Product Listing = No" setting
52+
*
53+
* @return void
54+
*/
55+
#[
56+
DataFixture(
57+
ProductFixture::class,
58+
['sku' => 'simple-special-price', 'price' => 100, 'special_price' => 90]
59+
),
60+
]
61+
public function testGetSpecialPriceLoadsFromDatabaseWhenNotInCollection(): void
62+
{
63+
64+
// Load product in a collection WITHOUT special_price attribute
65+
/** @var Collection $collection */
66+
$collection = $this->productCollectionFactory->create();
67+
$collection->addFieldToFilter('sku', 'simple-special-price')
68+
->addAttributeToSelect(['name', 'price']) // Intentionally exclude special_price
69+
->load();
70+
71+
/** @var Product $product */
72+
$product = $collection->getFirstItem();
73+
74+
// Verify special_price is not loaded in the product data
75+
$this->assertFalse(
76+
$product->hasData('special_price'),
77+
'Special price should not be loaded in product collection'
78+
);
79+
80+
// Get the SpecialPrice pricing object
81+
/** @var PriceInfo $priceInfo */
82+
$priceInfo = $product->getPriceInfo();
83+
/** @var SpecialPrice $specialPriceObject */
84+
$specialPriceObject = $priceInfo->getPrice(SpecialPrice::PRICE_CODE);
85+
86+
// Call getSpecialPrice() - it should load from database
87+
$specialPrice = $specialPriceObject->getSpecialPrice();
88+
89+
// Assert special price was loaded correctly
90+
$this->assertNotNull($specialPrice, 'Special price should be loaded from database');
91+
$this->assertEquals(90, $specialPrice, 'Special price should match the saved value');
92+
93+
// Verify the attribute is now cached in product data
94+
$this->assertTrue(
95+
$product->hasData('special_price'),
96+
'Special price should be cached in product after loading'
97+
);
98+
}
99+
}

0 commit comments

Comments
 (0)