![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/app/code/Amasty/Label/Model/Pricing/ |
<?php declare(strict_types=1); /** * @author Amasty Team * @copyright Copyright (c) Amasty (https://www.amasty.com) * @package Product Labels for Magento 2 */ namespace Amasty\Label\Model\Pricing; use Magento\Bundle\Model\Product\Type as BundleType; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Pricing\Price\FinalPrice; use Magento\Catalog\Pricing\Price\RegularPrice; use Magento\Catalog\Pricing\Price\SpecialPrice; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; class GetProductSpecialPrice { /** * @var TimezoneInterface */ private $timezone; public function __construct( TimezoneInterface $timezone ) { $this->timezone = $timezone; } public function execute(ProductInterface $product): float { switch ($product->getTypeId()) { case BundleType::TYPE_CODE: case Configurable::TYPE_CODE: $specialPrice = $this->getPrice($product, FinalPrice::PRICE_CODE); break; default: $finalPrice = $this->getPrice($product, FinalPrice::PRICE_CODE); if ($this->isSpecialPriceActive($product)) { $specialPrice = $this->getPrice($product, SpecialPrice::PRICE_CODE); if (!$specialPrice) { $specialPrice = $this->getPrice($product, RegularPrice::PRICE_CODE); } $specialPrice = min($finalPrice, $specialPrice); } else { $specialPrice = $finalPrice; } } return (float) $specialPrice; } /** * @param ProductInterface $product * @param string $code * * @return float|string|false */ private function getPrice(ProductInterface $product, string $code) { return $product->getPriceInfo()->getPrice($code)->getAmount()->getValue(); } private function isSpecialPriceActive(ProductInterface $product): bool { $specialFromDate = $product->getSpecialFromDate(); $specialToDate = $product->getSpecialToDate(); return ($specialFromDate || $specialToDate) && $this->timezone->isScopeDateInInterval( $product->getStoreId(), $specialFromDate, $specialToDate ); } }