Skip to content

Commit b563d92

Browse files
committed
adds extended functionality - calculated prices, settings and media data
1 parent 013304a commit b563d92

File tree

7 files changed

+526
-16
lines changed

7 files changed

+526
-16
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © MageWorx. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace MageWorx\OptionGraphQl\Model\Resolver;
9+
10+
use MageWorx\OptionFeatures\Helper\Data as Helper;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Framework\GraphQl\Query\ResolverInterface;
18+
19+
class AdvancedProductOptionsSettings implements ResolverInterface
20+
{
21+
/**
22+
* @var Helper
23+
*/
24+
protected $helper;
25+
26+
/**
27+
* AdvancedProductOptionsSettings constructor.
28+
*
29+
* @param Helper $featuresHelper
30+
*/
31+
public function __construct(
32+
Helper $helper
33+
) {
34+
$this->helper = $helper;
35+
}
36+
37+
/**
38+
* Fetches the data from persistence models and format it according to the GraphQL schema
39+
*
40+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
41+
* @param ContextInterface $context
42+
* @param ResolveInfo $info
43+
* @param array|null $value
44+
* @param array|null $args
45+
* @return mixed|Value
46+
* @throws \Exception
47+
*/
48+
public function resolve(
49+
Field $field,
50+
$context,
51+
ResolveInfo $info,
52+
array $value = null,
53+
array $args = null
54+
) {
55+
try {
56+
$data = [
57+
'is_swatch_title' => $this->helper->isShowSwatchTitle(),
58+
'is_show_swatch_price' => $this->helper->isShowSwatchPrice(),
59+
'is_qty_input_enabled' => $this->helper->isQtyInputEnabled(),
60+
'is_enabled_shareable_link' => $this->helper->isEnabledShareableLink(),
61+
'is_enabled_additional_product_price_field' => $this->helper->isEnabledAdditionalProductPriceField(),
62+
'tooltip_image_thumbnail_size' => $this->helper->getTooltipImageThumbnailSize(),
63+
'additional_product_price_field_lable' => $this->helper->getAdditionalProductPriceFieldLabel(),
64+
'additional_product_price_field_mode' => $this->helper->getAdditionalProductPriceFieldMode(),
65+
'base_image_thumbnail_height' => $this->helper->getBaseImageThumbnailHeight(),
66+
'base_image_thumbnail_width' => $this->helper->getBaseImageThumbnailWidth(),
67+
'default_qty_label' => $this->helper->getDefaultQtyLabel(),
68+
'shareable_link_hint_text' => $this->helper->getShareableLinkHintText(),
69+
'shareable_link_success_text' => $this->helper->getShareableLinkSuccessText(),
70+
'shareable_link_text' => $this->helper->getShareableLinkText(),
71+
'text_swatch_max_width' => $this->helper->getTextSwatchMaxWidth(),
72+
'swatch_width' => $this->helper->getSwatchWidth(),
73+
'swatch_height' => $this->helper->getSwatchHeight()
74+
];
75+
76+
} catch (NoSuchEntityException $e) {
77+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
78+
}
79+
80+
return $data;
81+
}
82+
}

Model/Resolver/DependencyState.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ public function resolve(
9595
];
9696
}
9797

98-
if (!empty($data['hidden_values']) && !empty($selectedValues)) {
99-
foreach ($selectedValues as $selectedValue) {
100-
if (!in_array($selectedValue, $data['hidden_values'])) {
101-
continue;
102-
}
103-
throw new GraphQlInputException(
104-
__("Selected value '%1' is wrong and should be hidden", $selectedValue)
105-
);
106-
}
107-
}
10898
$data['preselected_values'] = $this->baseHelper->jsonEncode($data['preselected_values']);
10999
} catch (NoSuchEntityException $e) {
110100
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Copyright © MageWorx. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace MageWorx\OptionGraphQl\Model\Resolver;
9+
10+
use Magento\Catalog\Model\ProductRepository;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Query\ResolverInterface;
17+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
18+
use Magento\Framework\Pricing\PriceCurrencyInterface;
19+
use Magento\Framework\Serialize\Serializer\Json as Serializer;
20+
use MageWorx\OptionBase\Model\Product\Option\Attributes as OptionAttributes;
21+
use MageWorx\OptionBase\Model\Product\Option\Value\Attributes as OptionValueAttributes;
22+
use MageWorx\OptionBase\Helper\Data as BaseHelper;
23+
use MageWorx\OptionBase\Helper\Price as BasePriceHelper;
24+
use MageWorx\OptionBase\Model\Config\Base as BaseConfig;
25+
26+
27+
class ProductExtendConfig implements ResolverInterface
28+
{
29+
/**
30+
* @var \Magento\Framework\Locale\Format
31+
*/
32+
protected $localeFormat;
33+
34+
/**
35+
* @var PriceCurrencyInterface
36+
*/
37+
protected $priceCurrency;
38+
39+
/**
40+
* @var OptionAttributes
41+
*/
42+
protected $optionAttributes;
43+
44+
/**
45+
* @var OptionValueAttributes
46+
*/
47+
protected $optionValueAttributes;
48+
49+
/**
50+
* @var BaseHelper
51+
*/
52+
protected $baseHelper;
53+
54+
/**
55+
* @var BasePriceHelper
56+
*/
57+
protected $basePriceHelper;
58+
59+
/**
60+
* @var BaseConfig
61+
*/
62+
protected $baseConfig;
63+
64+
/**
65+
* @var Serializer
66+
*/
67+
protected $serializer;
68+
69+
/**
70+
* @var ProductRepository
71+
*/
72+
protected $productRepository;
73+
74+
/**
75+
* ExtendConfig constructor.
76+
*
77+
* @param \Magento\Framework\Locale\Format $localeFormat
78+
* @param PriceCurrencyInterface $priceCurrency
79+
* @param OptionAttributes $optionAttributes
80+
* @param OptionValueAttributes $optionValueAttributes
81+
* @param BaseHelper $baseHelper
82+
* @param BasePriceHelper $basePriceHelper
83+
* @param BaseConfig $baseConfig
84+
* @param Serializer $serializer
85+
* @param ProductRepository $productRepository
86+
*/
87+
public function __construct(
88+
\Magento\Framework\Locale\Format $localeFormat,
89+
PriceCurrencyInterface $priceCurrency,
90+
OptionAttributes $optionAttributes,
91+
OptionValueAttributes $optionValueAttributes,
92+
BaseHelper $baseHelper,
93+
BasePriceHelper $basePriceHelper,
94+
BaseConfig $baseConfig,
95+
Serializer $serializer,
96+
ProductRepository $productRepository
97+
) {
98+
$this->localeFormat = $localeFormat;
99+
$this->priceCurrency = $priceCurrency;
100+
$this->optionAttributes = $optionAttributes;
101+
$this->optionValueAttributes = $optionValueAttributes;
102+
$this->baseHelper = $baseHelper;
103+
$this->basePriceHelper = $basePriceHelper;
104+
$this->baseConfig = $baseConfig;
105+
$this->serializer = $serializer;
106+
$this->productRepository = $productRepository;
107+
}
108+
109+
110+
/**
111+
* Fetches the data from persistence models and format it according to the GraphQL schema.
112+
*
113+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
114+
* @param ContextInterface $context
115+
* @param ResolveInfo $info
116+
* @param array|null $value
117+
* @param array|null $args
118+
* @return mixed|Value
119+
* @throws \Exception
120+
*/
121+
public function resolve(
122+
Field $field,
123+
$context,
124+
ResolveInfo $info,
125+
array $value = null,
126+
array $args = null
127+
) {
128+
$data = [];
129+
130+
try {
131+
$productSku = $args['productSku'] ?? false;
132+
$qty = $args['Qty'] ?? 1;
133+
134+
$product = $this->productRepository->get($productSku);
135+
136+
$data['product_json_config'] = $this->baseConfig->getProductJsonConfig($product);
137+
$data['locale_price_format'] = $this->getLocalePriceFormat();
138+
$data['product_final_price_incl_tax'] = $this->baseConfig->getProductFinalPrice($product, true, $qty);
139+
$data['product_final_price_excl_tax'] = $this->baseConfig->getProductFinalPrice($product, false, $qty);
140+
$data['product_regular_price_incl_tax'] = $this->baseConfig->getProductRegularPrice($product, true);
141+
$data['product_regular_price_excl_tax'] = $this->baseConfig->getProductRegularPrice($product, false);
142+
$data['price_display_mode'] = $this->baseConfig->getPriceDisplayMode();
143+
$data['catalog_price_contains_tax'] = $this->baseConfig->getCatalogPriceContainsTax();
144+
145+
} catch (NoSuchEntityException $e) {
146+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
147+
}
148+
149+
return $data;
150+
}
151+
152+
/**
153+
* @return string
154+
*/
155+
public function getLocalePriceFormat(): string
156+
{
157+
$data = $this->localeFormat->getPriceFormat();
158+
$data['priceSymbol'] = $this->priceCurrency->getCurrency()->getCurrencySymbol();
159+
160+
return $this->serializer->serialize($data);
161+
}
162+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © MageWorx. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace MageWorx\OptionGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Catalog\Model\ProductRepository;
18+
use Magento\Catalog\Model\Product\Type\Price;
19+
use Magento\Framework\Serialize\Serializer\Json as Serializer;
20+
use MageWorx\OptionBase\Helper\Price as BasePriceHelper;
21+
22+
class ProductFinalPrice implements ResolverInterface
23+
{
24+
/**
25+
* @var ProductRepository
26+
*/
27+
protected $productRepository;
28+
29+
/**
30+
* @var Price
31+
*/
32+
protected $priceModel;
33+
34+
/**
35+
* @var Serializer
36+
*/
37+
protected $serializer;
38+
39+
/**
40+
* Item options prefix
41+
*/
42+
const OPTION_PREFIX = \Magento\Catalog\Model\Product\Type\AbstractType::OPTION_PREFIX;
43+
44+
/**
45+
* @var BasePriceHelper
46+
*/
47+
protected $basePriceHelper;
48+
49+
/**
50+
* ProductFinalPrice constructor.
51+
*
52+
* @param ProductRepository $productRepository
53+
* @param Price $priceModel
54+
* @param Serializer $serializer
55+
* @param BasePriceHelper $basePriceHelper
56+
*/
57+
public function __construct(
58+
ProductRepository $productRepository,
59+
Price $priceModel,
60+
Serializer $serializer,
61+
BasePriceHelper $basePriceHelper
62+
) {
63+
$this->productRepository = $productRepository;
64+
$this->priceModel = $priceModel;
65+
$this->serializer = $serializer;
66+
$this->basePriceHelper = $basePriceHelper;
67+
}
68+
69+
70+
/**
71+
* Fetches the data from persistence models and format it according to the GraphQL schema.
72+
* Example $args['currentOptions'] = '{"1120":"8076","1121":"","1122":""}'
73+
*
74+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
75+
* @param ContextInterface $context
76+
* @param ResolveInfo $info
77+
* @param array|null $value
78+
* @param array|null $args
79+
* @return mixed|Value
80+
* @throws \Exception
81+
*/
82+
public function resolve(
83+
Field $field,
84+
$context,
85+
ResolveInfo $info,
86+
array $value = null,
87+
array $args = null
88+
) {
89+
$data = [];
90+
91+
try {
92+
$productSku = $args['productSku'] ?? false;
93+
$selectedValuesString = $args['currentOptions'] ?? false;
94+
$qty = $args['currentQty'] ?? 1;
95+
96+
if ($selectedValuesString && $productSku) {
97+
$product = $this->productRepository->get($productSku);
98+
$unserializedOptions = $this->serializer->unserialize($selectedValuesString);
99+
if ($unserializedOptions) {
100+
$optionIds = array_keys($unserializedOptions);
101+
$product->addCustomOption('option_ids', implode(',', $optionIds));
102+
foreach ($unserializedOptions as $optionId => $optionValue) {
103+
$product->addCustomOption(self::OPTION_PREFIX . $optionId, $optionValue);
104+
}
105+
106+
$product->setHasCustomOptions(true);
107+
$basePrice = (float)$this->priceModel->getFinalPrice($qty, $product);
108+
$data['base_price'] = $basePrice;
109+
110+
$isCatalogPriceContainsTax = $this->basePriceHelper->getCatalogPriceContainsTax(
111+
$product->getStoreId()
112+
);
113+
$needTax = !$isCatalogPriceContainsTax ?? true;
114+
115+
$data['final_price'] = (float)$this->basePriceHelper->getTaxPrice(
116+
$product,
117+
$basePrice,
118+
$needTax
119+
);
120+
}
121+
}
122+
} catch (NoSuchEntityException $e) {
123+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
124+
}
125+
126+
return $data;
127+
}
128+
}

0 commit comments

Comments
 (0)