Slide 1

Slide 1 text

Custom Attributes Tutorial Spree Commerce Hub Tutorial Series 3 of 3

Slide 2

Slide 2 text

Why? ● Customize storefront

Slide 3

Slide 3 text

Why?

Slide 4

Slide 4 text

Why? ● Customize storefront ● Get customizations to the hub

Slide 5

Slide 5 text

Why? ● Customize storefront ● Get customizations to the hub ● Save on support time

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Why? ● Customize storefront ● Get customizations to the hub ● Save on support time ● Help new hires understand workflow

Slide 8

Slide 8 text

How? ● Step 1: Add custom attribute to storefront

Slide 9

Slide 9 text

How? ● Step 1: Add custom attribute to storefront – Sandbox

Slide 10

Slide 10 text

How? ● Step 1: Add custom attribute to storefront – Sandbox – Migration

Slide 11

Slide 11 text

How? ● Step 1: Add custom attribute to storefront – Sandbox – Migration – Model Decorator

Slide 12

Slide 12 text

How? module Spree Address.class_eval do attr_accessible :variety end end

Slide 13

Slide 13 text

How? ● Step 1: Add custom attribute to storefront – Sandbox – Migration – Model Decorator – Deface File

Slide 14

Slide 14 text

How?

> <%= form.label :variety, "Address Type" %>
<%= form.select :variety, ["Residence", "Business", "Other"] %>

Slide 15

Slide 15 text

How? ● Step 2: Extend JSON output

Slide 16

Slide 16 text

How? ● Step 2: Extend JSON output – API Decorator

Slide 17

Slide 17 text

How? Spree::Api::ApiHelpers.class_eval do def address_attributes_with_variety address_attributes_without_variety << :variety end alias_method_chain :address_attributes, :variety end

Slide 18

Slide 18 text

How? { "message": "order:new", "payload": { "order": { "ship_address": { "variety": "Residence" } } } }

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Proof $ curl --header "X-Spree-Token: your_spree_token" http://yourdomain.com/api/orders/R123456789.json

Slide 21

Slide 21 text

Proof $ curl --header "X-Spree-Token: your_spree_token" http://yourdomain.com/api/orders/R123456789.json {"id":1,"number":"R123456789","item_total":"15.99","total":"22.59","state":"complete","adjustment_total":"6.6","user_id":null,"created_at":"2013-08- 28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z","completed_at":"2013-08- 27T18:20:19Z","payment_total":"0.0","shipment_state":"pending","payment_state":"balance_due","email":"[email protected]","special_instructions":null,"token":"d32f8 9f869d639a7","bill_address":{"id":2,"firstname":"Grant","lastname":"Christiansen","full_name":"Grant Christiansen","address1":"93314 Elyssa Harbor","address2":"Apt. 338","city":"Krisport","zipcode":"16804","phone":"1-395-505-5100 x6665","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"ship_address": {"id":1,"firstname":"Retha","lastname":"Murray","full_name":"Retha Murray","address1":"8730 Dickens Keys","address2":"Apt. 049","city":"Karenburgh","zipcode":"16804","phone":"425-355- 5233","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"line_items": [{"id":1,"quantity":1,"price":"15.99","variant_id":1,"variant":{"id":1,"name":"Ruby on Rails Tote","sku":"ROR- 00011","price":"15.99","weight":null,"height":null,"width":null,"depth":null,"is_master":true,"cost_price":"17.0","permalink":"ruby-on-rails- tote","options_text":"","option_values":[],"product_id":1,"images": [{"id":21,"position":1,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/21/produ ct/ror_tote.jpeg?1377714005"}, {"id":22,"position":2,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote_back.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/22/produ ct/ror_tote_back.jpeg?1377714005"}]}}],"payments":[{"id":1,"amount":"22.59","state":"pending","payment_method_id":1,"payment_method":{"id":1,"name":"Credit Card","environment":"development"}}],"shipments": [{"id":1,"tracking":null,"number":"H65183147836","cost":"5.0","shipped_at":null,"state":"pending","order_id":"R123456789","stock_location_name":"default","shipping _rates":[{"id":1,"cost":"5.0","selected":true,"shipment_id":1,"shipping_method_id":1}, {"id":2,"cost":"10.0","selected":false,"shipment_id":1,"shipping_method_id":2}, {"id":3,"cost":"15.0","selected":false,"shipment_id":1,"shipping_method_id":3}],"shipping_method":{"name":"UPS Ground (USD)"},"inventory_units": [{"id":1,"state":"on_hand","variant_id":1,"shipment_id":1,"return_authorization_id":null}]}],"adjustments": [{"id":1,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount ":"0.8","label":"North America 5.0%","mandatory":false,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":3,"source_type":"Spree::Shipment","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id": 1,"amount":"5.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":5,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount" :"0.8","label":"Tax","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":7,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id":1," amount":"0.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}],"credit_cards": [{"id":1,"month":"12","year":"2014","cc_type":"visa","last_digits":"1111","first_name":"Sean","last_name":"Schofield","gateway_customer_profile_id":"BGS- 1234","gateway_payment_profile_id":null}]

Slide 22

Slide 22 text

Proof $ curl --header "X-Spree-Token: your_spree_token" http://yourdomain.com/api/orders/R123456789.json {"id":1,"number":"R123456789","item_total":"15.99","total":"22.59","state":"complete","adjustment_total":"6.6","user_id":null,"created_at":"2013-08- 28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z","completed_at":"2013-08- 27T18:20:19Z","payment_total":"0.0","shipment_state":"pending","payment_state":"balance_due","email":"[email protected]","special_instructions":null,"token":"d32f8 9f869d639a7","bill_address":{"id":2,"firstname":"Grant","lastname":"Christiansen","full_name":"Grant Christiansen","address1":"93314 Elyssa Harbor","address2":"Apt. 338","city":"Krisport","zipcode":"16804","phone":"1-395-505-5100 x6665","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"ship_address": {"id":1,"firstname":"Retha","lastname":"Murray","full_name":"Retha Murray","address1":"8730 Dickens Keys","address2":"Apt. 049","city":"Karenburgh","zipcode":"16804","phone":"425-355- 5233","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"line_items": [{"id":1,"quantity":1,"price":"15.99","variant_id":1,"variant":{"id":1,"name":"Ruby on Rails Tote","sku":"ROR- 00011","price":"15.99","weight":null,"height":null,"width":null,"depth":null,"is_master":true,"cost_price":"17.0","permalink":"ruby-on-rails- tote","options_text":"","option_values":[],"product_id":1,"images": [{"id":21,"position":1,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/21/produ ct/ror_tote.jpeg?1377714005"}, {"id":22,"position":2,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote_back.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/22/produ ct/ror_tote_back.jpeg?1377714005"}]}}],"payments":[{"id":1,"amount":"22.59","state":"pending","payment_method_id":1,"payment_method":{"id":1,"name":"Credit Card","environment":"development"}}],"shipments": [{"id":1,"tracking":null,"number":"H65183147836","cost":"5.0","shipped_at":null,"state":"pending","order_id":"R123456789","stock_location_name":"default","shipping _rates":[{"id":1,"cost":"5.0","selected":true,"shipment_id":1,"shipping_method_id":1}, {"id":2,"cost":"10.0","selected":false,"shipment_id":1,"shipping_method_id":2}, {"id":3,"cost":"15.0","selected":false,"shipment_id":1,"shipping_method_id":3}],"shipping_method":{"name":"UPS Ground (USD)"},"inventory_units": [{"id":1,"state":"on_hand","variant_id":1,"shipment_id":1,"return_authorization_id":null}]}],"adjustments": [{"id":1,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount ":"0.8","label":"North America 5.0%","mandatory":false,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":3,"source_type":"Spree::Shipment","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id": 1,"amount":"5.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":5,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount" :"0.8","label":"Tax","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":7,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id":1," amount":"0.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}],"credit_cards": [{"id":1,"month":"12","year":"2014","cc_type":"visa","last_digits":"1111","first_name":"Sean","last_name":"Schofield","gateway_customer_profile_id":"BGS- 1234","gateway_payment_profile_id":null}]

Slide 23

Slide 23 text

How? ● Step 3: Write the endpoint

Slide 24

Slide 24 text

How? ● Step 3: Write the endpoint – Fake API

Slide 25

Slide 25 text

How? module DummyShip def self.validate_address(address) ## Zipcode must be within a given range. unless (20170..20179).to_a.include? (address['zipcode'].to_i) raise "This order is outside our shipping zone." end end end

Slide 26

Slide 26 text

How? ● Step 3: Write the endpoint – Fake API – Gemfile

Slide 27

Slide 27 text

How? ● Step 3: Write the endpoint – Fake API – Gemfile – config.ru

Slide 28

Slide 28 text

How? ● Step 3: Write the endpoint – Fake API – Gemfile – config.ru – custom_attribute_endpoint.rb

Slide 29

Slide 29 text

post '/get_biz_signer' do get_address begin result = @address['variety'] == "Business" ? "do" : "do not" process_result 200, { 'message_id' => @message[:message_id], 'message' => "notification:info", "payload" => { "result" => "You #{result} need to get a signature for this package." } } rescue Exception => e process_result 200, { 'message_id' => @message[:message_id], 'message' => "notification:error", "payload" => { "result" => e.message} } end end

Slide 30

Slide 30 text

Proof If residential address: $ curl --data @./residential_order.json -i -X POST -H 'Content-type:application/json' http://localhost:9292/get_biz_signer {"message_id":"12345","message":"notification:info", "payload":{"result": "You do not need to get a signature for this package."}}

Slide 31

Slide 31 text

Proof If business address: $ curl --data @./business_order.json -i -X POST -H 'Content-type:application/json' http://localhost:9292/get_biz_signer {"message_id":"12345","message":"notification:info", "payload":{"result": "You do need to get a signature for this package."}}

Slide 32

Slide 32 text

Where? ● The tutorial is available in the Integration guides, at: guides.spreecommerce.com/integration/custom_attributes_tutorial.html The sample files are available at: github.com/spree/integration_tutorials

Slide 33

Slide 33 text

No content