Newbie Spree logic customisation question

44 views
Skip to first unread message

Simon Morley

unread,
Feb 12, 2012, 4:15:39 PM2/12/12
to Spree
We're reasonably need to spree and are using it to sell voucher codes.
Spree 1.1.0.beta is setup and working in our rails 3.1.3 application.

The vouchers codes are pre-generated in batches outside of spree. They
sit in a model called voucher.rb which is owned by batches. I need
spree to select a voucher at random from the db on successful
authorisation and present the value to customer (and display in their
account).

I need some help getting this setup. Having thought it through, I
think it should work like this:

1. User selects voucher (product) from list. Adds to cart. Checks
out...

2. Upon successful payment, spree selects the voucher code from the
correct batch using the batch_id (set in admin) using something like
this (simplified):

voucher = Voucher.first(:order => "RAND()")

3. Once generated, the voucher code is displayed on screen and in
customers account.

I have three questions:

1.When I create the voucher (product) in admin, I need to set the
batch_id so it knows where to pull the voucher from. How can this be
achieved?

2. What do I need to modify to get spree to select the voucher from
batch on successful payment?

3. How can this be added to the customer's account as an order?

Looking forward to your suggestions.










Brian Quinn

unread,
Feb 13, 2012, 3:37:45 AM2/13/12
to spree...@googlegroups.com

1.When I create the voucher (product) in admin, I need to set the
batch_id so it knows where to pull the voucher from. How can this be
achieved?
 You can just add a migration to extend the spree_products table to include the new batch_id column, and then create on products_decorator.rb that class_eval's the Product class to include the belongs_to :batch association.

Finally, you need to define a Deface override to include the batch drop down / form input on the products admin form. These steps are all covered in the customization section of the Spree Guides.
2. What do I need to modify to get spree to select the voucher from
batch on successful payment?
I think an ActiveRecord Observer on the Order model's state_machine is the way to go, take a look at the state_machine gems docs here:


You basically want to select the voucher on :complete

3. How can this be added to the customer's account as an order?

I'd suggest on step 2 above, that you associate the voucher with line_item in the order that way you can track back which order received which voucher(s). And you can always re-display the customer their voucher by looping through their orders.
 
Looking forward to your suggestions.










--
You received this message because you are subscribed to the Google Groups "Spree" group.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.

Simon Morley

unread,
Feb 13, 2012, 4:32:02 AM2/13/12
to Spree
Thanks for the reply. Think I'm happy with step one having played a
little this morning.

Am not quite as sure about step two. Will read through now..


On Feb 13, 8:37 am, Brian Quinn <br...@spreecommerce.com> wrote:
> > 1.When I create the voucher (product) in admin, I need to set the
> > batch_id so it knows where to pull the voucher from. How can this be
> > achieved?
>
>  You can just add a migration to extend the spree_products table to include the new batch_id column, and then create on products_decorator.rb that class_eval's the Product class to include the belongs_to :batch association.
>
> Finally, you need to define a Deface override to include the batch drop down / form input on the products admin form. These steps are all covered in the customization section of the Spree Guides.
>
>
>
> > 2. What do I need to modify to get spree to select the voucher from
> > batch on successful payment?
>
> I think an ActiveRecord Observer on the Order model's state_machine is the way to go, take a look at the state_machine gems docs here:
>
> https://github.com/pluginaweek/state_machine
>
> You basically want to select the voucher on :complete
>
> > 3. How can this be added to the customer's account as an order?
>
> I'd suggest on step 2 above, that you associate the voucher with line_item in the order that way you can track back which order received which voucher(s). And you can always re-display the customer their voucher by looping through their orders.
>
>
>
>
>
>
>
>
>
> > Looking forward to your suggestions.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Spree" group.
> > To post to this group, send email to spree...@googlegroups.com (mailto:spree...@googlegroups.com).
> > To unsubscribe from this group, send email to spree-user+...@googlegroups.com (mailto:spree-user+...@googlegroups.com).

Simon M

unread,
Feb 27, 2012, 10:09:47 AM2/27/12
to spree...@googlegroups.com
Hi Brian

I've now modified Spree to deal with digital voucher codes but need a little help understanding where to put the logic in my orders decorator.

Spree now detects whether an order contains a voucher and adjusts the state accordingly (skipping need for delivery etc). 

To test things, I put the following at the end of the finalize! block:

     if :prepaid_voucher?
           self.generate_voucher
     end

Where prepaid_voucher? determines if there's a voucher or not and the generate_voucher section calls a block which selects the voucher code from the database at random.

The issue is that we sell other things on the site and therefore only need to generate this number on the items in the cart which are tagged as vouchers. 

I can't figure out where to put this call to do so on the individual items...

Thanks,

Simon


On Monday, February 13, 2012 9:32:02 AM UTC, Simon M wrote:
Thanks for the reply. Think I'm happy with step one having played a
little this morning.

Am not quite as sure about step two. Will read through now..


On Feb 13, 8:37 am, Brian Quinn <br...@spreecommerce.com> wrote:
> > 1.When I create the voucher (product) in admin, I need to set the
> > batch_id so it knows where to pull the voucher from. How can this be
> > achieved?
>
>  You can just add a migration to extend the spree_products table to include the new batch_id column, and then create on products_decorator.rb that class_eval's the Product class to include the belongs_to :batch association.
>
> Finally, you need to define a Deface override to include the batch drop down / form input on the products admin form. These steps are all covered in the customization section of the Spree Guides.
>
>
>
> > 2. What do I need to modify to get spree to select the voucher from
> > batch on successful payment?
>
> I think an ActiveRecord Observer on the Order model's state_machine is the way to go, take a look at the state_machine gems docs here:
>
> https://github.com/pluginaweek/state_machine
>
> You basically want to select the voucher on :complete
>
> > 3. How can this be added to the customer's account as an order?
>
> I'd suggest on step 2 above, that you associate the voucher with line_item in the order that way you can track back which order received which voucher(s). And you can always re-display the customer their voucher by looping through their orders.
>
>
>
>
>
>
>
>
>
> > Looking forward to your suggestions.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Spree" group.
> > To post to this group, send email to spree...@googlegroups.com (mailto:spree-user@googlegroups.com).
> > To unsubscribe from this group, send email to spree-user+unsubscribe@googlegroups.com (mailto:spree-user+unsub...@googlegroups.com).
> > For more options, visit this group athttp://groups.google.com/group/spree-user?hl=en.

On Monday, February 13, 2012 9:32:02 AM UTC, Simon M wrote:
Thanks for the reply. Think I'm happy with step one having played a
little this morning.

Am not quite as sure about step two. Will read through now..


On Feb 13, 8:37 am, Brian Quinn <br...@spreecommerce.com> wrote:
> > 1.When I create the voucher (product) in admin, I need to set the
> > batch_id so it knows where to pull the voucher from. How can this be
> > achieved?
>
>  You can just add a migration to extend the spree_products table to include the new batch_id column, and then create on products_decorator.rb that class_eval's the Product class to include the belongs_to :batch association.
>
> Finally, you need to define a Deface override to include the batch drop down / form input on the products admin form. These steps are all covered in the customization section of the Spree Guides.
>
>
>
> > 2. What do I need to modify to get spree to select the voucher from
> > batch on successful payment?
>
> I think an ActiveRecord Observer on the Order model's state_machine is the way to go, take a look at the state_machine gems docs here:
>
> https://github.com/pluginaweek/state_machine
>
> You basically want to select the voucher on :complete
>
> > 3. How can this be added to the customer's account as an order?
>
> I'd suggest on step 2 above, that you associate the voucher with line_item in the order that way you can track back which order received which voucher(s). And you can always re-display the customer their voucher by looping through their orders.
>
>
>
>
>
>
>
>
>
> > Looking forward to your suggestions.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Spree" group.
> > To post to this group, send email to spree...@googlegroups.com (mailto:spree-user@googlegroups.com).
> > To unsubscribe from this group, send email to spree-user+unsubscribe@googlegroups.com (mailto:spree-user+unsub...@googlegroups.com).

Brian Quinn

unread,
Feb 27, 2012, 10:26:34 AM2/27/12
to spree...@googlegroups.com
Simon,

The if statement you included will always return true, I'm guessing it should be self.prepaid_voucher? (that is if prepaid_voucher? is a method on order).
     if :prepaid_voucher?
           self.generate_voucher
     end


To answer your actual question, you can interrogate the line_items associated on with an order in state machine callbacks and check any of them contain the variant that uses a voucher.



Simon M

unread,
Feb 27, 2012, 11:00:57 AM2/27/12
to spree...@googlegroups.com
Hello, thanks for reply

I'll check the logic as suggested. Thanks for that, didn't realise that would be the result.

Ok re. interrogation. Am I correct to put this in the finalize block?

S

Brian Quinn

unread,
Feb 27, 2012, 11:16:50 AM2/27/12
to spree...@googlegroups.com
Putting it in the finalize block is a bit of brute force way to do it.

You should look at adding an ActiveRecord observer on the order model, see the state machine's read me for more:


-- 
Brian Quinn
Spree Commerce

--
You received this message because you are subscribed to the Google Groups "Spree" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/1wlLgjiE_RoJ.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages