Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
#141 Retrieves actual price value units
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Glushko committed Sep 6, 2018
1 parent 5140906 commit 236ce9a
Showing 1 changed file with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Authorization\Model\UserContextInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Model\Config\Source\ProductPriceOptionsInterface;
use Magento\Catalog\Model\Product\Option\Type\DefaultType as DefaultOptionType;
use Magento\Catalog\Model\Product\Option\Type\Select as SelectOptionType;
use Magento\Catalog\Model\Product\Option\Type\Text as TextOptionType;
Expand All @@ -18,12 +19,20 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* {@inheritdoc}
*/
class CustomizableOptions implements ResolverInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var ValueFactory
*/
Expand All @@ -37,13 +46,16 @@ class CustomizableOptions implements ResolverInterface
/**
* @param ValueFactory $valueFactory
* @param UserContextInterface $userContext
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ValueFactory $valueFactory,
UserContextInterface $userContext
UserContextInterface $userContext,
StoreManagerInterface $storeManager
) {
$this->valueFactory = $valueFactory;
$this->userContext = $userContext;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -134,9 +146,11 @@ private function getOptionData($cartItem, int $optionId): array
|| ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX == $option->getType()
) {
$optionValue = $option->getValueById($itemOption->getValue());
$priceValueUnits = $this->getPriceValueUnits($optionValue->getPriceType());

$selectedOptionValueData['price'] = [
'type' => strtoupper($optionValue->getPriceType()),
'units' => '$',
'units' => $priceValueUnits,
'value' => $optionValue->getPrice(),
];

Expand All @@ -148,9 +162,11 @@ private function getOptionData($cartItem, int $optionId): array
|| ProductCustomOptionInterface::OPTION_GROUP_DATE == $option->getType()
|| ProductCustomOptionInterface::OPTION_TYPE_TIME == $option->getType()
) {
$priceValueUnits = $this->getPriceValueUnits($option->getPriceType());

$selectedOptionValueData['price'] = [
'type' => strtoupper($option->getPriceType()),
'units' => '$',
'units' => $priceValueUnits,
'value' => $option->getPrice(),
];

Expand All @@ -163,13 +179,14 @@ private function getOptionData($cartItem, int $optionId): array

foreach ($optionIds as $optionId) {
$optionValue = $option->getValueById($optionId);
$priceValueUnits = $this->getPriceValueUnits($optionValue->getPriceType());

$selectedOptionValueData[] = [
'id' => $itemOption->getId(),
'label' => $optionValue->getTitle(),
'price' => [
'type' => strtoupper($optionValue->getPriceType()),
'units' => '$',
'units' => $priceValueUnits,
'value' => $optionValue->getPrice(),
],
];
Expand All @@ -184,4 +201,29 @@ private function getOptionData($cartItem, int $optionId): array
'sort_order' => $option->getSortOrder(),
];
}

/**
* @param string $priceType
*/
private function getPriceValueUnits(string $priceType): string
{
if (ProductPriceOptionsInterface::VALUE_PERCENT == $priceType) {
return '%';
}

return $this->getCurrencySymbol();
}

/**
* Get currency symbol
*
* @return string
*/
private function getCurrencySymbol(): string
{
/** @var Store|StoreInterface $store */
$store = $this->storeManager->getStore();

return $store->getBaseCurrency()->getCurrencySymbol();
}
}

0 comments on commit 236ce9a

Please sign in to comment.