Skip to content

Commit 192c6d1

Browse files
author
Yevhen Miroshnychenko
committed
Merge branch '2.3-develop' into MAGETWO-94241
2 parents f5952a3 + 5276b27 commit 192c6d1

File tree

17 files changed

+995
-26
lines changed

17 files changed

+995
-26
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Variant/Attributes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1111
use Magento\Framework\GraphQl\Query\Resolver\Value;
12+
use Magento\Catalog\Model\Product;
1213
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1314
use Magento\Framework\GraphQl\Config\Element\Field;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -45,12 +46,14 @@ public function resolve(
4546
$data = [];
4647
foreach ($value['options'] as $option) {
4748
$code = $option['attribute_code'];
48-
if (!isset($value['product']['model'][$code])) {
49+
/** @var Product|null $model */
50+
$model = $value['product']['model'] ?? null;
51+
if (!$model || !$model->getData($code)) {
4952
continue;
5053
}
5154

5255
foreach ($option['values'] as $optionValue) {
53-
if ($optionValue['value_index'] != $value['product']['model'][$code]) {
56+
if ($optionValue['value_index'] != $model->getData($code)) {
5457
continue;
5558
}
5659
$data[] = [

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
3+
type Mutation {
4+
addConfigurableProductsToCart(input: AddConfigurableProductsToCartInput): AddConfigurableProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
5+
}
36

47
type ConfigurableProduct implements ProductInterface, PhysicalProductInterface, CustomizableProductInterface @doc(description: "ConfigurableProduct defines basic features of a configurable product and its simple product variants") {
58
variants: [ConfigurableVariant] @doc(description: "An array of variants of products") @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableVariant")
@@ -35,3 +38,30 @@ type ConfigurableProductOptionsValues @doc(description: "ConfigurableProductOpti
3538
store_label: String @doc(description: "The label of the product on the current store")
3639
use_default_value: Boolean @doc(description: "Indicates whether to use the default_label")
3740
}
41+
42+
input AddConfigurableProductsToCartInput {
43+
cart_id: String!
44+
cartItems: [ConfigurableProductCartItemInput!]!
45+
}
46+
47+
type AddConfigurableProductsToCartOutput {
48+
cart: Cart!
49+
}
50+
51+
input ConfigurableProductCartItemInput {
52+
data: CartItemDetailsInput!
53+
variant_sku: String!
54+
customizable_options:[CustomizableOptionInput!]
55+
}
56+
57+
type ConfigurableCartItem implements CartItemInterface {
58+
customizable_options: [SelectedCustomizableOption]!
59+
configurable_options: [SelectedConfigurableOption!]!
60+
}
61+
62+
type SelectedConfigurableOption {
63+
id: Int!
64+
option_label: String!
65+
value_id: Int!
66+
value_label: String!
67+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function __construct(
4545
* @param Quote $cart
4646
* @param array $cartItems
4747
* @throws GraphQlInputException
48+
* @throws \Magento\Framework\Exception\LocalizedException
49+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
4850
*/
4951
public function execute(Quote $cart, array $cartItems): void
5052
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct(
6161
* @return void
6262
* @throws GraphQlNoSuchEntityException
6363
* @throws GraphQlInputException
64+
* @throws \Magento\Framework\Exception\LocalizedException
6465
*/
6566
public function execute(Quote $cart, array $cartItemData): void
6667
{
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Address;
9+
10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;
15+
16+
/**
17+
* Collect and return information about a billing address
18+
*/
19+
class BillingAddressDataProvider
20+
{
21+
/**
22+
* @var CartRepositoryInterface
23+
*/
24+
private $cartRepository;
25+
26+
/**
27+
* @var Address
28+
*/
29+
private $addressMapper;
30+
31+
/**
32+
* @var ExtensibleDataObjectConverter
33+
*/
34+
private $dataObjectConverter;
35+
36+
/**
37+
* AddressDataProvider constructor.
38+
*
39+
* @param CartRepositoryInterface $cartRepository
40+
* @param Address $addressMapper
41+
* @param ExtensibleDataObjectConverter $dataObjectConverter
42+
*/
43+
public function __construct(
44+
CartRepositoryInterface $cartRepository,
45+
Address $addressMapper,
46+
ExtensibleDataObjectConverter $dataObjectConverter
47+
) {
48+
$this->cartRepository = $cartRepository;
49+
$this->addressMapper = $addressMapper;
50+
$this->dataObjectConverter = $dataObjectConverter;
51+
}
52+
53+
/**
54+
* Collect and return information about a billing addresses
55+
*
56+
* @param CartInterface $cart
57+
* @return null|array
58+
*/
59+
public function getCartAddresses(CartInterface $cart): ?array
60+
{
61+
$cart = $this->cartRepository->get($cart->getId());
62+
$billingAddress = $cart->getBillingAddress();
63+
64+
if (!$billingAddress) {
65+
return null;
66+
}
67+
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
68+
$addressData = array_merge($billingData, $this->addressMapper->toNestedArray($billingAddress));
69+
70+
return $addressData;
71+
}
72+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Address\Mapper;
9+
10+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
11+
12+
/**
13+
* Class Address
14+
*
15+
* Extract the necessary address fields from an Address model
16+
*/
17+
class Address
18+
{
19+
/**
20+
* Converts Address model data to nested array
21+
*
22+
* @param QuoteAddress $address
23+
* @return array
24+
*/
25+
public function toNestedArray(QuoteAddress $address): array
26+
{
27+
$addressData = [
28+
'country' => [
29+
'code' => $address->getCountryId(),
30+
'label' => $address->getCountry()
31+
],
32+
'region' => [
33+
'code' => $address->getRegionCode(),
34+
'label' => $address->getRegion()
35+
],
36+
'street' => $address->getStreet(),
37+
'selected_shipping_method' => [
38+
'code' => $address->getShippingMethod(),
39+
'label' => $address->getShippingDescription(),
40+
'free_shipping' => $address->getFreeShipping(),
41+
],
42+
'items_weight' => $address->getWeight(),
43+
'customer_notes' => $address->getCustomerNotes()
44+
];
45+
46+
if (!$address->hasItems()) {
47+
return $addressData;
48+
}
49+
50+
$addressItemsData = [];
51+
foreach ($address->getAllItems() as $addressItem) {
52+
$addressItemsData[] = [
53+
'cart_item_id' => $addressItem->getQuoteItemId(),
54+
'quantity' => $addressItem->getQty()
55+
];
56+
}
57+
$addressData['cart_items'] = $addressItemsData;
58+
59+
return $addressData;
60+
}
61+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Address;
9+
10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;
15+
16+
/**
17+
* Class AddressDataProvider
18+
*
19+
* Collect and return information about cart shipping and billing addresses
20+
*/
21+
class ShippingAddressesDataProvider
22+
{
23+
/**
24+
* @var ExtensibleDataObjectConverter
25+
*/
26+
private $dataObjectConverter;
27+
28+
/**
29+
* @var CartRepositoryInterface
30+
*/
31+
private $cartRepository;
32+
33+
/**
34+
* @var Address
35+
*/
36+
private $addressMapper;
37+
38+
/**
39+
* AddressDataProvider constructor.
40+
*
41+
* @param ExtensibleDataObjectConverter $dataObjectConverter
42+
* @param CartRepositoryInterface $cartRepository
43+
* @param Address $addressMapper
44+
*/
45+
public function __construct(
46+
ExtensibleDataObjectConverter $dataObjectConverter,
47+
CartRepositoryInterface $cartRepository,
48+
Address $addressMapper
49+
) {
50+
$this->dataObjectConverter = $dataObjectConverter;
51+
$this->cartRepository = $cartRepository;
52+
$this->addressMapper = $addressMapper;
53+
}
54+
55+
/**
56+
* Collect and return information about shipping addresses
57+
*
58+
* @param CartInterface $cart
59+
* @return array
60+
*/
61+
public function getCartAddresses(CartInterface $cart): array
62+
{
63+
$cart = $this->cartRepository->get($cart->getId());
64+
$addressData = [];
65+
$shippingAddresses = $cart->getAllShippingAddresses();
66+
67+
if ($shippingAddresses) {
68+
foreach ($shippingAddresses as $shippingAddress) {
69+
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
70+
$addressData[] = array_merge($shippingData, $this->addressMapper->toNestedArray($shippingAddress));
71+
}
72+
}
73+
74+
return $addressData;
75+
}
76+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\Customer\Api\Data\AddressInterface;
11+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Quote\Api\Data\CartInterface;
15+
use Magento\Quote\Model\Quote\Address;
16+
use Magento\Quote\Api\BillingAddressManagementInterface;
17+
use Magento\Customer\Api\AddressRepositoryInterface;
18+
19+
/**
20+
* Set billing address for a specified shopping cart
21+
*/
22+
class SetBillingAddressOnCart
23+
{
24+
/**
25+
* @var BillingAddressManagementInterface
26+
*/
27+
private $billingAddressManagement;
28+
29+
/**
30+
* @var AddressRepositoryInterface
31+
*/
32+
private $addressRepository;
33+
34+
/**
35+
* @var Address
36+
*/
37+
private $addressModel;
38+
39+
/**
40+
* @var CheckCustomerAccount
41+
*/
42+
private $checkCustomerAccount;
43+
44+
/**
45+
* @param BillingAddressManagementInterface $billingAddressManagement
46+
* @param AddressRepositoryInterface $addressRepository
47+
* @param Address $addressModel
48+
* @param CheckCustomerAccount $checkCustomerAccount
49+
*/
50+
public function __construct(
51+
BillingAddressManagementInterface $billingAddressManagement,
52+
AddressRepositoryInterface $addressRepository,
53+
Address $addressModel,
54+
CheckCustomerAccount $checkCustomerAccount
55+
) {
56+
$this->billingAddressManagement = $billingAddressManagement;
57+
$this->addressRepository = $addressRepository;
58+
$this->addressModel = $addressModel;
59+
$this->checkCustomerAccount = $checkCustomerAccount;
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
66+
{
67+
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
68+
$addressInput = $billingAddress['address'] ?? null;
69+
$useForShipping = $billingAddress['use_for_shipping'] ?? false;
70+
71+
if (null === $customerAddressId && null === $addressInput) {
72+
throw new GraphQlInputException(
73+
__('The billing address must contain either "customer_address_id" or "address".')
74+
);
75+
}
76+
if ($customerAddressId && $addressInput) {
77+
throw new GraphQlInputException(
78+
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
79+
);
80+
}
81+
$addresses = $cart->getAllShippingAddresses();
82+
if ($useForShipping && count($addresses) > 1) {
83+
throw new GraphQlInputException(
84+
__('Using the "use_for_shipping" option with multishipping is not possible.')
85+
);
86+
}
87+
if (null === $customerAddressId) {
88+
$billingAddress = $this->addressModel->addData($addressInput);
89+
} else {
90+
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
91+
92+
/** @var AddressInterface $customerAddress */
93+
$customerAddress = $this->addressRepository->getById($customerAddressId);
94+
$billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
95+
}
96+
97+
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
98+
}
99+
}

0 commit comments

Comments
 (0)