Class: Magento::Cart

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

api_resource, create, #delete, delete, entity_name, find, #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::Cart.add_coupon(
  1,
  'COAU4HXE0I'
)

Returns:

  • Boolean: true on success, false otherwise



67
68
69
70
# File 'lib/magento/cart.rb', line 67

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

.delete_coupon(id) ⇒ Object

Delete a coupon from a specified cart.

Example:

Magento::Cart.delete_coupon(1)

Returns:

  • Boolean: true on success, raise exception otherwise



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

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

.order(attributes) ⇒ Object

Place order for cart

Example:

Magento::Cart.order(
  cartId: '12345',
  paymentMethod: { method: 'cashondelivery' },
  email: email
)

Returns:

  • String: return the order id



97
98
99
100
101
# File 'lib/magento/cart.rb', line 97

def order(attributes)
  attributes.transform_keys(&:to_sym)
  url = "#{api_resource}/#{attributes[:cartId]}/order"
  request.put(url, attributes).parse
end

Instance Method Details

#add_coupon(coupon) ⇒ Object

Add a coupon by code to the current cart.

Example:

cart = Magento::Cart.find(1)
cart.add_coupon('COAU4HXE0I')

Returns:

  • Boolean: true on success, false otherwise



17
18
19
# File 'lib/magento/cart.rb', line 17

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

#delete_couponObject

Delete cart’s coupon

Example:

cart = Magento::Cart.find(1)
cart.delete_coupon()

Returns:

  • Boolean: true on success, raise exception otherwise



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

def delete_coupon
  self.class.delete_coupon(id)
end

#order(email:, payment:) ⇒ Object

Place order for cart

Example:

cart = Magento::Cart.find('12345')

# or use "build" to not request information from the magento API
cart = Magento::GuestCart.build({ 'cart_id' => '12345' })

cart.order(
  email: '[email protected]',
  payment: { method: 'cashondelivery' }
)

Returns:

  • String: return the order id



50
51
52
53
# File 'lib/magento/cart.rb', line 50

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