diff --git a/src/Model/Resolver/Product/Options.php b/src/Model/Resolver/Product/Options.php index bcabd1b..aa5d7e3 100644 --- a/src/Model/Resolver/Product/Options.php +++ b/src/Model/Resolver/Product/Options.php @@ -11,6 +11,7 @@ namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Product; +use Magento\Catalog\Pricing\Price\BasePrice; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; @@ -22,6 +23,8 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Uid; use Magento\Framework\App\ObjectManager; +use Magento\Catalog\Model\Product\Option\Value as OptionValue; +use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule; /** * Format a product's option information to conform to GraphQL schema representation @@ -29,7 +32,8 @@ class Options extends CoreOptions { protected const OPTION_TYPE = 'custom-option'; - + protected const DYNAMIC_TYPE = 'DYNAMIC'; + /** * @var PriceCurrencyInterface */ @@ -43,23 +47,85 @@ class Options extends CoreOptions /** @var Uid */ protected $uidEncoder; + /** + * @var CalculateCustomOptionCatalogRule + */ + protected $calculateCustomOptionCatalogRule; + /** * @param PriceCurrencyInterface $priceCurrency * @param CatalogData $catalogData * @param Uid|null $uidEncoder + * @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule */ public function __construct( PriceCurrencyInterface $priceCurrency, CatalogData $catalogData, - Uid $uidEncoder = null + Uid $uidEncoder = null, + CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule ) { + $this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule; $this->priceCurrency = $priceCurrency; $this->catalogData = $catalogData; $this->uidEncoder = $uidEncoder ?: ObjectManager::getInstance() ->get(Uid::class); } + /** + * @param $price + * @param $isPercent + * @param $product + * @return float + */ + public function getPrice($price, $isPercent, $product) + { + $catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute( + $product, + (float)$price, + $isPercent + ); + + if ($catalogPriceValue!==null) { + return $catalogPriceValue; + } + + return $price; + } + + /** + * @param array $optonArray + * @param $optionValue + * @param $product + * @param $currentCurrency + */ + public function updateOptionPriceData(array &$optionArray, $optionValue, $product, $currentCurrency) { + $optionArray['price_type'] = $optionValue->getPriceType() !== null + ? strtoupper($optionValue->getPriceType()) + : self::DYNAMIC_TYPE; + $optionArray['price'] = $this->getPrice( + $optionArray['price'], + strtolower($optionValue->getPriceType()) == OptionValue::TYPE_PERCENT, + $product + ); + + $selectionPrice = $optionArray['price']; + $optionArray['currency'] = $currentCurrency; + + // Calculate price including tax for option value + $taxablePrice = strtolower($optionValue->getPriceType()) == OptionValue::TYPE_PERCENT + ? $product->getFinalPrice() * $selectionPrice / 100 + : $selectionPrice; + $taxablePrice = $this->priceCurrency->convert($taxablePrice); + + $optionArray['priceInclTax'] = $this->catalogData->getTaxPrice( + $product, $taxablePrice, true, null, null, null, null, null, null + ); + $optionArray['priceExclTax'] = $this->catalogData->getTaxPrice( + $product, $taxablePrice, false, null, null, null, null, null, null + ); + } + /** * @inheritdoc * @@ -106,48 +172,14 @@ public function resolve( /** @var Option\Value $optionValue */ foreach ($values as $valueKey => $optionValue) { $options[$key]['value'][$valueKey] = $optionValue->getData(); - $options[$key]['value'][$valueKey]['price_type'] - = $optionValue->getPriceType() !== null ? strtoupper($optionValue->getPriceType()) : 'DYNAMIC'; - - $selectionPrice = $options[$key]['value'][$valueKey]['price']; - $options[$key]['value'][$valueKey]['price'] = $selectionPrice; - $options[$key]['value'][$valueKey]['currency'] = $currentCurrency; - - // Calculate price including tax for option value - $taxablePrice = strtoupper($optionValue->getPriceType()) == 'PERCENT' - ? $product->getFinalPrice() * $selectionPrice / 100 - : $selectionPrice; - $taxablePrice = $this->priceCurrency->convert($taxablePrice); - - $options[$key]['value'][$valueKey]['priceInclTax'] = $this->catalogData->getTaxPrice( - $product, $taxablePrice, true, null, null, null, null, null, null - ); - $options[$key]['value'][$valueKey]['priceExclTax'] = $this->catalogData->getTaxPrice( - $product, $taxablePrice, false, null, null, null, null, null, null + $this->updateOptionPriceData( + $options[$key]['value'][$valueKey], $optionValue, $product, $currentCurrency ); } if (empty($values)) { $options[$key]['value'] = $option->getData(); - $options[$key]['value']['price_type'] - = $option->getPriceType() !== null ? strtoupper($option->getPriceType()) : 'DYNAMIC'; - - $selectionPrice = $options[$key]['value']['price']; - $options[$key]['value']['price'] = $selectionPrice; - $options[$key]['value']['currency'] = $currentCurrency; - - // Calculate price including tax for option value - $taxablePrice = strtoupper($option->getPriceType()) == 'PERCENT' - ? $product->getFinalPrice() * $selectionPrice / 100 - : $selectionPrice; - $taxablePrice = $this->priceCurrency->convert($taxablePrice); - - $options[$key]['value']['priceInclTax'] = $this->catalogData->getTaxPrice( - $product, $taxablePrice, true, null, null, null, null, null, null - ); - $options[$key]['value']['priceExclTax'] = $this->catalogData->getTaxPrice( - $product, $taxablePrice, false, null, null, null, null, null, null - ); + $this->updateOptionPriceData($options[$key]['value'], $option, $product, $currentCurrency); } } }