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

graphQl-890: Replaced usage of the CartItemQuantity with the CartItem… #899

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,25 @@ public function execute(QuoteAddress $address): array
return $addressData;
}

$addressItemsData = [];
foreach ($address->getAllItems() as $addressItem) {
if ($addressItem instanceof \Magento\Quote\Model\Quote\Item) {
$itemId = $addressItem->getItemId();
} else {
$itemId = $addressItem->getQuoteItemId();
}

$addressItemsData[] = [
$productData = $addressItem->getProduct()->getData();
$productData['model'] = $addressItem->getProduct();
$addressData['cart_items'][] = [
'cart_item_id' => $itemId,
'quantity' => $addressItem->getQty()
];
$addressData['cart_items_v2'][] = [
'id' => $itemId,
'quantity' => $addressItem->getQty(),
'product' => $productData,
'model' => $addressItem,
];
}
$addressData['cart_items'] = $addressItemsData;

return $addressData;
}
}
11 changes: 6 additions & 5 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,19 @@ interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Mo
type ShippingCartAddress implements CartAddressInterface {
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\AvailableShippingMethods")
selected_shipping_method: SelectedShippingMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\SelectedShippingMethod")
items_weight: Float
cart_items: [CartItemQuantity]
customer_notes: String
items_weight: Float @deprecated(reason: "This information shoud not be exposed on frontend")
cart_items: [CartItemQuantity] @deprecated(reason: "`cart_items_v2` should be used instead")
cart_items_v2: [CartItemInterface]
}

type BillingCartAddress implements CartAddressInterface {
customer_notes: String @deprecated (reason: "The field is used only in shipping address")
}

type CartItemQuantity {
cart_item_id: Int!
quantity: Float!
type CartItemQuantity @doc(description:"Deprecated: `cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`") {
cart_item_id: Int! @deprecated(reason: "`cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`")
quantity: Float! @deprecated(reason: "`cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`")
}

type CartAddressRegion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,36 @@ public function testAddSimpleProductToCart()
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
self::assertArrayHasKey('shipping_addresses', $response['addSimpleProductsToCart']['cart']);
self::assertEmpty($response['addSimpleProductsToCart']['cart']['shipping_addresses']);
self::assertEquals($quantity, $response['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
self::assertArrayHasKey('prices', $response['addSimpleProductsToCart']['cart']['items'][0]);

self::assertArrayHasKey('price', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$price = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['price'];
self::assertArrayHasKey('value', $price);
self::assertEquals(10, $price['value']);
self::assertArrayHasKey('currency', $price);
self::assertEquals('USD', $price['currency']);

self::assertArrayHasKey('row_total', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$rowTotal = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total'];
self::assertArrayHasKey('value', $rowTotal);
self::assertEquals(20, $rowTotal['value']);
self::assertArrayHasKey('currency', $rowTotal);
self::assertEquals('USD', $rowTotal['currency']);

self::assertArrayHasKey(
'row_total_including_tax',
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']
);
$rowTotalIncludingTax =
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total_including_tax'];
self::assertArrayHasKey('value', $rowTotalIncludingTax);
self::assertEquals(20, $rowTotalIncludingTax['value']);
self::assertArrayHasKey('currency', $rowTotalIncludingTax);
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
}

/**
Expand Down Expand Up @@ -262,6 +290,34 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity):
product {
sku
}
prices {
price {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
shipping_addresses {
firstname
lastname
company
street
city
postcode
telephone
country {
code
label
}
__typename
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,36 @@ public function testAddSimpleProductToCart()
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);

self::assertArrayHasKey('shipping_addresses', $response['addSimpleProductsToCart']['cart']);
self::assertEmpty($response['addSimpleProductsToCart']['cart']['shipping_addresses']);
self::assertEquals($quantity, $response['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
self::assertArrayHasKey('prices', $response['addSimpleProductsToCart']['cart']['items'][0]);

self::assertArrayHasKey('price', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$price = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['price'];
self::assertArrayHasKey('value', $price);
self::assertEquals(10, $price['value']);
self::assertArrayHasKey('currency', $price);
self::assertEquals('USD', $price['currency']);

self::assertArrayHasKey('row_total', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
$rowTotal = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total'];
self::assertArrayHasKey('value', $rowTotal);
self::assertEquals(20, $rowTotal['value']);
self::assertArrayHasKey('currency', $rowTotal);
self::assertEquals('USD', $rowTotal['currency']);

self::assertArrayHasKey(
'row_total_including_tax',
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']
);
$rowTotalIncludingTax =
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total_including_tax'];
self::assertArrayHasKey('value', $rowTotalIncludingTax);
self::assertEquals(20, $rowTotalIncludingTax['value']);
self::assertArrayHasKey('currency', $rowTotalIncludingTax);
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
}

/**
Expand Down Expand Up @@ -231,6 +259,34 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity):
product {
sku
}
prices {
price {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
shipping_addresses {
firstname
lastname
company
street
city
postcode
telephone
country {
code
label
}
__typename
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function testGetAvailableShippingMethods()
$expectedAddressData,
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
);
self::assertCount(1, $response['cart']['shipping_addresses'][0]['cart_items']);
self::assertCount(1, $response['cart']['shipping_addresses'][0]['cart_items_v2']);
self::assertEquals(
'simple_product',
$response['cart']['shipping_addresses'][0]['cart_items_v2'][0]['product']['sku']
);
}

/**
Expand Down Expand Up @@ -140,6 +146,13 @@ private function getQuery(string $maskedQuoteId): string
cart_item_id
quantity
}
cart_items_v2 {
id
quantity
product {
sku
}
}
available_shipping_methods {
amount {
value
Expand Down