How to access Spree's link_to_cart function from main rails app

和自甴很熟 提交于 2019-12-04 09:49:52

Ok, thanks Ben for getting me on the right track. Here was my solution:

# class ApplicationController < ActionController::Base
   include Spree::Core::ControllerHelpers::Order
   include Spree::Core::ControllerHelpers::Auth
   helper Spree::BaseHelper
   helper Spree::StoreHelper

Update

I ran into an issue with current_store being undefined outside of the engine. I'm not sure how to solve this properly, but in the meantime I've just added the following to stop spree from calling current_store:

module Spree
  module Core
    module ControllerHelpers
      module Order
        def current_order_params
          { currency: current_currency, guest_token: cookies.signed[:guest_token], store_id: Spree::Store.first, user_id: try_spree_current_user.try(:id) }
        end
      end
    end
  end
end

Also helper Spree::StoreHelper seems to no longer be required to display the cart button and current orders..

You need to do two things

  1. Create your override
  2. Make your override accessible within the main app

Step 1: My Spree overrides are in the overrides folder in my main app. The override should call module_eval on the module that you are decorating.

#overrides/base_helper_decorator.rb
Spree::BaseHelper.module_eval do

  def link_to_cart(text = nil)
    #Your customizations here
  end

end

Step 2: Add one of the lines below to your main app ApplicationController to access your decorated helper.

helper_method :link_to_cart # Add only the link_to_cart method 

helper 'spree/base'         # Add all methods (your decoration plus the default methods) 
                            # from the Spree BaseHelper module to your main app

There have been some updates since Abram left his answer. It got me on the right track but I had a few hiccups. The main one was current_currency being undefined.

application_controller.rb

class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Common
  helper Spree::BaseHelper

I overrode the Spree navbar and used the one for my main app.

It started working when I added include Spree::Core::ControllerHelpers::Common. Unfortunately, this renders all views through the spree spree_application.html.erb layout. You may have to override and tinker a bit with this view.

Also, all of the css comes from spree at this point. You'll have to move your custom css into the spree namespace and @import it.

You can add this to your main_app to access the cart.

<%= link_to "Cart", spree.cart_path %>

You will have to include the right helpers in your main application outside the engine too. In the upcoming version 3.2.0 of spree I just had to add:

# class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth

to my ApplicationController to make link_to_cart work everywhere. You'll need to include more then one helper class, because for example current_store (used by other helpers) is defined in Spree::Core::ControllerHelpers::Store, so add that too to get rid of any errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!