Loading...

spree-user@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [spree-user] Input on Spree beginner's extension to round order total as a donation? Ryan Bigg Thu Feb 16 09:02:02 2012

Hi Chris, 

Rather than doing this:

order = Spree::Order.where(:number => params[:id]).first

Do this:

order = Spree::Order.find_by_number(params[:id]

Same goes for the Spree::Product count a little later on.

You also don't need to call Spree::Variant.find, as product.master will return 
that anyway. 

When you call add_variant, you don't need to pass through the quantity as it 
defaults to 1.

Happy to help,
Ryan Bigg


On Wednesday, 15 February 2012 at 4:00 AM, chris cummings wrote:

> While vetting Spree to replace our current cart application (Magento)
> I figured trying to implement some custom extensions would be a good
> way to get to know it. My company would like to offer customers the
> option to round up their order total to the next dollar amount as a
> donation to a charity relating to our customer base.
> 
> I'd like to get some input on my implementation of this feature. Have
> I gone the opposite direction of the Spree way/convention?
> Accomplished this in the most ridiculous way possible? Obviously the
> below code needs some refactoring, tests, etc. but I figured I'd get
> some input before cleaning it up.
> 
> ---
> 
> * There is a product name "Charity" with a dollar value of $0.00
> already in the database
> 
> * I've added a route in my extension's routes.rb to handle an AJAX
> call:
> 
> match 'orders/charity_round/:id' => 'orders#charity_round', :via
> => :post
> 
> 
> * And I've extended the orders_controller with a new method:
> 
> Spree::OrdersController.class_eval do
> 
> def charity_round
> order = Spree::Order.where(:number => params[:id]).first
> donation = params[:donation]
> has_charity_line_item = false
> 
> order.line_items.each { |item|
> if item.variant.name (http://item.variant.name) == 'Charity'
> item.price = donation
> item.save
> has_charity_line_item = true
> end
> }
> 
> if !has_charity_line_item
> charity_product = Spree::Product.where(:name => 'Charity').first
> charity_variant = Spree::Variant.find(charity_product.master.id)
> 
> line_item = order.add_variant(charity_variant, 1)
> line_item.price = donation
> 
> order.save
> end
> 
> # FIXME: handle JSON response
> end
> 
> end
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Spree" group.
> To post to this group, send email to [EMAIL PROTECTED] (mailto:[EMAIL 
> PROTECTED]).
> To unsubscribe from this group, send email to [EMAIL PROTECTED] 
> (mailto:[EMAIL PROTECTED]).
> For more options, visit this group at 
> http://groups.google.com/group/spree-user?hl=en.
> 
> 


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