Skip to content

Commit 7b52a9b

Browse files
author
Ryan Workman
authored
Merge pull request #202 from Shopify/cart-transform/fix-merge-bug
Fix CartTransform extension sample app
2 parents 67bbd22 + 9386156 commit 7b52a9b

File tree

5 files changed

+197
-35
lines changed

5 files changed

+197
-35
lines changed

checkout/rust/cart-transform/bundles/schema.graphql

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Attribute {
2424
}
2525

2626
"""
27-
Represents information about the buyer that is interacting with the cart.
27+
Represents information about the buyer that is interacting with the cart. Only set when the buyer is logged in.
2828
"""
2929
type BuyerIdentity {
3030
"""
@@ -72,6 +72,11 @@ type Cart {
7272
"""
7373
cost: CartCost!
7474

75+
"""
76+
A list of lines containing information about the items that can be delivered.
77+
"""
78+
deliverableLines: [DeliverableCartLine!]!
79+
7580
"""
7681
The delivery groups available for the cart based on the buyer's shipping address.
7782
"""
@@ -255,6 +260,26 @@ input CartOperation @oneOf {
255260
merge: MergeOperation
256261
}
257262

263+
"""
264+
A customization which applies cart transformations to the merchandise lines.
265+
"""
266+
type CartTransform implements HasMetafields {
267+
"""
268+
Returns a metafield by namespace and key that belongs to the resource.
269+
"""
270+
metafield(
271+
"""
272+
The key for the metafield.
273+
"""
274+
key: String!
275+
276+
"""
277+
The namespace for the metafield.
278+
"""
279+
namespace: String!
280+
): Metafield
281+
}
282+
258283
"""
259284
Represents information about a company which is also a customer of the shop.
260285
"""
@@ -2533,6 +2558,38 @@ Example values: `"29.99"`, `"29.999"`.
25332558
"""
25342559
scalar Decimal
25352560

2561+
"""
2562+
Represents information about the merchandise in the cart.
2563+
"""
2564+
type DeliverableCartLine {
2565+
"""
2566+
Retrieve a cart line attribute by key.
2567+
2568+
Cart line attributes are also known as line item properties in Liquid.
2569+
"""
2570+
attribute(
2571+
"""
2572+
The key of the attribute to retrieve.
2573+
"""
2574+
key: String
2575+
): Attribute
2576+
2577+
"""
2578+
The ID of the cart line.
2579+
"""
2580+
id: ID!
2581+
2582+
"""
2583+
The merchandise that the buyer intends to purchase.
2584+
"""
2585+
merchandise: Merchandise!
2586+
2587+
"""
2588+
The quantity of the merchandise that the customer intends to purchase.
2589+
"""
2590+
quantity: Int!
2591+
}
2592+
25362593
"""
25372594
List of different delivery method types.
25382595
"""
@@ -2710,6 +2767,11 @@ type Input {
27102767
The cart.
27112768
"""
27122769
cart: Cart!
2770+
2771+
"""
2772+
The CartTransform containing the function.
2773+
"""
2774+
cartTransform: CartTransform!
27132775
}
27142776

27152777
"""

checkout/rust/cart-transform/bundles/src/main.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ fn get_merge_cart_operations(cart: &Cart) -> Vec<CartOperation> {
6565
let mut result: Vec<CartOperation> = Vec::new();
6666

6767
for definition in merge_parent_defintions.iter() {
68-
let (components_in_cart, parent_variant_quantity) =
69-
get_components_in_cart(cart, definition);
68+
let components_in_cart = get_components_in_cart(cart, definition);
7069
if components_in_cart.len() == definition.component_reference.len() {
7170
let cart_lines: Vec<CartLineInput> = components_in_cart
7271
.iter()
7372
.map(|component| CartLineInput {
7473
cart_line_id: component.cart_line_id.clone(),
75-
quantity: parent_variant_quantity * component.quantity,
74+
quantity: component.quantity.clone(),
7675
})
7776
.collect();
7877

@@ -104,9 +103,8 @@ fn get_merge_cart_operations(cart: &Cart) -> Vec<CartOperation> {
104103
fn get_components_in_cart(
105104
cart: &Cart,
106105
definition: &ComponentParent,
107-
) -> (Vec<CartLineInput>, i64) {
106+
) -> Vec<CartLineInput> {
108107
let mut line_results: Vec<CartLineInput> = Vec::new();
109-
let mut maximum_available_component: Vec<i64> = Vec::new();
110108
for (reference, quantity) in definition
111109
.component_reference
112110
.iter()
@@ -127,23 +125,13 @@ fn get_components_in_cart(
127125
cart_line_id: line.id.clone(),
128126
quantity: quantity.clone(),
129127
});
130-
let maximum_available = if quantity > &0 {
131-
line.quantity / quantity
132-
} else {
133-
0
134-
};
135-
maximum_available_component.push(maximum_available);
136128
break;
137129
}
138130
}
139131
}
140132
}
141-
let parent_variant_quantity: i64 = match maximum_available_component.iter().min() {
142-
Some(available) => available.clone(),
143-
None => 0,
144-
};
145133

146-
return (line_results, parent_variant_quantity);
134+
return line_results;
147135
}
148136

149137
fn get_merge_parent_definitions(cart: &Cart) -> Vec<ComponentParent> {

checkout/rust/cart-transform/default/schema.graphql

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Attribute {
2424
}
2525

2626
"""
27-
Represents information about the buyer that is interacting with the cart.
27+
Represents information about the buyer that is interacting with the cart. Only set when the buyer is logged in.
2828
"""
2929
type BuyerIdentity {
3030
"""
@@ -72,6 +72,11 @@ type Cart {
7272
"""
7373
cost: CartCost!
7474

75+
"""
76+
A list of lines containing information about the items that can be delivered.
77+
"""
78+
deliverableLines: [DeliverableCartLine!]!
79+
7580
"""
7681
The delivery groups available for the cart based on the buyer's shipping address.
7782
"""
@@ -255,6 +260,26 @@ input CartOperation @oneOf {
255260
merge: MergeOperation
256261
}
257262

263+
"""
264+
A customization which applies cart transformations to the merchandise lines.
265+
"""
266+
type CartTransform implements HasMetafields {
267+
"""
268+
Returns a metafield by namespace and key that belongs to the resource.
269+
"""
270+
metafield(
271+
"""
272+
The key for the metafield.
273+
"""
274+
key: String!
275+
276+
"""
277+
The namespace for the metafield.
278+
"""
279+
namespace: String!
280+
): Metafield
281+
}
282+
258283
"""
259284
Represents information about a company which is also a customer of the shop.
260285
"""
@@ -2533,6 +2558,38 @@ Example values: `"29.99"`, `"29.999"`.
25332558
"""
25342559
scalar Decimal
25352560

2561+
"""
2562+
Represents information about the merchandise in the cart.
2563+
"""
2564+
type DeliverableCartLine {
2565+
"""
2566+
Retrieve a cart line attribute by key.
2567+
2568+
Cart line attributes are also known as line item properties in Liquid.
2569+
"""
2570+
attribute(
2571+
"""
2572+
The key of the attribute to retrieve.
2573+
"""
2574+
key: String
2575+
): Attribute
2576+
2577+
"""
2578+
The ID of the cart line.
2579+
"""
2580+
id: ID!
2581+
2582+
"""
2583+
The merchandise that the buyer intends to purchase.
2584+
"""
2585+
merchandise: Merchandise!
2586+
2587+
"""
2588+
The quantity of the merchandise that the customer intends to purchase.
2589+
"""
2590+
quantity: Int!
2591+
}
2592+
25362593
"""
25372594
List of different delivery method types.
25382595
"""
@@ -2710,6 +2767,11 @@ type Input {
27102767
The cart.
27112768
"""
27122769
cart: Cart!
2770+
2771+
"""
2772+
The CartTransform containing the function.
2773+
"""
2774+
cartTransform: CartTransform!
27132775
}
27142776

27152777
"""

sample-apps/bundles-cart-transform/extensions/cart-merge-expand/schema.graphql

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Attribute {
2424
}
2525

2626
"""
27-
Represents information about the buyer that is interacting with the cart.
27+
Represents information about the buyer that is interacting with the cart. Only set when the buyer is logged in.
2828
"""
2929
type BuyerIdentity {
3030
"""
@@ -72,6 +72,11 @@ type Cart {
7272
"""
7373
cost: CartCost!
7474

75+
"""
76+
A list of lines containing information about the items that can be delivered.
77+
"""
78+
deliverableLines: [DeliverableCartLine!]!
79+
7580
"""
7681
The delivery groups available for the cart based on the buyer's shipping address.
7782
"""
@@ -255,6 +260,26 @@ input CartOperation @oneOf {
255260
merge: MergeOperation
256261
}
257262

263+
"""
264+
A customization which applies cart transformations to the merchandise lines.
265+
"""
266+
type CartTransform implements HasMetafields {
267+
"""
268+
Returns a metafield by namespace and key that belongs to the resource.
269+
"""
270+
metafield(
271+
"""
272+
The key for the metafield.
273+
"""
274+
key: String!
275+
276+
"""
277+
The namespace for the metafield.
278+
"""
279+
namespace: String!
280+
): Metafield
281+
}
282+
258283
"""
259284
Represents information about a company which is also a customer of the shop.
260285
"""
@@ -2533,6 +2558,38 @@ Example values: `"29.99"`, `"29.999"`.
25332558
"""
25342559
scalar Decimal
25352560

2561+
"""
2562+
Represents information about the merchandise in the cart.
2563+
"""
2564+
type DeliverableCartLine {
2565+
"""
2566+
Retrieve a cart line attribute by key.
2567+
2568+
Cart line attributes are also known as line item properties in Liquid.
2569+
"""
2570+
attribute(
2571+
"""
2572+
The key of the attribute to retrieve.
2573+
"""
2574+
key: String
2575+
): Attribute
2576+
2577+
"""
2578+
The ID of the cart line.
2579+
"""
2580+
id: ID!
2581+
2582+
"""
2583+
The merchandise that the buyer intends to purchase.
2584+
"""
2585+
merchandise: Merchandise!
2586+
2587+
"""
2588+
The quantity of the merchandise that the customer intends to purchase.
2589+
"""
2590+
quantity: Int!
2591+
}
2592+
25362593
"""
25372594
List of different delivery method types.
25382595
"""
@@ -2710,6 +2767,11 @@ type Input {
27102767
The cart.
27112768
"""
27122769
cart: Cart!
2770+
2771+
"""
2772+
The CartTransform containing the function.
2773+
"""
2774+
cartTransform: CartTransform!
27132775
}
27142776

27152777
"""

0 commit comments

Comments
 (0)