Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Latest commit

 

History

History
72 lines (56 loc) · 1.7 KB

File metadata and controls

72 lines (56 loc) · 1.7 KB

README

This README has step by step instructions how to integrate Talkable referral platform into your spree store.

Getting started

Add talkable gem into your Gemfile:

gem 'talkable'

Run the bundle install command to install it.

Create a file config/initializers/talkable.rb and configure gem:

Talkable.configure do |config|
  config.site_slug  = 'YOUR-SITE-SLUG-AT-TALKABLE-COM'
  config.api_key    = 'YOUR-API-KEY-AT-TALKABLE-COM'
end

by default this configuration takes from environment variables ENV["TALKABLE_SITE_SLUG"] and ENV["TALKABLE_API_KEY"].

Modify your state machine by additional action after comlete state:

Spree::Order.state_machine.after_transition to: :complete, do: :register_at_talkable

Add register_at_talkable method to your Spree::Order model:

Spree::Order.class_eval do
  def register_at_talkable
    Talkable::API::Origin.create(Talkable::API::Origin::PURCHASE, talkable_params)
  end

  protected

  def talkable_params
    {
      email: email,
      order_number: number,
      subtotal: total,

      shipping_zip: ship_address&.zipcode,
      shipping_address: [
        ship_address&.address1,
        ship_address&.address2,
        ship_address&.city,
        ship_address&.state_name,
        ship_address&.zipcode,
        ship_address&.country&.name,
      ].reject(&:blank?).join(', '),
      coupon_code: promotions.map(&:code),
      customer_id: user_id,
      order_date: completed_at.iso8601,
      ip_address: last_ip_address,

      items: line_items.map do |item|
        {
          price: item.price,
          quantity: item.quantity,
          product_id: item.variant.sku,
        }
      end
    }
  end
end