Class: FlexCommerce::Cart
- Inherits:
-
FlexCommerceApi::ApiBase
- Object
- JsonApiClient::Resource
- FlexCommerceApi::BaseResource
- FlexCommerceApi::ApiBase
- FlexCommerce::Cart
- Defined in:
- app/models/cart.rb
Overview
A flex commerce Cart model
This model provides access to the flex commerce cart and associated line_items. This model allows you to create a cart, update its line items and delete a cart.
It is used much like an active record model.
Examples:
# Creating a cart
FlexCommerce::Cart.create #creates and returns a new cart ready for use
# Fetching its line items
cart.line_items
# Finding a cart
FlexCommerce::Cart.find(<<cart_id>>) # Finds the cart with this unique id
Constant Summary
Constants inherited from FlexCommerceApi::BaseResource
FlexCommerceApi::BaseResource::PRIVATE_ATTRIBUTES, FlexCommerceApi::BaseResource::RELATED_META_RESOURCES
Class Method Summary collapse
Instance Method Summary collapse
- #add_payment_transaction(transaction) ⇒ Object
- #available_shipping_methods ⇒ Object
- #empty? ⇒ Boolean
-
#find ⇒ FlexCommerce::Cart
Finds a cart.
-
#line_items ⇒ Object
Here we override line_items to provide a proxy to the array so we can use new and create on it in the normal active record way.
-
#merge!(other_cart) ⇒ Object
Merges another cart into this one using the API.
-
#validate_stock! ⇒ Object
This method is used when true stock levels re required - potentially from an external system To be used during checkout phases etc..
Methods inherited from FlexCommerceApi::ApiBase
Methods inherited from FlexCommerceApi::BaseResource
all, append_version, #as_json_api, capture_surrogate_keys, create!, endpoint_version, find, find_all, #freeze, #initialize, load, #meta_attribute, #method_missing, paginate, password, path, #public_attributes, reconfigure, reconfigure_all, reconfigure_api_base, reload_connection_if_required, #save!, username
Constructor Details
This class inherits a constructor from FlexCommerceApi::BaseResource
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class FlexCommerceApi::BaseResource
Class Method Details
.create(args = {}) ⇒ Object
107 108 109 110 111 112 113 |
# File 'app/models/cart.rb', line 107 def self.create(args = {}) if FlexCommerceApi.config.order_test_mode super(args.merge(test: true)) else super end end |
Instance Method Details
#add_payment_transaction(transaction) ⇒ Object
103 104 105 |
# File 'app/models/cart.rb', line 103 def add_payment_transaction(transaction) self.class.requestor.custom("relationships/payment_transactions", {request_method: :post}, {id: id, data: [type: "payment_transactions", id: transaction.id.to_s]}) end |
#available_shipping_methods ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/cart.rb', line 115 def available_shipping_methods return super if relationships[:available_shipping_methods].key?("data") shipping_methods = (:available_shipping_methods).to_a if shipping_methods.any? { |sm| sm.is_a?(FlexCommerce::RemoteShippingMethod) } shipping_method_references = shipping_methods.map(&:reference) # We are filtering in memory here as there will never be many shipping methods and they will almost certainly be in the cache anyway FlexCommerce::ShippingMethod.all.select { |shipping_method| shipping_method_references.include?(shipping_method.reference)} else shipping_methods end end |
#empty? ⇒ Boolean
60 61 62 |
# File 'app/models/cart.rb', line 60 def empty? line_items_count == 0 end |
#line_items ⇒ Object
Here we override line_items to provide a proxy to the array so we can use new and create on it in the normal active record way
|
# File 'app/models/cart.rb', line 35
|
#merge!(other_cart) ⇒ Object
Merges another cart into this one using the API
66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/cart.rb', line 66 def merge!(other_cart) self.last_result_set = self.class.requestor.custom("merge", { request_method: :patch }, data: {type: "carts", attributes: { from_cart_id: other_cart.id, to_cart_id: id } } ) mark_as_persisted! if updated = last_result_set.first self.attributes = updated.attributes self.relationships = updated.relationships clear_changes_information end self end |
#validate_stock! ⇒ Object
This method is used when true stock levels re required - potentially from an external system To be used during checkout phases etc.. Adds errors to the line items “unit_quantity” attribute if we do not have enough
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/cart.rb', line 80 def validate_stock! return {} if empty? stock_levels.each_with_object({}) do |stock_level, obj| line_items.detect { |li| li.item.sku == stock_level.id }.tap do |li| next if li.nil? if stock_level.stock_available <= 0 obj[li.item.sku] = { stock_level: 0, line_item_quantity: li.unit_quantity, message: "Out of stock" } elsif stock_level.stock_available < li.unit_quantity obj[li.item.sku] = { stock_level: stock_level.stock_available, line_item_quantity: li.unit_quantity, message: "Only #{stock_level.stock_available} in stock" } end li.errors.add(:unit_quantity, obj.dig(li.item.sku, :message)) unless obj.dig(li.item.sku, :message).nil? end end end |