Some Tips and tricks for Spree 2.2 promotions/Coupons

Some Tips and tricks for Spree 2.2 promotions/Coupons

How to apply a Spree coupon promotion within console:

# search the order in this case I'm using the last one
o = Spree::Order.last
# apply the coupon code you already create from the admin panel section
o.coupon_code = 'order_discount'
# apply the coupon code specified
handler = Spree::PromotionHandler::Coupon.new(o).apply

if you want to delete/remove a promotion/coupon applied do the following:

1. Delete the promotion or all the promotion you want(coupon already applied)
2. Delete all the adjustments associated with that order.
3. And update totals

Something as follow:

o = Spree::Order.last // you have to search the correct order you want to process
o.adjustments.destroy_all
# NOTE: this line will destroy the promotions created if you dont want that 
# don't run this line
# o.promotions.destroy_all // this will destroy the promotion as well
o.promotions.clear // will only destroy the spree_orders_promotions relation
o.promotions.find(id: xxx).delete // if you want to destroy a specific promotion 
o.update_totals
o.persist_totals

Print the adjustments amount related to an Order:
In this case I’m using the adjustments within the last spree order

Spree::Adjustment.where(:adjustable_id => Spree::Order.last.id).map{|a| a.amount.to_s}
No Comments

Post A Comment