Not able to auto generate discount code using API of Shopify

Highlighted
Shopify Partner
2 0 0

I am trying to autogenerate a discount code after sharing an Image on Facebook, but the discount code will destroy after one day. I have tried using the API guide line of shopify but unable to POST the required data using the API provided by shopify. Can anyone help me out of this problem?

0 Likes
Highlighted
Community Moderator
Community Moderator
3073 231 502

Hi there, 

Nick here from Shopify. 

Would you be able to elaborate a little more on what you mean, please? You can fully edit the discount codes in the Shopify admin and adjust the time so it doesn't expire after one day. If you are working with the API on autogenerating the codes, it does sound like it is something very customized. Because of this, I would suggest to reach out to a Shopify expert about it. A Shopify expert could work with you directly on getting this to work with you as it seems out of the scope of the admin or an app. You can see the Shopify Expert list directory here

Cheers, Nick

Nick | Community Moderator @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

0 Likes
Highlighted
Shopify Partner
2 0 0

Hi Nick,

I don't think that I have done anything which is out of scope of Shopify. I have just followed the guidelines of shopify i.e https://help.shopify.com/en/api/reference/discounts/discountcode#lookup and https://help.shopify.com/en/api/reference/discounts/pricerule.

You can check my code also

         var d = new Date();
         var n = d.toISOString();
         var data='{"price_rule": {"title": "5OFFCUSTOMERGROUP","target_type": "shipping_line","target_selection": "all","allocation_method": "each","value_type": "percentage","value": "-100.0","usage_limit": null,"customer_selection": "all","prerequisite_subtotal_range": {"greater_than_or_equal_to": "50.0"},"starts_at":"'+n+'" } }';

 var ence = 'Basic '+btoa(unescape(encodeURIComponent(+'83cfab0443d41acfc2ebfd5d9c5a0649:3f4583069e8493d93e9f950850860997')));

            var xhttp = new XMLHttpRequest();
            xhttp.open("POST", "/admin/price_rules.json",true);
            xhttp.setRequestHeader("Content-type","application/json");
            xhttp.setRequestHeader("Authorization",ence);
            var input = data;
            xhttp.send(input);

can you please check and tell me if I'm doing anything wrong here ?

Thanks.        

 

0 Likes
Highlighted
Community Moderator
Community Moderator
3073 231 502

Hi again, 

Working within the API is not something I am an expert in or comfortable with. It could be worth asking this in the API discussion forum to see if someone who has expertise in this can confirm this for you or as I mentioned in my original post, reaching out to a Shopify expert to work on this with you. An expert or someone in the API discussion forum would be a lot more suited to seeing if this is correct for you. 

All the best, Nick

Nick | Community Moderator @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

0 Likes