vinsol-spree-contrib / spree_product_subscriptions

SpreeCommerce product subscription extension lets customers subscribe for a product and get it delivered at the same time every week, month or year over a predefined period of time.
http://vinsol.com/spreecommerce-subscription
MIT License
10 stars 57 forks source link

After migration to 3.7.2, subscription doesn't work #64

Open hardreggaecafe opened 4 years ago

hardreggaecafe commented 4 years ago

I have a question. I modified spree_product_subscriptions for spree 3.7.2 by folking below. https://github.com/hardreggaecafe/spree_product_subscriptions/tree/migrate_for_3.7.2 Some of feature is working, but when I add subscribed products to cart, subscription doesn't work correctly(see below). Ruby on Rails Bag - Spree Demo Site Shopping Cart - Spree Demo Site

How should I fix this problems? Tell me please.

mjuneja commented 4 years ago

@sawangupta92 Can someone look at this and respond.

sawangupta92 commented 4 years ago

@mjuneja sure, we will looking into it

sawangupta92 commented 4 years ago

@hardreggaecafe You are working with an older version of this extension. We had upgraded this extension a while back, here is the PR for the same https://github.com/vinsol-spree-contrib/spree_product_subscriptions/pull/58/files. Please try this branch. You will need to do some changes in the "Appraisals" file to accommodate it for Spree 3.7. But there shouldn't be any big challenge there.

Please let us know if you get into any other issue.

hardreggaecafe commented 4 years ago

@sawangupta92 Oops! I didn't know that. I'll check it later. I'll let you know how it turns out. Thanks.

hardreggaecafe commented 4 years ago

@sawangupta92 I applied what you suggested earlier. Here's the one. https://github.com/hardreggaecafe/spree_product_subscriptions/tree/migrate_for_3.7.2 But regrettably, still same. Any idea?

sawangupta92 commented 4 years ago

@hardreggaecafe We have reproduced the issue and unfortunately this extension is only working fine for spree versions up to 3.5.

Spree 3.7 is using the latest V2 API for adding the line items to an order, which is not supported in this extension as of now.

We do plan to upgrade this extension in the future. Meanwhile, if you have already fixed the issues in upgrades, you can raise a pull request and we will review and merge it.

For pointers, you can check the OrdersController in the extension. That should help you in debugging & fixing the issue.

hardreggaecafe commented 4 years ago

@sawangupta92 Oh, I knew it. Every time I press 'subscribe' button, spree calls '/api/v2/storefront/cart/add_item' API. I felt bit strange and confused why this could happen in this gem. Therefore, could you give me some more advise how to fix this gem if I do it by myself? What about adding 'app/controllers/spree/api/v2/storefront/cart_controller.rb' and adding some modification to 'add_item' like this?

          def add_item
            variant = Spree::Variant.find(params[:variant_id])

            spree_authorize! :update, spree_current_order, order_token
            spree_authorize! :show, variant

            result = add_item_service.call(
              order: spree_current_order,
              variant: variant,
              quantity: params[:quantity],
              options: params[:options]
            )
            #Add record to Spree:Subscription

            render_order(result)
          end
sawangupta92 commented 4 years ago

@hardreggaecafe Yes, this should be the starting point. I think that updating the params[:options] as per the #add_subscription_fields and permitting the params, should be the first step to solve the issue.

rajneeshsharma9 commented 4 years ago

@hardreggaecafe Assuming you haven't figured out the solution yet, I was looking into the fix for this and I could see the spree now uses an ajax request to add an item to cart. You are right in adding the subscription creation logic to "add_item". But you will need to pass the params to add_item action before you use them to actually create Spree::Subscription.

The starting point for this will be overwriting the behaviour of "product.js" in "spree_frontend" to allow it to get the subscription form elements and passing them as params to "SpreeAPI.Storefront.addToCart" method which is defined in "cart.js" in spree_api. Once you have the subscription params there you can either pass them to "Spree.routes.api_v2_storefront_cart_add_item"(add_item method of cart controller) or can even create you own action in "app/controllers/spree/api/v2/storefront/cart_controller.rb" to handle this.

And then the logic of creation of Spree::Subscription should work fine based on the value you have received in the params.

hardreggaecafe commented 4 years ago

@rajneeshsharma9 Thank you for the comment. Actually, I modified add_item and product.js. Also I'm fixing those file you mentioned above.

api/v2/storefront/cart_controller.rb

          def add_subscription_fields
            is_subscribed = params[:options].present? ? params[:options].fetch(:subscribe, "").present? : false

            existing_options = {options: (params[:options].present? ? params[:options] : {}).permit!}
            updated_subscription_params = (params[:options].present? ? params[:options] : {}).permit!

            existing_options[:options].merge!(updated_subscription_params)

Is it right modification of params?

And I can't figure out how to fix this. models/spree/order_contents.rb

 module Cart
    class AddItem
      prepend Spree::ServiceModule::Base

      def call(order:, variant:, quantity: nil, options: {})
        ApplicationRecord.transaction do
          run :add_to_line_item
          run Spree::Dependencies.cart_recalculate_service.constantize
        end
      end

      private

      def add_to_line_item(order:, variant:, quantity: nil, options: {})
        options ||= {}
        quantity ||= 1

        line_item = Spree::Dependencies.line_item_by_variant_finder.constantize.new.execute(order: order, variant: variant, options: options)

I'd appreciate if you give some comment.

hardreggaecafe commented 4 years ago

I applied some modifications on my forked brunch. Here's the one. https://github.com/hardreggaecafe/spree_product_subscriptions/tree/migrate_for_3.7.2 Does anyone can review it?

githubble01 commented 3 years ago

Hi @hardreggaecafe!

Just bumped into the same issue. So I thought I'd try your branch while I'm at it.

However, there seems to be a problem, so I thought I'd might as well drop you a line..

BR Georg

Showing ..../spree_product_subscriptions-spree_v3.7/app/views/spree/shared/_taxonomies.html.erb where line #4 raised: image

My gemfile is

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

gem 'spree', '= 3.7.2'
gem 'spree_auth_devise', '~> 3.5'
gem 'spree_gateway', '~> 3.4'

gem 'spree_product_subscriptions', github: 'hardreggaecafe/spree_product_subscriptions', branch: 'migrate_for_3.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]