|
| 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\Pricing\Price; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\Product\PriceModifierInterface; |
| 12 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Calculates prices of custom options of the product with catalog rules applied. |
| 16 | + */ |
| 17 | +class CalculateCustomOptionCatalogRule |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var PriceCurrencyInterface |
| 21 | + */ |
| 22 | + private $priceCurrency; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var PriceModifierInterface |
| 26 | + */ |
| 27 | + private $priceModifier; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param PriceCurrencyInterface $priceCurrency |
| 31 | + * @param PriceModifierInterface $priceModifier |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + PriceCurrencyInterface $priceCurrency, |
| 35 | + PriceModifierInterface $priceModifier |
| 36 | + ) { |
| 37 | + $this->priceModifier = $priceModifier; |
| 38 | + $this->priceCurrency = $priceCurrency; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Calculate prices of custom options of the product with catalog rules applied. |
| 43 | + * |
| 44 | + * @param Product $product |
| 45 | + * @param float $optionPriceValue |
| 46 | + * @param bool $isPercent |
| 47 | + * @return float |
| 48 | + */ |
| 49 | + public function execute( |
| 50 | + Product $product, |
| 51 | + float $optionPriceValue, |
| 52 | + bool $isPercent |
| 53 | + ): float { |
| 54 | + $regularPrice = (float)$product->getPriceInfo() |
| 55 | + ->getPrice(RegularPrice::PRICE_CODE) |
| 56 | + ->getValue(); |
| 57 | + $catalogRulePrice = $this->priceModifier->modifyPrice( |
| 58 | + $regularPrice, |
| 59 | + $product |
| 60 | + ); |
| 61 | + |
| 62 | + // Apply catalog price rules to product options only if catalog price rules are applied to product. |
| 63 | + if ($catalogRulePrice < $regularPrice) { |
| 64 | + $optionPrice = $this->getOptionPriceWithoutPriceRule($optionPriceValue, $isPercent, $regularPrice); |
| 65 | + $totalCatalogRulePrice = $this->priceModifier->modifyPrice( |
| 66 | + $regularPrice + $optionPrice, |
| 67 | + $product |
| 68 | + ); |
| 69 | + $finalOptionPrice = $totalCatalogRulePrice - $catalogRulePrice; |
| 70 | + } else { |
| 71 | + $finalOptionPrice = $this->getOptionPriceWithoutPriceRule( |
| 72 | + $optionPriceValue, |
| 73 | + $isPercent, |
| 74 | + $regularPrice |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return $this->priceCurrency->convertAndRound($finalOptionPrice); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * Calculate option price without catalog price rule discount. |
| 84 | + * |
| 85 | + * @param float $optionPriceValue |
| 86 | + * @param bool $isPercent |
| 87 | + * @param float $basePrice |
| 88 | + * @return float |
| 89 | + */ |
| 90 | + private function getOptionPriceWithoutPriceRule(float $optionPriceValue, bool $isPercent, float $basePrice): float |
| 91 | + { |
| 92 | + return $isPercent ? $basePrice * $optionPriceValue / 100 : $optionPriceValue; |
| 93 | + } |
| 94 | +} |
0 commit comments