d
Topic
ShaneP Member
Posts:
3
May 27, 2013

Available admin features reference documentation This post is outdated

Hello,

I am hunting for information on creating coupon codes. I found this post http://ecommerce.shopify.com/c/shopify-apis-and-technology/t/creating-a-coupon-code-through-curl-isn-t-working-anymore-139445

Thomas is using myshopify.com/admin/discounts.json I cant seem to find anything in the API reference talking about /admin/discounts.json.

Could someone kindly let me know where I should be looking?

 

Thank you

 

i
Replies
Posts:
3732
May 27, 2013

You are barking up the wrong tree at this time. There is no discounts API. There is a free Shopify App for creating discount codes, and if you're handy with writing code, you can automate the process with Mechanize, curl or even selenium etc. 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
ShaneP Member
Posts:
3
May 27, 2013

Hi HunkyBill, thanks for you response.

I dont know perl but it looks like they are using the api to create the coupon?

#!/usr/bin/perl

use strict;
use warnings;

use WWW::Shopify;

package WWW::Shopify::Model::Discount;
use parent "WWW::Shopify::Model::Item";

my $fields; sub fields { return $fields; } 
BEGIN { $fields = {
    "applies_once" => new WWW::Shopify::Field::Boolean(),
    "code" => new WWW::Shopify::Field::String(),
    "ends_at" => new WWW::Shopify::Field::Date(min => '2010-01-01 00:00:00', max => 'now'),
    "starts_at" => new WWW::Shopify::Field::Date(min => '2010-01-01 00:00:00', max => 'now'),
    "id" => new WWW::Shopify::Field::Identifier(),
    "minimum_order_amount" => new WWW::Shopify::Field::Money(),
    "status" => new WWW::Shopify::Field::String::Enum(["enabled", "disabled"]),
    "usage_limit" => new WWW::Shopify::Field::Int(),
    "value" => new WWW::Shopify::Field::Money(),
    "discount_type" => new WWW::Shopify::Field::String::Enum(["fixed_amount", "percentage"]),
    "applies_to_resource" => new WWW::Shopify::Field::String::Enum(["order", "product", "collection", "customer_group"]),
    "times_used" => new WWW::Shopify::Field::Int(),
    "applies_to_id" => new WWW::Shopify::Field::Relation::ReferenceOne("WWW::Shopify::Model::Product")
}; }

sub creation_minimal { return qw(discount_type code value); }
sub creation_filled { return qw(status); }
sub update_filled { return qw(); }

sub actions { return qw(disable enable); }

sub needs_login { return 1; }

eval(__PACKAGE__->generate_accessors); die $@ if $@;

1;

You mentioned there is a free app. What's it called? I am trying to code the automatation of discount codes so any info would be apperiated. 

Posts:
3732
May 27, 2013
g
1
upvotes

Shopify offers a FREE App to make all the discount codes you'll ever need. Check out their Apps page.. 

And that code you're looking at is Perl code that fakes being a person.. it is NOT using any API.. it is pretending to be a person.. this is Mechanize... 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Adam Harrison Member
Posts:
127
Last edited May 28, 2013

Hi Shane,

I'm the one who wrote that Perl code. HunkyBill is right. The API for discounts is not technically available to the public, but it does follow the same rules as the rest of their API. So while it looks like the official API, it isn't, and won't work with a normal access token; the system has to first authenticate itself as a user.

What specifically are you trying to do? If this needs to be a one-time thing, HunkyBill is right, that you're way better off using the free app, as it's official and supported. If you absolutely must create discount codes on the fly, you can do it using the snippet that I posted in that thread, and WWW::Shopify (you can even call it from Ruby, though it'd be somewhat cumbersome, and slow; you can even write Ruby inside Perl, if you want with Inline::Ruby)

 

Adam

ShaneP Member
Posts:
3
May 28, 2013

Thanks Adam,

I work for a loyalty based company as was wanting to build an app that let us generate coupon codes for users of the app.

I'm a .net developer so the ruby and perl stuff wont help me much but I can follow through your snippet for what's happening.

Do you mean I won't be able to pass the oauth key and secret it the requests header to authenicate? I don't think this will work if I need to know a username and password.

 

 

 

Adam Harrison Member
Posts:
127
Last edited May 28, 2013

Hi Shane,

Yes, that's eactly what I'm saying. Unfortunately, you  need to have the username and password; it's a limitation of this approach. Ben Crudo Consulting/eShop Admin (where I work) has actually just recently developed a loyalty app that generates discount codes, and we're about to hit unofficial beta in a week or two (apps that use unofficial methods like this of modifying a customer's store generally aren't allowed in the app store, and with good reason). If Shopify releases their discount API, then things'd be different, but unfortunately, we don't really know when this is going to happen, so we're kind of getting around the limitation, for now.

So if you need something similar to this approach, we may have actually already just built it (I deployed today). If you're interested in using the app ( is the comapny you work for a retailer, or a software provider?) , drop me an email at adam@eshopadmin.com with your list of requirements, and I'll let you know if we've got something that's compatible.

Thanks,

Adam

Posts:
3732
May 28, 2013

I love the quote up there stating Ruby would be "cumbersome" and "slow". 

I will put up a pink slip on my car that not only is the speed issue senseless in that Mechanize in any language will go at the speed of the network and not be measurabley hindered by the scripting code, but that the Ruby not only reads and looks so much nicer than Perl, but is easier to test, develop and deploy. Where is your Heroku mr. perl... oh. ya.. you have to deploy your own server... snicker snicker... 

Perl is a valid scripting language. I go back to the 1970's writing code, and as a software engineer, I will give you all the benefit of the doubt that you can function fine writing in Perl. But seriously. It is 2013 and Perl syntax is as ugly as ever, living in the 80's... Remember neon clothes, Trans-ams, Rotten Ronnie Reagan and Thatcher? That is Perl baby. Of all the scripting languages in major use, it deserves it's 9/10 for ugly. It works. But it's ugly. We all had to deal with it at one point, and most of us moved on, for good reason!!

 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Adam Harrison Member
Posts:
127
Last edited May 29, 2013

Hi HunkyBill,

You're misunderstanding what I'm saying. I'm not saying that Ruby is slow (it's probably slightly faster than Perl in most instances; most of the time AFAIK, I think they're probably around the same speed). So when I'm saying "you can do it using the snippet that I posted in that thread, and WWW::Shopify (you can even call it from Ruby, though it'd be somewhat cumbersome, and slow", I mean that you can use the snippet that I posted in that thread which is:

perl -MWWW::Shopify -e '$sa = new WWW::Shopify("armstrong-morar5254.myshopify.com", "<USERNAME>", "<PASSWORD>"); $sa->create(new WWW::Shopify::Model::Discount({ discount_type => "fixed_amount", code => "asfhsdfsd", value => 10.0 }));'

And literally stick it into a system command of Ruby. However, this would entail starting up a copy of the perl interpreter, and then performing a get, sign in and a create with every call. Some of the code inside the module uses Moose, which is Perl's post-modern object system. Moose is very nice, concise, clear and reasonably speedy, but does have a startup penalty as it compiles all the classes into something more manageable, at compile-time. This would be in addition to the unecessary get/sign in, which would make the thing relatively slow, partially for the reasons you yourself state.

Ruby is a fine language! My coworkers use Ruby. We even talked about Ruby during the meetup, some. I happen to like Perl. They're both dynamically typed, semi-interpereted scripting languages, and are very similar. Both have very nice modules; some people like Rails (or other Ruby web frameworks), I like Catalyst (alnngside other Perl web frameworks). Both have examples of popular websites written in either. Both have deployment methods of integerating with popular web servers with a number of protocols.

Don't let anyone tell you that one language is better/easier-to-use than another, without actual, reasonable statistical data to back it up, which as far as I know, no one really has due to the sheer expense of having to hire many people to write the same non-trivial projects in two separate languages.

So don't think I'm bashing Ruby! And you sound like you're under a misapprehension of just what modern perl looks like these days! There's no more clumbsy OO, no more awkward CGI, no more DBI ugliness. Take another look:

https://metacpan.org/module/ETHER/Moose-2.0802/lib/Moose.pm

https://metacpan.org/module/RIBASUSHI/DBIx-Class-0.08250/lib/DBIx/Class.pm

https://metacpan.org/module/JJNAPIORK/Catalyst-Runtime-5.90030/lib/Catalyst.pm.

You may be surprised :)