angkiki
angkiki

Reputation: 495

Cart checkout function without User logged in

Hi everybody i've searched everywhere over the web and can't seem to find an answer to this so here goes.

I have a website with Tutor Profiles being listed. The function i would like to achieve is to allow users to be able to shortlist users and have a "checkout" function. After adding a number of tutors into their "cart", they would proceed to "checkout". When they do that, they will be prompted to enter in their Name/Email/PhoneNumber. An email would then be sent to me with the list of Tutors that they have shortlisted along with their information.

I have tried researching on similar "Shopping Cart" functionalities but most of them seem to have a user currently logged in. Also i can't seem to figure out how to process this "Checkout" function and have that information sent to me after the User has shortlisted all their desired Tutors.

All solutions and help would be greatly appreciated! Thank you very much!

Upvotes: 2

Views: 1302

Answers (2)

faron
faron

Reputation: 1059

As it was already mentioned you can use guest user or store info in session (both approaches are quite similar actually).

With guest user you will still store his id in session (warden does that for you) and a record in DB (that will be used only while user session in browser is open). If you want, you can use other table/model for this purpose and store it's id in user session on your own. Though guest user has benefit when you have both registered and not registered users, your logic will be quite the same. You've mentioned that you can do shopping on amazon without registration, but guest user strategy also doesn't require registration and it's you who decides how to show it to user (whether it's Hello guest user #123 or just keep link Sign In)

Things to keep in mind:

  • you may want to 'cleanup' old guest users to prevent users table from endless grow.
  • you may want to add logic 'merging' guest user info with existing user (ex. if someone forgot to login, picked some items and then logged in)

For session storage you'll need to switch from CookiesStorage to smth with bigger capacity (cookies are 4KB at most, right?). Options are DatabaseStorage, or memcached, or redis, etc

Things to keep in mind:

  • you won't need to do cleanup
  • you may still want to add logic 'merging' guest user info with existing user (ex. if someone forgot to login, picked some items and then logged in)
  • if you want to handle both registered and guest users this way will lead to many conditionals in code, so guest user would be a better option

Upvotes: 2

max
max

Reputation: 102134

One strategy is to create a guest user record when the user first hits the site.

Here is a basic example using Warden.

class User < ActiveRecord::Base
  has_secure_password, validations: false
  enum status: [:guest, :registered] # ...

  validates :email, presence: true, unless: :guest?
  validates :password, presence: true, confirmation: true, unless: :guest?
end

class ApplicationController < ActionController::Base
  helper_method :signed_in?, :current_user

  prepend_before_action :authenticate!
  before_action :create_guest_user!, unless: :signed_in?

  def create_guest_user!
    warden.set_user( User.create!(status: :guest) )
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user
    request.env['warden'].user
  end

  def authenticate!
    warden.authenticate!
  end
end

Then when the user completes the checkout you would update the users.status column to show that they are a bona-fide user.

Of course this will create a bunch of guest records that may never be used that might need to be cleaned out with something like a rake task.

namespace :users do
  desc "Cleans out guest records"
  task :cull => :environment do
    User.guest.where('created_at > ?' 1.month.ago).destroy_all
  end
end

Another approach is to store the data in the session - usually this would require using a session store such as memcached to avoid the browser cookie size limits.

Upvotes: 2

Related Questions