Skip to content

Instantly share code, notes, and snippets.

@dabit
Created June 15, 2011 23:00
Show Gist options
  • Save dabit/1028335 to your computer and use it in GitHub Desktop.
Save dabit/1028335 to your computer and use it in GitHub Desktop.
def self.use_certificate(code, cart)
return false if cart.nil?
return cart.set_gift(0, nil) if code.blank?
return false if cart.gift_code == code
certificate = self.certificate(code).first
unless certificate.blank? || certificate.state == 'in_cart'
unless certificate.state == 'empty'
total = cart.total + cart.gift_discount
if certificate.amount < total
cart.set_gift(certificate.amount, certificate.gift_code)
else
cart.set_gift(total, certificate.gift_code)
end
else
cart.set_gift(0, nil)
end
else
cart.set_gift(0, nil)
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment