Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Base shipping methods support #342

Merged
merged 17 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/code/Magento/ConfigurableProductGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<module name="Magento_ConfigurableProduct"/>
<module name="Magento_GraphQl"/>
<module name="Magento_CatalogGraphQl"/>
<module name="Magento_QuoteGraphQl"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's necessary since CartItemDetailsInput field that is used in this module declared within a scope of Magento_QuoteGraphQl

</sequence>
</module>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function execute(QuoteAddress $address): array
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
$addressData['model'] = $address;

if ($address->getShippingMethod()) {
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
$shippingAmount = $address->getShippingAmount();
}

$addressData = array_merge($addressData, [
'country' => [
'code' => $address->getCountryId(),
Expand All @@ -51,9 +56,10 @@ public function execute(QuoteAddress $address): array
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'carrier_code' => $carrierCode ?? null,
'method_code' => $methodCode ?? null,
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
'amount' => $shippingAmount ?? null
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,39 @@ public function __construct(
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls, verify that is proper changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this change is required. Please, take a look at the schema change https://github.com/magento/graphql-ce/pull/342/files#diff-795a33fde881f18aba5165a5a8c7513fR63
We have changed the input in order to reflect the new schema from here https://github.com/magento/graphql-ce/wiki/Checkout-workflow

mutation {
  setShippingMethodOnCart (
    input: {
      cart_id: "vpLKDPvz0F2P2J3ksx97ZcSHUA95a8PX"
      shipping_addresses: [ <!-- Here is the mentioned change
        {
             ...

Thanks.

$shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args);
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
if (!$shippingMethods) {
if (!$shippingAddresses) {
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
}

$shippingMethod = reset($shippingMethods); // This point can be extended for multishipping
$shippingAddress = reset($shippingAddresses); // This point can be extended for multishipping

if (!$shippingMethod['cart_address_id']) {
if (!$shippingAddress['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
if (!isset($shippingAddress['shipping_method'])) {
throw new GraphQlInputException(__('Required parameter "shipping_method" is missing'));
}
if (!$shippingMethod['shipping_method_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
if (!$shippingAddress['shipping_method']['carrier_code']) {
throw new GraphQlInputException(__('Required parameter "carrier_code" is missing'));
}
if (!$shippingAddress['shipping_method']['method_code']) {
throw new GraphQlInputException(__('Required parameter "method_code" is missing'));
}

$userId = $context->getUserId();
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);

$this->setShippingMethodOnCart->execute(
$cart,
$shippingMethod['cart_address_id'],
$shippingMethod['shipping_carrier_code'],
$shippingMethod['shipping_method_code']
$shippingAddress['cart_address_id'],
$shippingAddress['shipping_method']['carrier_code'],
$shippingAddress['shipping_method']['method_code']
);

return [
Expand Down
11 changes: 9 additions & 2 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ input CartAddressInput {

input SetShippingMethodsOnCartInput {
cart_id: String!
shipping_methods: [ShippingMethodForAddressInput!]!
shipping_addresses: [ShippingMethodForAddressInput!]!
}

input ShippingMethodForAddressInput {
cart_address_id: Int!
shipping_method: ShippingMethodInput!
}

input ShippingMethodInput {
carrier_code: String!
method_code: String!
}
Expand Down Expand Up @@ -140,7 +144,10 @@ type CartAddressCountry {
}

type SelectedShippingMethod {
amount: Float!
carrier_code: String
method_code: String
label: String
amount: Float
}

type AvailableShippingMethod {
Expand Down
Loading