Skip to content

Commit 75cf826

Browse files
authored
Merge pull request #3848 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 74ebef8 + a1fa332 commit 75cf826

38 files changed

+1134
-237
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@
3838
</item>
3939
<item name="customizable_options" xsi:type="array">
4040
<item name="field" xsi:type="string">CustomizableFieldOption</item>
41+
<item name="date" xsi:type="string">CustomizableDateOption</item>
4142
<item name="date_time" xsi:type="string">CustomizableDateOption</item>
43+
<item name="time" xsi:type="string">CustomizableDateOption</item>
4244
<item name="file" xsi:type="string">CustomizableFileOption</item>
4345
<item name="area" xsi:type="string">CustomizableAreaOption</item>
4446
<item name="drop_down" xsi:type="string">CustomizableDropDownOption</item>
47+
<item name="multiple" xsi:type="string">CustomizableMultipleOption</item>
4548
<item name="radio" xsi:type="string">CustomizableRadioOption</item>
49+
<item name="checkbox" xsi:type="string">CustomizableCheckboxOption</item>
4650
</item>
4751
</argument>
4852
</arguments>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ type CustomizableDropDownValue @doc(description: "CustomizableDropDownValue defi
323323
sort_order: Int @doc(description: "The order in which the option is displayed")
324324
}
325325

326+
type CustomizableMultipleOption implements CustomizableOptionInterface @doc(description: "CustomizableMultipleOption contains information about a multiselect that is defined as part of a customizable option") {
327+
value: [CustomizableMultipleValue] @doc(description: "An array that defines the set of options for a multiselect")
328+
}
329+
330+
type CustomizableMultipleValue @doc(description: "CustomizableMultipleValue defines the price and sku of a product whose page contains a customized multiselect") {
331+
option_type_id: Int @doc(description: "The ID assigned to the value")
332+
price: Float @doc(description: "The price assigned to this option")
333+
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
334+
sku: String @doc(description: "The Stock Keeping Unit for this option")
335+
title: String @doc(description: "The display name for this option")
336+
sort_order: Int @doc(description: "The order in which the option is displayed")
337+
}
338+
326339
type CustomizableFieldOption implements CustomizableOptionInterface @doc(description: "CustomizableFieldOption contains information about a text field that is defined as part of a customizable option") {
327340
value: CustomizableFieldValue @doc(description: "An object that defines a text field")
328341
product_sku: String @doc(description: "The Stock Keeping Unit of the base product")
@@ -407,6 +420,19 @@ type CustomizableRadioValue @doc(description: "CustomizableRadioValue defines t
407420
sort_order: Int @doc(description: "The order in which the radio button is displayed")
408421
}
409422

423+
type CustomizableCheckboxOption implements CustomizableOptionInterface @doc(description: "CustomizableCheckbbixOption contains information about a set of checkbox values that are defined as part of a customizable option") {
424+
value: [CustomizableCheckboxValue] @doc(description: "An array that defines a set of checkbox values")
425+
}
426+
427+
type CustomizableCheckboxValue @doc(description: "CustomizableCheckboxValue defines the price and sku of a product whose page contains a customized set of checkbox values") {
428+
option_type_id: Int @doc(description: "The ID assigned to the value")
429+
price: Float @doc(description: "The price assigned to this option")
430+
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
431+
sku: String @doc(description: "The Stock Keeping Unit for this option")
432+
title: String @doc(description: "The display name for this option")
433+
sort_order: Int @doc(description: "The order in which the checkbox value is displayed")
434+
}
435+
410436
type VirtualProduct implements ProductInterface, CustomizableProductInterface @doc(description: "A virtual product is non-tangible product that does not require shipping and is not kept in inventory") {
411437
}
412438

app/code/Magento/QuoteGraphQl/Model/Cart/AssignBillingAddressToCart.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\AddressInterface;
1415
use Magento\Quote\Api\Data\CartInterface;
1516
use Magento\Quote\Api\BillingAddressManagementInterface;
16-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1717

1818
/**
1919
* Set billing address for a specified shopping cart
@@ -38,22 +38,22 @@ public function __construct(
3838
* Assign billing address to cart
3939
*
4040
* @param CartInterface $cart
41-
* @param QuoteAddress $billingAddress
41+
* @param AddressInterface $billingAddress
4242
* @param bool $useForShipping
4343
* @throws GraphQlInputException
4444
* @throws GraphQlNoSuchEntityException
4545
*/
4646
public function execute(
4747
CartInterface $cart,
48-
QuoteAddress $billingAddress,
48+
AddressInterface $billingAddress,
4949
bool $useForShipping
5050
): void {
5151
try {
5252
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
5353
} catch (NoSuchEntityException $e) {
54-
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
54+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
5555
} catch (LocalizedException $e) {
56-
throw new GraphQlInputException(__($e->getMessage()));
56+
throw new GraphQlInputException(__($e->getMessage()), $e);
5757
}
5858
}
5959
}

app/code/Magento/QuoteGraphQl/Model/Cart/AssignShippingAddressToCart.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\AddressInterface;
1415
use Magento\Quote\Api\Data\CartInterface;
15-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1616
use Magento\Quote\Model\ShippingAddressManagementInterface;
1717

1818
/**
@@ -38,20 +38,20 @@ public function __construct(
3838
* Assign shipping address to cart
3939
*
4040
* @param CartInterface $cart
41-
* @param QuoteAddress $shippingAddress
41+
* @param AddressInterface $shippingAddress
4242
* @throws GraphQlInputException
4343
* @throws GraphQlNoSuchEntityException
4444
*/
4545
public function execute(
4646
CartInterface $cart,
47-
QuoteAddress $shippingAddress
47+
AddressInterface $shippingAddress
4848
): void {
4949
try {
5050
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
5151
} catch (NoSuchEntityException $e) {
52-
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
52+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
5353
} catch (LocalizedException $e) {
54-
throw new GraphQlInputException(__($e->getMessage()));
54+
throw new GraphQlInputException(__($e->getMessage()), $e);
5555
}
5656
}
5757
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Checkout\Api\Data\ShippingInformationInterface;
11+
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
12+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
17+
use Magento\Quote\Api\Data\AddressInterface;
18+
use Magento\Quote\Api\Data\CartInterface;
19+
20+
/**
21+
* Assign shipping method to cart
22+
*/
23+
class AssignShippingMethodToCart
24+
{
25+
/**
26+
* @var ShippingInformationInterfaceFactory
27+
*/
28+
private $shippingInformationFactory;
29+
30+
/**
31+
* @var ShippingInformationManagementInterface
32+
*/
33+
private $shippingInformationManagement;
34+
35+
/**
36+
* @param ShippingInformationInterfaceFactory $shippingInformationFactory
37+
* @param ShippingInformationManagementInterface $shippingInformationManagement
38+
*/
39+
public function __construct(
40+
ShippingInformationInterfaceFactory $shippingInformationFactory,
41+
ShippingInformationManagementInterface $shippingInformationManagement
42+
) {
43+
$this->shippingInformationFactory = $shippingInformationFactory;
44+
$this->shippingInformationManagement = $shippingInformationManagement;
45+
}
46+
47+
/**
48+
* Assign shipping method to cart
49+
*
50+
* @param CartInterface $cart
51+
* @param AddressInterface $quoteAddress
52+
* @param string $carrierCode
53+
* @param string $methodCode
54+
* @throws GraphQlInputException
55+
* @throws GraphQlNoSuchEntityException
56+
*/
57+
public function execute(
58+
CartInterface $cart,
59+
AddressInterface $quoteAddress,
60+
string $carrierCode,
61+
string $methodCode
62+
): void {
63+
/** @var ShippingInformationInterface $shippingInformation */
64+
$shippingInformation = $this->shippingInformationFactory->create([
65+
'data' => [
66+
/* If the address is not a shipping address (but billing) the system will find the proper shipping
67+
address for the selected cart and set the information there (actual for single shipping address) */
68+
ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress,
69+
ShippingInformationInterface::SHIPPING_CARRIER_CODE => $carrierCode,
70+
ShippingInformationInterface::SHIPPING_METHOD_CODE => $methodCode,
71+
],
72+
]);
73+
74+
try {
75+
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
76+
} catch (NoSuchEntityException $e) {
77+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
78+
} catch (LocalizedException $e) {
79+
throw new GraphQlInputException(__($e->getMessage()), $e);
80+
}
81+
}
82+
}

app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Customer\Model\Address\AbstractAddress;
1011
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1112
use Magento\Quote\Api\Data\AddressInterface;
1213
use Magento\Quote\Model\Quote\Address as QuoteAddress;
@@ -40,8 +41,17 @@ public function execute(QuoteAddress $address): array
4041
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
4142
$addressData['model'] = $address;
4243

44+
if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
45+
$addressType = 'SHIPPING';
46+
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
47+
$addressType = 'BILLING';
48+
} else {
49+
$addressType = null;
50+
}
51+
4352
$addressData = array_merge($addressData, [
4453
'address_id' => $address->getId(),
54+
'address_type' => $addressType,
4555
'country' => [
4656
'code' => $address->getCountryId(),
4757
'label' => $address->getCountry()
@@ -51,12 +61,6 @@ public function execute(QuoteAddress $address): array
5161
'label' => $address->getRegion()
5262
],
5363
'street' => $address->getStreet(),
54-
'selected_shipping_method' => [
55-
'code' => $address->getShippingMethod(),
56-
'label' => $address->getShippingDescription(),
57-
'free_shipping' => $address->getFreeShipping(),
58-
'amount' => $address->getShippingAmount()
59-
],
6064
'items_weight' => $address->getWeight(),
6165
'customer_notes' => $address->getCustomerNotes()
6266
]);

app/code/Magento/QuoteGraphQl/Model/Cart/GetCustomerAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1717

1818
/**
19-
* Get customer address. Throws exception if customer is not owner of address
19+
* Get customer address
2020
*/
2121
class GetCustomerAddress
2222
{
@@ -52,7 +52,7 @@ public function execute(int $addressId, int $customerId): AddressInterface
5252
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
5353
);
5454
} catch (LocalizedException $e) {
55-
throw new GraphQlInputException(__($e->getMessage()));
55+
throw new GraphQlInputException(__($e->getMessage()), $e);
5656
}
5757

5858
if ((int)$customerAddress->getCustomerId() !== $customerId) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Quote\Api\Data\AddressInterface;
14+
use Magento\Quote\Api\Data\AddressInterfaceFactory;
15+
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;
16+
17+
/**
18+
* Get quote address
19+
*/
20+
class GetQuoteAddress
21+
{
22+
/**
23+
* @var AddressInterfaceFactory
24+
*/
25+
private $quoteAddressFactory;
26+
27+
/**
28+
* @var AddressResource
29+
*/
30+
private $quoteAddressResource;
31+
32+
/**
33+
* @param AddressInterfaceFactory $quoteAddressFactory
34+
* @param AddressResource $quoteAddressResource
35+
*/
36+
public function __construct(
37+
AddressInterfaceFactory $quoteAddressFactory,
38+
AddressResource $quoteAddressResource
39+
) {
40+
$this->quoteAddressFactory = $quoteAddressFactory;
41+
$this->quoteAddressResource = $quoteAddressResource;
42+
}
43+
44+
/**
45+
* Get quote address
46+
*
47+
* @param int $quoteAddressId
48+
* @param int|null $customerId
49+
* @return AddressInterface
50+
* @throws GraphQlInputException
51+
* @throws GraphQlNoSuchEntityException
52+
* @throws GraphQlAuthorizationException
53+
*/
54+
public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
55+
{
56+
$quoteAddress = $this->quoteAddressFactory->create();
57+
58+
$this->quoteAddressResource->load($quoteAddress, $quoteAddressId);
59+
if (null === $quoteAddress->getId()) {
60+
throw new GraphQlNoSuchEntityException(
61+
__('Could not find a cart address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
62+
);
63+
}
64+
65+
$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();
66+
67+
/* Guest cart, allow operations */
68+
if (!$quoteAddressCustomerId && null === $customerId) {
69+
return $quoteAddress;
70+
}
71+
72+
if ($quoteAddressCustomerId !== $customerId) {
73+
throw new GraphQlAuthorizationException(
74+
__(
75+
'The current user cannot use cart address with ID "%cart_address_id"',
76+
['cart_address_id' => $quoteAddressId]
77+
)
78+
);
79+
}
80+
return $quoteAddress;
81+
}
82+
}

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress):
6060
try {
6161
$quoteAddress->importCustomerAddressData($customerAddress);
6262
} catch (LocalizedException $e) {
63-
throw new GraphQlInputException(__($e->getMessage()));
63+
throw new GraphQlInputException(__($e->getMessage()), $e);
6464
}
6565
return $quoteAddress;
6666
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ public function __construct(
6363
*
6464
* @param ContextInterface $context
6565
* @param CartInterface $cart
66-
* @param array $billingAddress
66+
* @param array $billingAddressInput
6767
* @return void
6868
* @throws GraphQlInputException
6969
* @throws GraphQlAuthenticationException
7070
* @throws GraphQlAuthorizationException
7171
* @throws GraphQlNoSuchEntityException
7272
*/
73-
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
73+
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
7474
{
75-
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
76-
$addressInput = $billingAddress['address'] ?? null;
77-
$useForShipping = isset($billingAddress['use_for_shipping'])
78-
? (bool)$billingAddress['use_for_shipping'] : false;
75+
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
76+
$addressInput = $billingAddressInput['address'] ?? null;
77+
$useForShipping = isset($billingAddressInput['use_for_shipping'])
78+
? (bool)$billingAddressInput['use_for_shipping'] : false;
7979

8080
if (null === $customerAddressId && null === $addressInput) {
8181
throw new GraphQlInputException(

0 commit comments

Comments
 (0)