From d0e625322f69bd231932abb22cb121e5fd358e4c Mon Sep 17 00:00:00 2001 From: Eric Saupe Date: Thu, 13 Jun 2019 13:56:59 -0400 Subject: [PATCH] Added verified purchaser One feature that many other eCommerce sites have is the concept of a "verified purchaser" which is when someone leaves a review for a product they verifiably purchased. In the implementation here we add a toggle on whether to display the verfied purchaser on the frontend, a column on the database to store if the review is from a verified purchaser, and logic when creating the review to set that value. --- app/helpers/spree/reviews_helper.rb | 9 +++++ app/models/spree/review.rb | 18 ++++++++-- app/models/spree/reviews_configuration.rb | 3 ++ .../spree/admin/review_settings/edit.html.erb | 6 ++++ app/views/spree/admin/reviews/index.html.erb | 11 ++++--- app/views/spree/shared/_review.html.erb | 3 ++ config/locales/de-CH.yml | 2 ++ config/locales/de.yml | 2 ++ config/locales/en-GB.yml | 2 ++ config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ config/locales/fr.yml | 2 ++ config/locales/it.yml | 2 ++ config/locales/pl.yml | 2 ++ config/locales/pt-BR.yml | 2 ++ config/locales/pt.yml | 2 ++ config/locales/ro.yml | 2 ++ config/locales/ru.yml | 2 ++ config/locales/sv.yml | 2 ++ config/locales/tr.yml | 2 ++ config/locales/uk.yml | 2 ++ config/locales/zh-CN.yml | 2 ++ config/locales/zh-TW.yml | 2 ++ ...65528_add_verified_purchaser_to_reviews.rb | 5 +++ spec/helpers/review_helper_spec.rb | 2 +- spec/models/review_spec.rb | 33 +++++++++++++++++++ spec/models/reviews_configuration_spec.rb | 6 ++++ 27 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20190613165528_add_verified_purchaser_to_reviews.rb diff --git a/app/helpers/spree/reviews_helper.rb b/app/helpers/spree/reviews_helper.rb index a226088..b1f4f98 100644 --- a/app/helpers/spree/reviews_helper.rb +++ b/app/helpers/spree/reviews_helper.rb @@ -14,4 +14,13 @@ def txt_stars(n, show_out_of = true) res end + def display_verified_purchaser?(review) + Spree::Reviews::Config[:show_verified_purchaser] && review.user && + Spree::LineItem.joins(:order, :variant) + .where.not(spree_orders: { completed_at: nil }) + .find_by( + spree_variants: { product_id: review.product_id }, + spree_orders: { user_id: review.user_id } + ).present? + end end diff --git a/app/models/spree/review.rb b/app/models/spree/review.rb index 627b8e8..47e7e2c 100644 --- a/app/models/spree/review.rb +++ b/app/models/spree/review.rb @@ -3,6 +3,7 @@ class Spree::Review < ActiveRecord::Base belongs_to :user, :class_name => Spree.user_class.to_s has_many :feedback_reviews + before_create :verify_purchaser after_save :recalculate_product_rating, :if => :approved? after_destroy :recalculate_product_rating @@ -10,13 +11,13 @@ class Spree::Review < ActiveRecord::Base validates :review, presence: true validates :rating, numericality: { only_integer: true, - greater_than_or_equal_to: 1, + greater_than_or_equal_to: 1, less_than_or_equal_to: 5, message: :you_must_enter_value_for_rating } default_scope { order("spree_reviews.created_at DESC") } - + scope :localized, ->(lc) { where('spree_reviews.locale = ?', lc) } scope :most_recent_first, -> { order('spree_reviews.created_at DESC') } scope :oldest_first, -> { reorder('spree_reviews.created_at ASC') } @@ -37,4 +38,17 @@ def recalculate_product_rating def email user.try!(:email) end + + def verify_purchaser + return unless user_id && product_id + + verified_purchase = Spree::LineItem.joins(:order, :variant) + .where.not(spree_orders: { completed_at: nil }) + .find_by( + spree_variants: { product_id: product_id }, + spree_orders: { user_id: user_id } + ).present? + + self.verified_purchaser = verified_purchase + end end diff --git a/app/models/spree/reviews_configuration.rb b/app/models/spree/reviews_configuration.rb index bc4961a..13503af 100644 --- a/app/models/spree/reviews_configuration.rb +++ b/app/models/spree/reviews_configuration.rb @@ -12,6 +12,9 @@ def self.boolean_preferences # show a reviewer's email address preference :show_email, :boolean, :default => false + # show if a reviewer actually purchased the product + preference :show_verified_purchaser, :boolean, :default => false + # show helpfullness rating form elements preference :feedback_rating, :boolean, :default => false diff --git a/app/views/spree/admin/review_settings/edit.html.erb b/app/views/spree/admin/review_settings/edit.html.erb index 8f58475..ceb20e8 100644 --- a/app/views/spree/admin/review_settings/edit.html.erb +++ b/app/views/spree/admin/review_settings/edit.html.erb @@ -32,6 +32,12 @@ <%= I18n.t("spree.spree_reviews.show_email") %> +
+ +