Class: AboutYou::SDK::Model::BasketSet
- Inherits:
-
Object
- Object
- AboutYou::SDK::Model::BasketSet
- Includes:
- AbstractBasketItem, ResultError
- Defined in:
- lib/AboutYou/Model/Basket/basket_set.rb
Overview
BasketSet is a class used for adding a set of variant items into the basket
If you want to add a set of variant items into a basket, you need to create an instance of a BasketSet. The BasketSet contains BasketSetItems.
A set can be useful if you want to sell several variants as a single product. For example, if you offer a pair of shoes and additionally different styles of shoelaces the customer can choose from, you maybe want to put both - shoes and laces - together.
Example usage: $lacesVariantId = $lacesVariant->getId(); // $lacesVariant is instance of CollinsShopApiModelVariant $shoesVariantID = $shoesVariant->getId(); // $lacesVariant is instance of CollinsShopApiModelVariant $basketItem1 = new BasketItem($lacesVariantId);
$basketSet = new BasketItemSet(‘my-personal-identifier’); $basketSet->addItem(new BasketSetItem($lacesVariantId)); $basketSet->addItem(new BasketSetItem($shoesVariantId)); $basketSet->setAdditionalData([‘description’ => ‘Shoes with laces ’yellow star”, ‘image_url’ = ‘
’]); $basket->updateItemSet($basketSet) $shop_api->update_basket(session_id(), $basket);
You can use the static method create as an alternative to generate a basket set: $basketSet = BasketItemSet::create(
'my-personal-identifier',
[
[$lacesVariant->getId()],
[$shoesVariantID->getId()],
]
);
Constant Summary collapse
- IMAGE_URL_REQUIRED =
true
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#id ⇒ Object
The ID of this basket item.
-
#item_app_id ⇒ Object
Returns the value of attribute item_app_id.
-
#items ⇒ Object
Returns the value of attribute items.
-
#total_net ⇒ Object
Returns the value of attribute total_net.
-
#total_price ⇒ Object
Returns the value of attribute total_price.
-
#total_vat ⇒ Object
Returns the value of attribute total_vat.
Attributes included from AbstractBasketItem
Attributes included from ResultError
#error_code, #error_ident, #error_message
Instance Method Summary collapse
- #add_item(item) ⇒ Object
- #check_id(id) ⇒ Object
-
#create(item_id, sub_items, additional_data = nil) ⇒ Object
Create an basket item set from an array, for example: BasketSet::create( ‘identifier4’, [ [12312121], [7777777, [‘description’ => ‘engravingssens’, ‘internal_infos’ => [‘stuff’]]] ], [‘description’ => ‘Wunderschön und so’] );.
-
#create_from_json(json_object, factory, products) ⇒ Object
BasketSet.
-
#errors? ⇒ Boolean
Boolean.
-
#initialize(id, additional_data = nil) ⇒ BasketSet
constructor
Additional data are transmitted to the merchant untouched.
- #unique_key ⇒ Object
Methods included from AbstractBasketItem
#addition_data=, #check_additional_data, #description
Methods included from ResultError
Constructor Details
#initialize(id, additional_data = nil) ⇒ BasketSet
Additional data are transmitted to the merchant untouched. If set (array not empty), a key ‘description’ must exist. This description must be a string that describes the variant. If you want to pass an image URL that represents this item set, you can add a key ‘image_url’ to the $additional_data that contains the URL to the image.
71 72 73 74 75 76 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 71 def initialize(id, additional_data = nil) check_id(id) check_additional_data(additional_data, true) self.id = id self.additional_data = additional_data end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
55 56 57 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 55 def errors @errors end |
#id ⇒ Object
The ID of this basket item. You can choose this ID by yourself to identify your item later.
53 54 55 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 53 def id @id end |
#item_app_id ⇒ Object
Returns the value of attribute item_app_id.
59 60 61 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 59 def item_app_id @item_app_id end |
#items ⇒ Object
Returns the value of attribute items.
54 55 56 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 54 def items @items end |
#total_net ⇒ Object
Returns the value of attribute total_net.
57 58 59 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 57 def total_net @total_net end |
#total_price ⇒ Object
Returns the value of attribute total_price.
56 57 58 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 56 def total_price @total_price end |
#total_vat ⇒ Object
Returns the value of attribute total_vat.
58 59 60 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 58 def total_vat @total_vat end |
Instance Method Details
#add_item(item) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 153 def add_item(item) if items.count == 0 self.item_app_id = item.app_id elsif item_app_id != item.app_id fail '\InvalidArgumentException! you can not set different app ids for items in an item-set.' end items.push(item) end |
#check_id(id) ⇒ Object
191 192 193 194 195 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 191 def check_id(id) fail '\InvalidArgumentException! ID of the BasketSetItem must be a String that must contain minimum two characters' unless id.is_a?(String) || !id.length < 2 end |
#create(item_id, sub_items, additional_data = nil) ⇒ Object
Create an basket item set from an array, for example:
BasketSet::create(
'identifier4',
[
[12312121],
[7777777, ['description' => 'engravingssens', 'internal_infos' => ['stuff']]]
],
['description' => 'Wunderschön und so']
);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 135 def create(item_id, sub_items, additional_data = nil) set = new(item_id, additional_data) sub_items.each do |item_data| set.add_item( AboutYou::SDK::Model::BasketSetItem.new( item_data[0], item_data[1] ? item_data[1] : nil ) ) end set end |
#create_from_json(json_object, factory, products) ⇒ Object
Returns BasketSet.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 85 def create_from_json(json_object, factory, products) set = new( json_object['id'], if json_object.key?('additional_data') [json_object['additional_data']] else nil end ) set.parse_error_result(json_object) json_object['set_items'].each do |index, json_term| item = factory.create_basket_set_item(json_term, products) if item.errors? set.errors[index] = item else set.items[index] = item end end if json_object.key?('total_price') set.totalPrice = json_object['total_price'] end if json_object.key?('total_net') set.totalPrice = json_object['total_net'] end if json_object.key?('total_vat') set.totalPrice = json_object['total_vat'] end set end |
#errors? ⇒ Boolean
Returns boolean.
167 168 169 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 167 def errors? error_code || errors.count > 0 end |
#unique_key ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/AboutYou/Model/Basket/basket_set.rb', line 171 def unique_key key = ':' unless additional_data additional_data.sort! key = key + ':' + additional_data.to_json end items = [] self.items.each do |item| items.push(item.unique_key) end key += items.to_json key end |