|
| 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 | +} |
0 commit comments