Discussing Shopify Functions development, deployment, and usage in Shopify apps.
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:
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,
})
}
#[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,
}
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
}
}
}
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.
Solved! Go to the solution
This is an accepted solution.
Hi Adkhamllama,
I am an engineer on the Discounts team. Thank you for the report and all the info you provided.
I can confirm that we were able to reproduce the error. If the discount is set to apply to only one item. When selecting 2 items of the same product variant, but with a different product attribute, the discount will not apply to either of them.
Our team will be looking at it.
To learn more visit the Shopify Help Center or the Community Blog.
This is an accepted solution.
Hi Adkhamllama,
I am an engineer on the Discounts team. Thank you for the report and all the info you provided.
I can confirm that we were able to reproduce the error. If the discount is set to apply to only one item. When selecting 2 items of the same product variant, but with a different product attribute, the discount will not apply to either of them.
Our team will be looking at it.
To learn more visit the Shopify Help Center or the Community Blog.
Hi, sorry to bother you but I'm facing the same problem I guess. I only apply discount if the product has attribute "_x" for exemple, it works partially but if I apply the discount to the product that has this attribute and I add the same product without this attribute on cart the discount will be added to the product without attribute too (if I only add to cart the product without the attribute, discount is not applied). Here is some of my code:
I hope you can help me. Thanks!
The target for discount can be only product variant, so the discount is added to this variant id, not the line id. I think that is why the discount is applied to all products that has the same variant id, but not same attribute (sorry for my english, I hope you understand it).
Hello. I'm facing a problem that looks like yours... I was wondering, do you add this attribute to the product with an input hidden named 'attributes[attributeName]'?
Hello Gabrielvictoria
No, I was adding it via JavaScript when adding a product to the cart.
But the bug I was facing was resolved by Shopify team