Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Custom discount / graphql limits

Custom discount / graphql limits

dan-hrpr
Shopify Partner
10 0 2

I have created a discount app that uses a resource picker to only apply the discount to selected products.

 

The problem is that when I select more than 4 products I get an error generated from by the Shopify function.

 

Error parsing selectedProducts: TypeError: cannot read property 'reduce' of undefined

It works fine with any combination of variants and in the admin interface the product list saves with more than 4 products selected so the products are being stored in the metafield there's just a problem when the discount function retrieves them when there's more than 4.

 

I've tried to get the discount metfields from GRaphicQL but I couldn't work it out.

here's my input query for the discount function.

 

 

query Input {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ...on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      email
      customer{
        metafield(namespace: "loyalty-discount", key: "loyalty_tier") {
          value
        }
      }
    }
  }
  discountNode {
    functionConfiguration: metafield(namespace: "$app:loyalty-discount", key: "function-configuration") {
      value
    }
    selectedProducts: metafield(namespace: "$app:loyalty-discount", key: "selected-products") {
      value
    }
  }
}

 

 


Thanks Dan

 

Replies 4 (4)

tobebuilds
Shopify Partner
593 42 159

Hi Dan-Hrpr,

 

This doesn't seem like you're running into a limit (e.g. instruction count limit or input query limit).

 

Specifically, the error message "TypeError: cannot read property 'reduce' of undefined" makes me think it's a JS runtime error.

 

If you console.error the input when your function runs, you might be able to identify the cause. Maybe `selectedProducts`, or one of its properties, is undefined.

 

Best,

Tobe

Founder, Regios Discounts app (4.8 stars, 93 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer
- Often imitated, never duplicated
dan-hrpr
Shopify Partner
10 0 2

Hi Tobe,

It is undefined but only when I save more than 4 products in it.

Cheers Dan

tobebuilds
Shopify Partner
593 42 159

You might be running into the 10kb metafield size limit, then. I recommend only storing the IDs of products. I don't know if you are storing other information about products in the metafield, but it fills up fast.

Founder, Regios Discounts app (4.8 stars, 93 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer
- Often imitated, never duplicated
dan-hrpr
Shopify Partner
10 0 2

I did think that but it works in the backend in the product selector.

 

So if I add 20 products in the product selector and then save the discount then when I edit the discount again the 20 products are still selected.

 

I will try and save the variant id's only like you suggest.