Howto add product customization to the Substruct ruby on rails shopping cart

Posted by Jason, Fri Jan 12 10:19:00 UTC 2007

I have been getting a LOT of email from the Substruct mailing list recently from software developers asking how I was able to add product customization to Substruct, the ruby on rails shopping cart. To save myself from repeatedly answering these emails, I thought it would be a great idea to post it here. This way, other fellow ruby-ists can find it via google and the ones that do contact me, I can reply with a URL rather than a solution. Its nice when laziness parallels efficiency.

Obviously, this will vary depending on the application. For the application that I built, the products were displayed with a customize button. If the user did not click the customize button the stock product is submitted for order, if he does however click customize, an ajax drop down is rendered below the product with the customization options. So, I'm sure everyone gets the picture, lets get to the code. I had originally posted the code here, but it seemed like it made the post feel littered. It seems like just linking the files for folks to download is a cleaner approach. So, here ya go:

Inside of my display_product, I have the following code:

display_product.rhtml

These are the link_to_remote statements that set your view up for the customizations functionality. You will of course need to have a div specified for you to ajax your customization options into. Now, create a customize_product.rhtml file and drop in the following code. I will put in some example options to get the point across, but you would most likely want to change this.

customize_product.rhtml

Just for the sake of being complete, I thought I would also include my _cart.rhtml partial. Most of the modifications are pretty simple, and including it is most likely not necessary, but I felt it would be best to include it for clarity sake.

_cart.rhtml

That should finish things off for what should be inside the view.. Yes yes, I know that some of this code needs a bit of polishing to get some of the small things to be done the ruby way, but I've been busy and it will give you the idea.. :) Lets get onto the controller code.

store_controller.rb

And moving right along, lets not forget the models.

cart.rb

order_line_item.rb

I had some people asking about the database, here is my schema file:

schema.rb

So, I most likely overlooked some small details when posting everything as its been a while since I have looked at this project. If anyone has any improvements or such, drop a comment below..

I hope this helps all the others that are getting into a Substruct project and need customization functionality, if I forgot anything post me a comment and I'll get it there.

Filed Under: Substruct | Tags:

Comments

  1. grpuqhakud 10.30.07 / 09AM
  2. jvynygtnza 01.25.08 / 19PM

Have your say

A name is required. You may use HTML in your comments.