I am creating app with Shopify Functions for applying discounts with Product Discount API .
The project template was taken from https://github.com/Shopify/function-examples/tree/main/sample-apps/discounts-tutorial (Code I will be talking about is placed in /extentions/volume/src/main.rs)
While developing I have noticed a bug, that discounts are not applied when cart contains 2 items of same product and same variant, but has different attributes.
Steps to reproduce the bug:
-
Add the following code to /extentions/volume/src/main.rs
use std::process; ... fn function(input: input::Input) -> Result<FunctionResult, Box<dyn std::error::Error>> { let config: input::Configuration = input.configuration(); let cart_lines = input.cart.lines; let mut targets = vec![]; for line in &cart_lines { if line.attribute.is_none() == true { continue; } targets.push(Target::ProductVariant { id: line.merchandise.id.as_ref().unwrap().to_string(), quantity: 1 }); } if targets.is_empty() { return Ok(FunctionResult { discounts: vec![], discount_application_strategy: DiscountApplicationStrategy::First, }); } eprintln!("targets: {:?}", targets); // process::exit(1); // added for debugging Ok(FunctionResult { discounts: vec![Discount { message: None, conditions: None, targets, value: Value::Percentage(Percentage { value: config.percentage }), }], discount_application_strategy: DiscountApplicationStrategy::Maximum, }) } -
Update CartLine in api.rs
#[derive(Clone, Debug, Deserialize)] pub struct CartLine { pub quantity: Int, pub merchandise: Merchandise, pub attribute: Option<CartLineAttribute>, } #[derive(Clone, Debug, Deserialize)] pub struct CartLineAttribute { pub value: String, } -
Update input.graphql
query Input { cart { lines { quantity attribute(key: "YOUR_ATTRIBUTE_KEY") { value } merchandise { ...on ProductVariant { id } } } } discountNode { metafield(namespace: "discounts-tutorial-app", key: "function-config") { value } } } -
Create discount from App
-
Add product with attribute (Discount is applied)
-
Add same product without attribute (Here the bug comes)
After we add product without attribute the discount is not applied at all (neither to first or second item).
I have checked that code in main.rs produces valid output by logging targets:
targets: [ProductVariant { id: "gid://shopify/ProductVariant/40125286744188", quantity: 1 }]
Possible reason for bug:
I suppose the problem is in target variant id. Currently there are 2 items in cart with same variant id. Maybe script responsible for applying discounts is failing to identify which line item should get a discount.
This does not happen if items are different, or if I increase quantity of items in cart (adding same item with attribute)
I donāt know where or whom to address this issue, so I have created it here.
