Interested in Building a Shopify App?

10 February 2010

In case you haven't seen it yet, Shopify has a brilliant developer platform that makes it easy for developers to create Shopify add-ons that can be marketed to Shopify store owners. And now, for even more awesomeness, there's the Shopify App Rails Kit that makes it incredibly easy to build an app to run on the Shopify platform. Check out the announcement on the Rails Kits blog, and see how quickly you can build a Shopify app.

Let me know when you've launched your app, and I'll add you to the Shopify App Rails Kit product page as a reference site. :)



Fun with instance_eval

30 June 2009

Recently someone asked for a combination discount at the RailsKits store -- he wanted to purchase both the SaaS Rails Kit and the Helpdesk Rails Kit to get his site rolling quickly. I didn't have combo discounts at that time, but I figured that was a good excuse to build that feature into the store. :)

Previously when I have implemented combo discounts for my clients in their e-commerce platforms, I would do so by creating models that stored the combinations and the products that make up those combinations. These models would contain info like what quantities of each product were required, if one product out of a set could qualify for a combo, etc. It can get complicated fairly quickly.

This time, though, the client was me, and I'm comfortable with Ruby, so I decided to simplify the code by simply allowing the store administrator (me) to use Ruby to specify the rules for qualifying for a combo discount. In this case, instance_eval came in quite handy.

I already had a Coupon model, so I just added a text field named conditions to store the code that would be evaluated in the context of a cart instance. Then in my Cart model, after checking that a coupon was otherwise available, I ran instance_eval on the contents of that conditions field. That allowed me to create a coupon that requires at least two items in the cart with prices above $100 quite easily:

line_items.select {|i| i.unit_amount > 100 }.size > 1

It was fun to be able to implement something that can be hairy in just a few lines of code, thanks to the awesomeness of Ruby.

BTW, if you are interested in using that discount -- 15% off! -- just enter the discount code 'combo' (without the quotes) during checkout while shopping for excellent Rails code.



SaaS Rails Kit now supports Authorize.net CIM

17 July 2008

I've been keeping busy on a number of projects, like the recently-released NextProof, a site that provides easy e-commerce for professional photographers.

I have still managed to find some time to add more awesome to the SaaS Rails Kit, though... this time in the form of support for Authorize.net CIM as a payment gateway. A number of people have wanted this, so I'm happy to be able to get it out there.

In other news, I'm close to releasing the next Rails Kit, so stay tuned. :)



RESTful searching in Rails

26 June 2008

Over and over again I see people asking about how to do a search the "RESTful way" with Rails, and every time I do I just shake my head and wonder why we sometimes get so caught up in how we do something that it keeps from actually doing it. So, with that introduction, I want to present to you a really simple implementation of the most basic type of searching you can do in a Rails application. It just so happens that this method will also handle the bulk of your searching needs.

Let's say you want to search through orders for a shopping cart. Instead of doing something fancy, let's just assume you want to search on the customer's name, the customer's email, and also optionally restrict the search to a date range. First, here's a snippet from the view, index.html.erb:

... < % form_tag({:action => 'index'}, :method => 'get') do %> < label>Name/Email< /label> < %= text_field_tag :q, h(params[:q]) %> < label>Date Range< /label> < %= text_field_tag :start_on, h(params[:start_on]) %> < %= text_field_tag :end_on, h(params[:end_on]) %>
< %= submit_tag 'Search' %> < % end %> ...

What, throwing search right into the index action? You betcha. Oh, you want pagination with that? No problem:

... < %= will_paginate @orders, :params => params %> ...

Bam. Ok, on to the controller:

class OrdersController < ApplicationController

def index @orders = Order.paginate(:all, :order => 'orders.created_at', :page => params[:page], :conditions => search_conditions, :joins => :user) end

...

protected def search_conditions cond_params = { :q => "%#{params[:q]}%", :start_on => Chronic.parse(params[:start_on]), :end_on => Chronic.parse(params[:end_on]) } cond_strings = returning([]) do |strings| strings < < "(users.name like :q or users.email like :q)" unless params[:q].blank? if cond_params[:start_on] && cond_params[:end_on] strings << "orders.created_at between :start_on and :end_on" elsif cond_params[:start_on] strings << "orders.created_at >= :start_on" elsif cond_params[:end_on] strings < < "orders.created_at <= :end_on" end end cond_strings.any? ? [ cond_strings.join(' and '), cond_params ] : nil end end

It just doesn't get much easier than this. If there was no search, then the :conditions arg gets passed in as nil, which gets silently dropped. If there was one, then we build some conditions based on which parameters were specified. With a little bit of help from the chronic gem, we get easy date entry, to boot.

We're done. And I just don't care whether it's RESTful or not. ;)



Webstore by Amazon

28 January 2008

I got an email from Amazon about their new WebStore by Amazon offering. Like Shopify, it's a just-add-water storefront for people wanting to sell their products.

This seems like a natural step for Amazon, considering their recent product offerings like payment processing and fulfillment services.

Perhaps my days of building custom e-commerce applications for clients like Griffin Technology are numbered?