Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit dbdc67c

Browse files
authored
Merge pull request #3670 from magento-epam/EPAM-PR-31
[epam] MAGETWO-96429: [2.3.x] Wrong special price displayed in product search results
2 parents d3203b0 + 20dbe08 commit dbdc67c

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Type;
9+
10+
use Magento\Store\Model\Store;
11+
use Magento\Catalog\Model\ResourceModel\Product\Price\SpecialPrice;
12+
use Magento\Catalog\Api\Data\SpecialPriceInterface;
13+
use Magento\Store\Api\Data\WebsiteInterface;
14+
15+
/**
16+
* Product special price model.
17+
*
18+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
*/
21+
class FrontSpecialPrice extends Price
22+
{
23+
/**
24+
* @var SpecialPrice
25+
*/
26+
private $specialPrice;
27+
28+
/**
29+
* @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $ruleFactory
30+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
31+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
32+
* @param \Magento\Customer\Model\Session $customerSession
33+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
34+
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
35+
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
36+
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory
37+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
38+
* @param SpecialPrice $specialPrice
39+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
40+
*/
41+
public function __construct(
42+
\Magento\CatalogRule\Model\ResourceModel\RuleFactory $ruleFactory,
43+
\Magento\Store\Model\StoreManagerInterface $storeManager,
44+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
45+
\Magento\Customer\Model\Session $customerSession,
46+
\Magento\Framework\Event\ManagerInterface $eventManager,
47+
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
48+
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
49+
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory,
50+
\Magento\Framework\App\Config\ScopeConfigInterface $config,
51+
SpecialPrice $specialPrice
52+
) {
53+
$this->specialPrice = $specialPrice;
54+
parent::__construct(
55+
$ruleFactory,
56+
$storeManager,
57+
$localeDate,
58+
$customerSession,
59+
$eventManager,
60+
$priceCurrency,
61+
$groupManagement,
62+
$tierPriceFactory,
63+
$config
64+
);
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
protected function _applySpecialPrice($product, $finalPrice)
71+
{
72+
if (!$product->getSpecialPrice()) {
73+
return $finalPrice;
74+
}
75+
76+
$specialPrices = $this->getSpecialPrices($product);
77+
$specialPrice = !(empty($specialPrices)) ? min($specialPrices) : $product->getSpecialPrice();
78+
79+
$specialPrice = $this->calculateSpecialPrice(
80+
$finalPrice,
81+
$specialPrice,
82+
$product->getSpecialFromDate(),
83+
$product->getSpecialToDate(),
84+
WebsiteInterface::ADMIN_CODE
85+
);
86+
$product->setData('special_price', $specialPrice);
87+
88+
return $specialPrice;
89+
}
90+
91+
/**
92+
* Get special prices.
93+
*
94+
* @param mixed $product
95+
* @return array
96+
*/
97+
private function getSpecialPrices($product): array
98+
{
99+
$allSpecialPrices = $this->specialPrice->get([$product->getSku()]);
100+
$specialPrices = [];
101+
foreach ($allSpecialPrices as $price) {
102+
if ($this->isSuitableSpecialPrice($product, $price)) {
103+
$specialPrices[] = $price['value'];
104+
}
105+
}
106+
107+
return $specialPrices;
108+
}
109+
110+
/**
111+
* Price is suitable from default and current store + start and end date are equal.
112+
*
113+
* @param mixed $product
114+
* @param array $price
115+
* @return bool
116+
*/
117+
private function isSuitableSpecialPrice($product, array $price): bool
118+
{
119+
$priceStoreId = $price[Store::STORE_ID];
120+
if (($priceStoreId == Store::DEFAULT_STORE_ID || $product->getStoreId() == $priceStoreId)
121+
&& $price[SpecialPriceInterface::PRICE_FROM] == $product->getSpecialFromDate()
122+
&& $price[SpecialPriceInterface::PRICE_TO] == $product->getSpecialToDate()) {
123+
return true;
124+
}
125+
126+
return false;
127+
}
128+
}

app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminProductActionGroup.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@
265265
<checkOption selector="{{ProductInWebsitesSection.website(website)}}" stepKey="selectWebsite"/>
266266
</actionGroup>
267267

268+
<actionGroup name="AdminProductAddSpecialPrice">
269+
<arguments>
270+
<argument name="specialPrice" type="string"/>
271+
</arguments>
272+
<click selector="{{AdminProductFormSection.advancedPricingLink}}" stepKey="clickAdvancedPricingLink1"/>
273+
<waitForElementVisible selector="{{AdminProductFormAdvancedPricingSection.specialPrice}}" stepKey="waitSpecialPrice1"/>
274+
<click selector="{{AdminProductFormAdvancedPricingSection.useDefaultPrice}}" stepKey="checkUseDefault"/>
275+
<fillField userInput="{{specialPrice}}" selector="{{AdminProductFormAdvancedPricingSection.specialPrice}}" stepKey="fillSpecialPrice"/>
276+
<click selector="{{AdminProductFormAdvancedPricingSection.doneButton}}" stepKey="clickDone"/>
277+
<waitForElementNotVisible selector="{{AdminProductFormAdvancedPricingSection.specialPrice}}" stepKey="waitForCloseModalWindow"/>
278+
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="clickOnSaveButton"/>
279+
<seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeSaveProductMessage"/>
280+
</actionGroup>
281+
268282
<!--Switch to New Store view-->
269283
<actionGroup name="SwitchToTheNewStoreView">
270284
<arguments>

app/code/Magento/Catalog/Test/Mftf/Data/CatalogStorefrontConfigData.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,26 @@
3838
<entity name="DefaultFlatCatalogProduct" type="flat_catalog_product">
3939
<data key="value">0</data>
4040
</entity>
41+
42+
<entity name="UseFlatCatalogCategoryAndProduct" type="catalog_storefront_config">
43+
<requiredEntity type="flat_catalog_product">UseFlatCatalogProduct</requiredEntity>
44+
<requiredEntity type="flat_catalog_category">UseFlatCatalogCategory</requiredEntity>
45+
</entity>
46+
47+
<entity name="UseFlatCatalogProduct" type="flat_catalog_product">
48+
<data key="value">1</data>
49+
</entity>
50+
51+
<entity name="UseFlatCatalogCategory" type="flat_catalog_category">
52+
<data key="value">1</data>
53+
</entity>
54+
55+
<entity name="DefaultFlatCatalogCategoryAndProduct" type="catalog_storefront_config">
56+
<requiredEntity type="flat_catalog_product">DefaultFlatCatalogProduct</requiredEntity>
57+
<requiredEntity type="flat_catalog_category">DefaultFlatCatalogCategory</requiredEntity>
58+
</entity>
59+
60+
<entity name="DefaultFlatCatalogCategory" type="flat_catalog_category">
61+
<data key="value">0</data>
62+
</entity>
4163
</entities>

app/code/Magento/Catalog/Test/Mftf/Section/AdminProductFormSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,6 @@
187187
<section name="AdminProductFormAdvancedPricingSection">
188188
<element name="specialPrice" type="input" selector="input[name='product[special_price]']"/>
189189
<element name="doneButton" type="button" selector=".product_form_product_form_advanced_pricing_modal button.action-primary"/>
190+
<element name="useDefaultPrice" type="checkbox" selector="//input[@name='product[special_price]']/parent::div/following-sibling::div/input[@name='use_default[special_price]']"/>
190191
</section>
191192
</sections>

app/code/Magento/Catalog/etc/frontend/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@
120120
<plugin name="catalog_app_action_dispatch_controller_context_plugin"
121121
type="Magento\Catalog\Plugin\Framework\App\Action\ContextPlugin" />
122122
</type>
123+
<preference for="Magento\Catalog\Model\Product\Type\Price" type="Magento\Catalog\Model\Product\Type\FrontSpecialPrice" />
123124
</config>

0 commit comments

Comments
 (0)