Skip to content

Instantly share code, notes, and snippets.

@mrpollo
Last active December 26, 2015 04:09
Show Gist options
  • Save mrpollo/7091561 to your computer and use it in GitHub Desktop.
Save mrpollo/7091561 to your computer and use it in GitHub Desktop.
Spree Utils
# cancel shipped order
Spree::Order.class_eval do
def allow_cancel?
true
end
end
Spree::Shipment.class_eval do
def after_cancel
# do nothing
end
end
o = Spree::Order.find_by_number 'ORDER_NUMBER'
o.shipments.each do |s|
s.state = 'ready'
s.save
end
o.cancel
# change order contents price
# when user is distro and a non distro changed
# the order in the backend (changed = added items)
o = Spree::Order.find_by_number 'ORDER_NUMBER'
o.line_items.each do |l|
distro_price = Spree::UserGroupsVariant.where(:user_group_id => o.user.user_group.id, :variant_id => l.variant_id).try(:first).try(:price)
if distro_price.nil?
puts "No changes made to this line_item: #{l.id} - #{l.variant.name}"
next
end
l.price = distro_price
l.save
end
Spree::ReturnAuthorization.class_eval do
private
def process_return
inventory_units.each do |iu|
iu.return!
# don't re stock
# Spree::StockMovement.create!(stock_item_id: iu.find_stock_item.id, quantity: 1)
end
credit = Adjustment.new(amount: amount.abs * -1, label: Spree.t(:rma_credit))
credit.source = self
credit.adjustable = order
credit.save
order.return if inventory_units.all?(&:returned?)
end
end
# remove backordered status from shipments
s = Spree::Shipment.find_by_number 'SHIPMENT_NUMBER'
s.inventory_units.backordered.each &:fill_backorder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment