Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify Spree promotion coupon from an external API

I want to apply a coupon to my spree application but the coupon should be verified from an external API

I searched through the docs as well as tutorials but I haven't found anything which can help me

The requirement is something like this:

  • I am selling a product and I want to give a discount of 10% to members of some organization.

  • On payment page, the user will enter his email address and I want to verify that email from API provided by organization

I am referring this right now

1 . Promotions

EDIT:

I am currently looking into Promotion rules and Promotion Handlers

  • Registering a New Rule
  • Promotion Handler - Coupon
like image 289
Deepak Mahakale Avatar asked Oct 30 '22 14:10

Deepak Mahakale


1 Answers

You can create a new user rule. In app/models/spree/promotion/rules/organisational_user.rb,

module Spree
  class Promotion
    module Rules
      class OrganisationalUser < PromotionRule
        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          # hit external API to verify user existence
        end
      end
    end
  end
end

Register this rule, In spree.rb,

Rails.application.config.after_initialize do
  Rails.application.config.spree.promotions.rules << Spree::Promotion::Rules::OrganisationalUser
end

Then add requisite view files and translation for this rule. And that should be it. Now you can use that rule in conjunction with any other rule and action.

like image 54
Priyank Gupta Avatar answered Nov 15 '22 07:11

Priyank Gupta