Skip to content

Commit

Permalink
Merge pull request #126 from riha112/2666
Browse files Browse the repository at this point in the history
[2666] Customizable option with discount / price rules
  • Loading branch information
carinadues authored Oct 14, 2021
2 parents 4c1551d + af1f808 commit a8b1bdd
Showing 1 changed file with 71 additions and 39 deletions.
110 changes: 71 additions & 39 deletions src/Model/Resolver/Product/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,14 +23,17 @@
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
*/
class Options extends CoreOptions
{
protected const OPTION_TYPE = 'custom-option';

protected const DYNAMIC_TYPE = 'DYNAMIC';

/**
* @var PriceCurrencyInterface
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit a8b1bdd

Please sign in to comment.