Skip to content

Commit 625b01e

Browse files
authored
LYNX-867: [AC] Intoroduce GraphQL queries returning applied customer group and cart rules
1 parent f8d95a5 commit 625b01e

File tree

18 files changed

+834
-2
lines changed

18 files changed

+834
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Config;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
class AccountInformation
13+
{
14+
private const XML_PATH_SHARE_CUSTOMER_GROUP = 'customer/account_information/graphql_share_customer_group';
15+
16+
/**
17+
* AccountInformation constructor
18+
*
19+
* @param ScopeConfigInterface $scopeConfig
20+
*/
21+
public function __construct(
22+
private readonly ScopeConfigInterface $scopeConfig
23+
) {
24+
}
25+
26+
/**
27+
* Is 'graphql_share_customer_group' config enabled
28+
*
29+
* @return bool
30+
*/
31+
public function isShareCustomerGroupEnabled(): bool
32+
{
33+
return $this->scopeConfig->isSetFlag(self::XML_PATH_SHARE_CUSTOMER_GROUP);
34+
}
35+
}

app/code/Magento/Customer/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@
197197
<label>Require email confirmation if email has been changed</label>
198198
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
199199
</field>
200+
<field id="graphql_share_customer_group" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0">
201+
<label>Allow to retrieve applied customer group details via GraphQL API</label>
202+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
203+
<comment>Option to disable providing customer group details via GraphQl</comment>
204+
</field>
200205
</group>
201206
<group id="address" translate="label" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
202207
<label>Name and Address Options</label>

app/code/Magento/Customer/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<change_email_template>customer_account_information_change_email_template</change_email_template>
3434
<change_email_and_password_template>customer_account_information_change_email_and_password_template</change_email_and_password_template>
3535
<confirm>0</confirm>
36+
<graphql_share_customer_group>1</graphql_share_customer_group>
3637
</account_information>
3738
<password>
3839
<forgot_email_identity>support</forgot_email_identity>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\Customer\Model\Config\AccountInformation;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Query\Uid;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
class CustomerGroup implements ResolverInterface
18+
{
19+
/**
20+
* CustomerGroup Constructor
21+
*
22+
* @param AccountInformation $config
23+
* @param Uid $idEncoder
24+
*/
25+
public function __construct(
26+
private readonly AccountInformation $config,
27+
private readonly Uid $idEncoder
28+
) {
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function resolve(
35+
Field $field,
36+
$context,
37+
ResolveInfo $info,
38+
?array $value = null,
39+
?array $args = null
40+
): ?array {
41+
if (!isset($value['model'])) {
42+
throw new LocalizedException(__('"model" value should be specified'));
43+
}
44+
45+
if (!$this->config->isShareCustomerGroupEnabled()) {
46+
return null;
47+
}
48+
49+
return [
50+
'uid' => $this->idEncoder->encode((string)$value['model']->getGroupId())
51+
];
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\CatalogCustomerGraphQl\Model\Resolver\Customer\GetCustomerGroup;
11+
use Magento\Customer\Model\Config\AccountInformation;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Query\Uid;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
18+
class Visitor implements ResolverInterface
19+
{
20+
/**
21+
* Visitor Constructor
22+
*
23+
* @param AccountInformation $config
24+
* @param GetCustomerGroup $getCustomerGroup
25+
* @param Uid $idEncoder
26+
*/
27+
public function __construct(
28+
private readonly AccountInformation $config,
29+
private readonly GetCustomerGroup $getCustomerGroup,
30+
private readonly Uid $idEncoder
31+
) {
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function resolve(
38+
Field $field,
39+
$context,
40+
ResolveInfo $info,
41+
?array $value = null,
42+
?array $args = null
43+
): array {
44+
45+
if (!$this->config->isShareCustomerGroupEnabled()) {
46+
throw new GraphQlInputException(__('Sharing customer group information is disabled or not configured.'));
47+
}
48+
49+
return [
50+
'uid' => $this->idEncoder->encode((string)$this->getCustomerGroup->execute($context->getUserId()))
51+
];
52+
}
53+
}

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"magento/module-tax": "*",
1818
"magento/module-graph-ql-cache": "*",
1919
"magento/module-graph-ql-resolver-cache": "*",
20-
"magento/module-sales": "*"
20+
"magento/module-sales": "*",
21+
"magento/module-catalog-customer-graph-ql": "*"
2122
},
2223
"license": [
2324
"OSL-3.0",

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<item name="minimum_password_length" xsi:type="string">customer/password/minimum_password_length</item>
3939
<item name="autocomplete_on_storefront" xsi:type="string">customer/password/autocomplete_on_storefront</item>
4040
<item name="create_account_confirmation" xsi:type="string">customer/create_account/confirm</item>
41+
<item name="graphql_share_customer_group" xsi:type="string">customer/account_information/graphql_share_customer_group</item>
4142
</argument>
4243
</arguments>
4344
</type>

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ type StoreConfig {
66
minimum_password_length : String @doc(description: "The minimum number of characters required for a valid password.")
77
autocomplete_on_storefront : Boolean @doc(description: "Indicates whether to enable autocomplete on login and forgot password forms.")
88
create_account_confirmation: Boolean @doc(description: "Indicates if the new accounts need confirmation.")
9+
graphql_share_customer_group: Boolean @doc(description: "Configuration data from customer/account_information/graphql_share_customer_group")
910
}
1011

1112
type Query {
1213
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "Return detailed information about a customer account.") @cache(cacheable: false)
1314
isEmailAvailable (
1415
email: String! @doc(description: "The email address to check.")
1516
): IsEmailAvailableOutput @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\IsEmailAvailable") @doc(description: "Check whether the specified email has already been used to create a customer account.")
17+
customerGroup: CustomerGroupStorefront! @doc(description: "Provides Customer Group assigned to the Customer or Guest.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Visitor")
1618
}
1719

1820
type Mutation {
@@ -153,6 +155,7 @@ type Customer @doc(description: "Defines the customer name, addresses, and other
153155
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2).")
154156
custom_attributes(attributeCodes: [ID!]): [AttributeValueInterface] @doc(description: "Customer's custom attributes.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CustomAttributeFilter")
155157
confirmation_status: ConfirmationStatusEnum! @doc(description: "The customer's confirmation status.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\ConfirmationStatus")
158+
group: CustomerGroupStorefront @doc(description: "Customer group assigned to the customer") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerGroup")
156159
}
157160

158161
type CustomerAddresses {
@@ -491,3 +494,7 @@ enum ConfirmationStatusEnum @doc(description: "List of account confirmation stat
491494
ACCOUNT_CONFIRMED @doc(description: "Account confirmed")
492495
ACCOUNT_CONFIRMATION_NOT_REQUIRED @doc(description: "Account confirmation not required")
493496
}
497+
498+
type CustomerGroupStorefront @doc(description: "Data of customer group.") {
499+
uid: ID! @doc(description: "The unique ID for a `CustomerGroup` object.")
500+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
class Config
14+
{
15+
public const SHARE_APPLIED_CART_RULES = 'promo/graphql/share_applied_cart_rule';
16+
17+
/**
18+
* Config Constructor
19+
*
20+
* @param ScopeConfigInterface $scopeConfig
21+
*/
22+
public function __construct(
23+
private readonly ScopeConfigInterface $scopeConfig,
24+
) {
25+
}
26+
27+
/**
28+
* Get share currently applied cart rule flag value
29+
*
30+
* @return bool
31+
*/
32+
public function isShareAppliedCartRulesEnabled(): bool
33+
{
34+
return $this->scopeConfig->isSetFlag(self::SHARE_APPLIED_CART_RULES, ScopeInterface::SCOPE_STORE);
35+
}
36+
}

app/code/Magento/SalesRule/etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
<frontend_class>validate-digits</frontend_class>
4141
</field>
4242
</group>
43+
<group id="graphql" translate="label" showInDefault="1" sortOrder="10">
44+
<label>GraphQl Configurations</label>
45+
<field id="share_applied_cart_rule" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
46+
<label>Share Applied Cart Rules</label>
47+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
48+
<comment>Allow to retrieve applied cart rules via GraphQL API</comment>
49+
</field>
50+
</group>
4351
</section>
4452
<section id="rss">
4553
<group id="catalog">

0 commit comments

Comments
 (0)