app/models/plugins/ecommerce/cart.rb in camaleon_ecommerce-1.2 vs app/models/plugins/ecommerce/cart.rb in camaleon_ecommerce-1.2.1

- old
+ new

@@ -10,10 +10,20 @@ belongs_to :user, :class_name => "CamaleonCms::User", foreign_key: :user_id scope :active_cart, ->{ where("#{Plugins::Ecommerce::Cart.table_name}.updated_at >= ?", 24.hours.ago) } after_create :generate_slug + # status: bank_pending => pending of verification for bank transfer orders + # paid => paid by some method + # canceled => canceled order + # shipped => shipped status + # accepted => received status + + def payment_method + @_cama_cache_payment_method ||= Plugins::Ecommerce::PaymentMethod.find_by_id(get_meta('payment_method_id', self.payment_method_id)) + end + def add_product(product, qty = 1, variation_id = nil) pi = product_items.where(product_id: product.id, variation_id: variation_id).first if pi.present? pi.update_column(:qty, qty) else @@ -59,13 +69,11 @@ res[:error] = 'times_exceeded' elsif !coupon.valid_min_price?(sub_total) res[:error] = 'required_minimum_price' else case opts[:discount_type] - when 'free' - res[:discount] = price || sub_total - when 'free_ship' + when 'free_ship', 'free' res[:discount] = total_shipping when 'percent' res[:discount] = sub_total * opts[:amount].to_f / 100 when 'money' res[:discount] = opts[:amount].to_f @@ -124,16 +132,21 @@ # return the total price of shipping def total_shipping shipping_method.present? ? shipping_method.get_price_from_weight(weight_total) : 0 end - # set user in filter + # set user in filter (filter carts by user_id or cookie_id) + # cookie_id is used for public users who are buying without login def self.set_user(user) - user_id = defined?(user.id) ? user.id : user.to_i - self.where(user_id: user_id) + defined?(user.id) ? self.where(user_id: user.id) : self.where(visitor_key: user) end + # move current cart from public user into existent user + def change_user(user) + update_columns(user_id: user.id, visitor_key: nil) + end + # check if the price of the cart is 0, including prices for products, discounts, shipping def free_cart? total_amount <= 0 end @@ -164,15 +177,26 @@ cache_the_weight: c.the_weight_total, cache_the_discounts: c.the_total_discounts, cache_the_shipping: c.the_total_shipping, ) end - + def mark_paid(status = 'paid') self.update_columns( status: status, paid_at: Time.current, ) + end + + # return the gateway for paypal transactions + def paypal_gateway + ActiveMerchant::Billing::Base.mode = payment_method.options[:paypal_sandbox].to_s.to_bool ? :test : :production + paypal_options = { + login: payment_method.options[:paypal_login], + password: payment_method.options[:paypal_password], + signature: payment_method.options[:paypal_signature] + } + ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options) end private def generate_slug