Class: Magento::GuestCart

Inherits:
Model
  • Object
show all
Defined in:
lib/magento/guest_cart.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

api_resource, #delete, delete, entity_name, #id, #save, #update, update

Methods included from ModelParser

included, #to_h

Class Method Details

.add_coupon(id, coupon) ⇒ Object

Add a coupon by code to a specified cart.

Example Magento::GuestCart.add_coupon(

'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN',
'COAU4HXE0I'

)

Returns:

  • Boolean: true on success, false otherwise



101
102
103
104
# File 'lib/magento/guest_cart.rb', line 101

def add_coupon(id, coupon)
  url = "#{api_resource}/#{id}/coupons/#{coupon}"
  request.put(url, nil).parse
end

.add_item(id, attributes) ⇒ Object



85
86
87
88
89
# File 'lib/magento/guest_cart.rb', line 85

def add_item(id, attributes)
  url  = "#{api_resource}/#{id}/items"
  hash = request.post(url, attributes).parse
  Magento::ModelMapper.map_hash(Magento::Item, hash)
end

.create(load_cart_info: false) ⇒ Object



59
60
61
62
# File 'lib/magento/guest_cart.rb', line 59

def create(load_cart_info: false)
  cart = build(cart_id: request.post(api_resource).parse)
  find cart.id if load_cart_info
end

.delete_coupon(id) ⇒ Object

Delete a coupon from a specified cart.

Example: Magento::GuestCart.delete_coupon(‘aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN’)

Returns:

  • Boolean: true on success, raise exception otherwise



113
114
115
116
# File 'lib/magento/guest_cart.rb', line 113

def delete_coupon(id)
  url = "#{api_resource}/#{id}/coupons"
  request.delete(url).parse
end

.find(id) ⇒ Object



64
65
66
# File 'lib/magento/guest_cart.rb', line 64

def find(id)
  cart = build request.get("#{api_resource}/#{id}").parse.merge(cart_id: id)
end

.payment_information(attributes) ⇒ Object

Set payment information to finish the order using class method

Example: Magento::GuestCart.payment_information(

cartId: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN',
paymentMethod: { method: 'cashondelivery' },
email: email

)

Returns:

  • String: return the order id



79
80
81
82
83
# File 'lib/magento/guest_cart.rb', line 79

def payment_information(attributes)
  attributes.transform_keys(&:to_sym)
  url = "#{api_resource}/#{attributes[:cartId]}/payment-information"
  request.post(url, attributes).parse
end

Instance Method Details

#add_coupon(coupon) ⇒ Object

Add a coupon by code to the current cart.

Example cart = Magento::GuestCart.find(‘gXsepZcgJbY8RCJXgGioKOO9iBCR20r7’) cart.add_coupon(‘COAU4HXE0I’)

Returns:

  • Boolean: true on success, false otherwise



43
44
45
# File 'lib/magento/guest_cart.rb', line 43

def add_coupon(coupon)
  self.class.add_coupon(cart_id, coupon)
end

#add_item(item_attributes) ⇒ Object



8
9
10
11
12
13
# File 'lib/magento/guest_cart.rb', line 8

def add_item(item_attributes)
  attributes = { cartItem: item_attributes.merge(quote_id: cart_id) }
  item = self.class.add_item(cart_id, attributes)
  @items = @items.reject {|i| i.item_id == item.item_id} << item if @items
  item
end

#delete_couponObject

Delete cart’s coupon

Example: cart = Magento::GuestCart.find(‘gXsepZcgJbY8RCJXgGioKOO9iBCR20r7’) cart.delete_coupon()

Returns:

  • Boolean: true on success, raise exception otherwise



54
55
56
# File 'lib/magento/guest_cart.rb', line 54

def delete_coupon
  self.class.delete_coupon(cart_id)
end

#payment_information(email:, payment:) ⇒ Object

Set payment information to finish the order

Example: cart = Magento::GuestCart.find(‘gXsepZcgJbY8RCJXgGioKOO9iBCR20r7’)

# or use “build” to not request information from the magento API cart = Magento::GuestCart.build({ ‘cart_id’ => ‘aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN’ })

cart.payment_information(

email: '[email protected]',
payment: { method: 'cashondelivery' }

)

Returns:

  • String: return the order id



30
31
32
33
# File 'lib/magento/guest_cart.rb', line 30

def payment_information(email:, payment:)
  attributes = { cartId: cart_id, paymentMethod: payment, email: email }
  self.class.payment_information(attributes)
end