Skip to content

Commit ffa9cb6

Browse files
committed
init
1 parent 945f014 commit ffa9cb6

File tree

6 files changed

+317
-0
lines changed

6 files changed

+317
-0
lines changed

Model/Resolver/DependencyState.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Event\ManagerInterface;
17+
use Magento\Catalog\Api\ProductRepositoryInterface;
18+
use MageWorx\OptionBase\Model\HiddenDependents as HiddenDependentsStorage;
19+
use MageWorx\OptionBase\Helper\Data as BaseHelper;
20+
21+
/**
22+
* DependencyState page field resolver for GraphQL request processing
23+
*/
24+
class DependencyState implements ResolverInterface
25+
{
26+
/**
27+
* @var BaseHelper
28+
*/
29+
protected $baseHelper;
30+
31+
/**
32+
* @var ProductRepositoryInterface
33+
*/
34+
protected $productRepository;
35+
36+
/**
37+
* @var HiddenDependentsStorage
38+
*/
39+
protected $hiddenDependentsStorage;
40+
41+
/**
42+
* @var ManagerInterface
43+
*/
44+
protected $eventManager;
45+
46+
/**
47+
* @param BaseHelper $baseHelper
48+
* @param ProductRepositoryInterface $productRepository
49+
* @param HiddenDependentsStorage $hiddenDependentsStorage
50+
* @param ManagerInterface $eventManager
51+
*/
52+
public function __construct(
53+
BaseHelper $baseHelper,
54+
ProductRepositoryInterface $productRepository,
55+
HiddenDependentsStorage $hiddenDependentsStorage,
56+
ManagerInterface $eventManager
57+
) {
58+
$this->baseHelper = $baseHelper;
59+
$this->productRepository = $productRepository;
60+
$this->hiddenDependentsStorage = $hiddenDependentsStorage;
61+
$this->eventManager = $eventManager;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function resolve(
68+
Field $field,
69+
$context,
70+
ResolveInfo $info,
71+
array $value = null,
72+
array $args = null
73+
) {
74+
try {
75+
$productSku = $args['productSku'] ?? false;
76+
$selectedValuesString = $args['selectedValues'] ?? [];
77+
$selectedValues = explode(',', $selectedValuesString);
78+
79+
$product = $this->productRepository->get($productSku);
80+
if (!$product) {
81+
throw new GraphQlNoSuchEntityException(__("Wrong product SKU"));
82+
}
83+
84+
$this->eventManager->dispatch(
85+
'mageworx_calculate_dependency_state',
86+
['product' => $product, 'selected_values' => $selectedValues]
87+
);
88+
89+
$data = $this->hiddenDependentsStorage->getQuoteItemsHiddenDependents();
90+
if (!$data) {
91+
$data = [
92+
'hidden_options' => [],
93+
'hidden_values' => [],
94+
'preselected_values' => []
95+
];
96+
}
97+
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+
}
108+
$data['preselected_values'] = $this->baseHelper->jsonEncode($data['preselected_values']);
109+
} catch (NoSuchEntityException $e) {
110+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
111+
}
112+
113+
return $data;
114+
}
115+
}

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "mageworx/module-optiongraphql",
3+
"description": "GraphQl support for APO",
4+
"require": {
5+
"magento/framework" : ">=102.0.0 <104",
6+
"magento/module-catalog-graph-ql": ">=100.3.0",
7+
"mageworx/module-optionbase" : ">=2.16.3"
8+
},
9+
"type": "magento2-module",
10+
"version": "2.0.0",
11+
"license": [
12+
"OSL-3.0",
13+
"AFL-3.0"
14+
],
15+
"autoload": {
16+
"files": [
17+
"registration.php"
18+
],
19+
"psr-4": {
20+
"MageWorx\\OptionGraphQl\\": ""
21+
}
22+
}
23+
}

etc/module.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © MageWorx. All rights reserved.
5+
* See LICENSE.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="MageWorx_OptionGraphQl" setup_version="2.0.0">
10+
<sequence>
11+
<module name="MageWorx_OptionBase"/>
12+
<module name="Magento_Catalog"/>
13+
<module name="Magento_CatalogGraphQl"/>
14+
</sequence>
15+
</module>
16+
</config>

etc/schema.graphqls

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
type Query {
2+
dependencyState (
3+
productSku : String @doc(description: "Product SKU")
4+
selectedValues : String @doc(description: "Selected option value IDs to calculate dependencies")
5+
): DependencyState @resolver(class: "MageWorx\\OptionGraphQl\\Model\\Resolver\\DependencyState") @doc(description: "The query returns DependencyState information")
6+
}
7+
8+
type DependencyState @doc(description: "DependencyState information, array of hidden options, hidden values and preselected values") {
9+
hidden_options: [Int] @doc(description: "An array of hidden options")
10+
hidden_values: [Int] @doc(description: "An array of hidden values")
11+
preselected_values: String @doc(description: "JSON with preselected values")
12+
}
13+
14+
interface ProductInterface {
15+
dependency_rules: String @doc(description: "Dependency Rules")
16+
hidden_dependents: String @doc(description: "Hidden Dependents")
17+
absolute_price: String @doc(description: "Absolute Price")
18+
absolute_cost: String @doc(description: "Absolute Cost")
19+
absolute_weight: String @doc(description: "Absolute Weight")
20+
sku_policy: String @doc(description: "Sku Policy")
21+
shareable_link: String @doc(description: "Shareable Link")
22+
hide_additional_product_price: String @doc(description: "Hide Additional Product Price")
23+
}
24+
25+
interface CustomizableOptionInterface {
26+
description: String @doc(description: "Description")
27+
sku_policy: String @doc(description: "Sku Policy")
28+
qty_input: String @doc(description: "Qty Input")
29+
div_class: String @doc(description: "Div Class")
30+
one_time: String @doc(description: "One-time")
31+
mageworx_option_gallery: String @doc(description: "Option Gallery Mode")
32+
mageworx_option_image_mode: String @doc(description: "Option Image Mode")
33+
is_all_customer_groups: String @doc(description: "Is Visible by All Customer Groups")
34+
is_all_store_views: String @doc(description: "Is Visible by All Store Views")
35+
customer_group: String @doc(description: "Customer Groups Visibility")
36+
store_view: String @doc(description: "Store Views Visibility")
37+
disabled: String @doc(description: "Is Disabled")
38+
disabled_by_values: String @doc(description: "Is Disabled by Values")
39+
}
40+
41+
type CustomizableCheckboxValue {
42+
is_hidden: String @doc(description: "Is Hidden")
43+
selection_limit_from: String @doc(description: "Selection Limit From")
44+
selection_limit_to: String @doc(description: "Selection Limit To")
45+
mageworx_option_type_price: String @doc(description: "MageWorx Option Value Price")
46+
mageworx_title: String @doc(description: "MageWorx Option Value Title")
47+
special_price: String @doc(description: "Special Price")
48+
tier_price: String @doc(description: "Tier Price")
49+
description: String @doc(description: "Description")
50+
dependency: String @doc(description: "Dependency")
51+
dependency_type: String @doc(description: "Dependency Type")
52+
cost: String @doc(description: "Cost")
53+
images_data: String @doc(description: "Images Data")
54+
is_default: String @doc(description: "Is Default")
55+
qty_multiplier: String @doc(description: "Quantity Multiplier")
56+
weight: String @doc(description: "Weight")
57+
weight_type: String @doc(description: "Weight Type")
58+
qty: String @doc(description: "Quantity")
59+
manage_stock: String @doc(description: "Manage Stock")
60+
disabled: String @doc(description: "Is Disabled")
61+
}
62+
63+
type CustomizableRadioValue {
64+
mageworx_option_type_price: String @doc(description: "MageWorx Option Value Price")
65+
mageworx_title: String @doc(description: "MageWorx Option Value Title")
66+
special_price: String @doc(description: "Special Price")
67+
tier_price: String @doc(description: "Tier Price")
68+
description: String @doc(description: "Description")
69+
dependency: String @doc(description: "Dependency")
70+
dependency_type: String @doc(description: "Dependency Type")
71+
cost: String @doc(description: "Cost")
72+
images_data: String @doc(description: "Images Data")
73+
is_default: String @doc(description: "Is Default")
74+
qty_multiplier: String @doc(description: "Quantity Multiplier")
75+
weight: String @doc(description: "Weight")
76+
weight_type: String @doc(description: "Weight Type")
77+
qty: String @doc(description: "Quantity")
78+
manage_stock: String @doc(description: "Manage Stock")
79+
disabled: String @doc(description: "Is Disabled")
80+
}
81+
82+
type CustomizableDropDownValue {
83+
is_swatch: String @doc(description: "Is Swatch")
84+
mageworx_option_type_price: String @doc(description: "MageWorx Option Value Price")
85+
mageworx_title: String @doc(description: "MageWorx Option Value Title")
86+
special_price: String @doc(description: "Special Price")
87+
tier_price: String @doc(description: "Tier Price")
88+
description: String @doc(description: "Description")
89+
dependency: String @doc(description: "Dependency")
90+
dependency_type: String @doc(description: "Dependency Type")
91+
cost: String @doc(description: "Cost")
92+
images_data: String @doc(description: "Images Data")
93+
is_default: String @doc(description: "Is Default")
94+
qty_multiplier: String @doc(description: "Quantity Multiplier")
95+
weight: String @doc(description: "Weight")
96+
weight_type: String @doc(description: "Weight Type")
97+
qty: String @doc(description: "Quantity")
98+
manage_stock: String @doc(description: "Manage Stock")
99+
disabled: String @doc(description: "Is Disabled")
100+
}
101+
102+
type CustomizableMultipleValue {
103+
is_swatch: String @doc(description: "Is Swatch")
104+
selection_limit_from: String @doc(description: "Selection Limit From")
105+
selection_limit_to: String @doc(description: "Selection Limit To")
106+
mageworx_option_type_price: String @doc(description: "MageWorx Option Value Price")
107+
mageworx_title: String @doc(description: "MageWorx Option Value Title")
108+
special_price: String @doc(description: "Special Price")
109+
tier_price: String @doc(description: "Tier Price")
110+
description: String @doc(description: "Description")
111+
dependency: String @doc(description: "Dependency")
112+
dependency_type: String @doc(description: "Dependency Type")
113+
cost: String @doc(description: "Cost")
114+
images_data: String @doc(description: "Images Data")
115+
is_default: String @doc(description: "Is Default")
116+
qty_multiplier: String @doc(description: "Quantity Multiplier")
117+
weight: String @doc(description: "Weight")
118+
weight_type: String @doc(description: "Weight Type")
119+
qty: String @doc(description: "Quantity")
120+
manage_stock: String @doc(description: "Manage Stock")
121+
disabled: String @doc(description: "Is Disabled")
122+
}
123+
124+
type CustomizableFieldValue {
125+
dependency: String @doc(description: "Dependency")
126+
dependency_type: String @doc(description: "Dependency Type")
127+
mageworx_option_price: String @doc(description: "MageWorx Option Price")
128+
mageworx_title: String @doc(description: "MageWorx Option Title")
129+
}
130+
131+
type CustomizableAreaValue {
132+
dependency: String @doc(description: "Dependency")
133+
dependency_type: String @doc(description: "Dependency Type")
134+
mageworx_option_price: String @doc(description: "MageWorx Option Price")
135+
mageworx_title: String @doc(description: "MageWorx Option Title")
136+
}
137+
138+
type CustomizableFileValue {
139+
dependency: String @doc(description: "Dependency")
140+
dependency_type: String @doc(description: "Dependency Type")
141+
mageworx_option_price: String @doc(description: "MageWorx Option Price")
142+
mageworx_title: String @doc(description: "MageWorx Option Title")
143+
}
144+
145+
type CustomizableDateValue {
146+
dependency: String @doc(description: "Dependency")
147+
dependency_type: String @doc(description: "Dependency Type")
148+
mageworx_option_price: String @doc(description: "MageWorx Option Price")
149+
mageworx_title: String @doc(description: "MageWorx Option Title")
150+
}

i18n/en_US.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Wrong product SKU","Wrong product SKU"
2+
"Selected value '%1' is wrong and should be hidden","Selected value '%1' is wrong and should be hidden"

registration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © MageWorx. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
\Magento\Framework\Component\ComponentRegistrar::register(
8+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
9+
'MageWorx_OptionGraphQl',
10+
__DIR__
11+
);

0 commit comments

Comments
 (0)