Brilliant Guide on Upgrading Spree 1.3.3 to 2.0.4

This is the best guide I have seen for upgrading Spree from 1.3.3 to 2.0.4

HOWTO: Upgrade Spree 1.3.3 to 2.0.4

These steps were sufficient to upgrade Cult Cosmetics to Spree 2.0.4 from 1.3.3. Here are some details on our environment.

  • We only have single-variant products
  • We are not making use of stock locations (single warehouse)
  • Frontend entirely overridden w/ custom bootstrap layout
  • Braintree gateway
  • Rails 3.2.14
  • Upgraded from Ruby 1.9.3-p448 to 2.0.0-p247 in the process (no issues)

Pull requests are encouraged to update this with steps that I missed!

Lessons learned (the hard way)

  1. MailMethods have been refactored. This breaks your existing mail config.
  2. Shipping Calculators have been refactored. This breaks your existing shipping calculators.
  3. Shipping Category is now required for all products and shipping methods. Checkout will just stop at the delivery step with no explanation if you don't have this configured.
  4. Lots has been changed in the frontend code, which has been refactored out into the spree_frontend gem. If you're overriding the views, there is plenty more to do that I'll document later, although I probably only scratched the surface.

Steps I followed

  1. Update Spree and extensions in your Gemfile
gem 'spree', '~> 2.0.4'
# Should work for most extensions
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '2-0-stable'
  1. bundle install (or just bundle if you're super-cool)
  2. bundle exec rake railties:install:migrations && bundle exec rake db:migrate
  3. Edit app/assets/javascripts/admin/all.js
-//= require admin/spree_core
-//= require admin/spree_promo
+//= require admin/spree_backend
  1. Edit app/assets/javascripts/store/all.js
-//= require admin/spree_core
-//= require admin/spree_promo
+//= require admin/spree_backend
  1. Edit app/assets/stylesheets/admin/all.css
- *= require admin/spree_core
- *= require admin/spree_promo
+ *= require admin/spree_backend
  1. Edit app/assets/stylesheets/admin/all.css
- *= require admin/spree_core
- *= require admin/spree_promo
+ *= require admin/spree_frontend
  1. Shipments need a stock location and your existing shipments won't have one. rails g migration AddStockLocationToShipments

Should contain: (obviously this is more complicated if you're actually planning on using multiple Stock Locations)

location = Spree::StockLocation.find_by_name('default')
Spree::Shipment.update_all(stock_location_id: location.id)
  1. You need a Shipping Category if you don't already have one (they weren't required before 2.0). Every Product && Shipping Method needs a shipping category assigned to it as well. rails g migration CreateDefaultShippingCategory

Should contain: ```ruby default_category = Spree::ShippingCategory.first default_category ||= Spree::ShippingCategory.create!(:name => "Default")

Spree::ShippingMethod.all.each do |method|
  method.shipping_categories << default_category if method.shipping_categories.blank?
end

Spree::Product.where(shipping_category_id: nil).update_all(shipping_category_id: default_category.id)
```
  1. New Products also need a shipping category but this isn't obvious in the Admin. I chose to set it before a new Product is created in app/controllers/admin/products_controller_decorator.rb

    Spree::Admin::ProductsController.class_eval do
    
      new_action.before :set_shipping_category
    
      private
    
      def set_shipping_category
        @product.shipping_category = Spree::ShippingCategory.first # or whichever category your heart desires
      end
    end
  2. This one is a doozy. Spree 2.0 uses new Shipping Calculators that are separate from the old Spree::Calculator classes. This insane migration will recreate your calculators using the new models and copy over your old preferences. rails g migration MigrateOldShippingCalculators

    Spree::ShippingMethod.all.each do |shipping_method|
      old_calculator = shipping_method.calculator
      next if old_calculator.class < Spree::ShippingCalculator # We don't want to mess with new shipping calculators
      new_calculator = eval("Spree::Calculator::Shipping::#{old_calculator.class.name.demodulize}").new
      new_calculator.preferences.keys.each do |pref|
        # Preferences can't be read/set by name, you have to prefix preferred_
        pref_method = "preferred_#{pref}"
        new_calculator.send("#{pref_method}=", old_calculator.send(pref_method))
      end
      new_calculator.save
      shipping_method.calculator = new_calculator
    end

See this comment if you have custom shipping calculators: https://gist.github.com/swrobel/6343549#comment-918763

  1. Mail Methods have also been refactored, breaking your existing mail config. I prefer to just set up the ActionMailer config in my environment config files (production.rb, development.rb, etc) and use the new setting to prevent spree from overriding ActionMailer like so:

    # config/initializers/spree.rb
    Spree.config do |config|
      config.override_actionmailer_config = false
      config.mails_from = 'Your Company <mail@yourcompany.com>'
      # Set domain for emails
      ActionMailer::Base.default_url_options[:host] = config.site_url
    end

Otherwise, just make sure to go into the Admin and set up your Mail config again for 2.0

  1. Replace any instances of t(:translation) in your code with Spree.t(:translation)

  2. If you were using the Spree::Config.show_zero_stock_products = false option, use my extension to bring it back:

gem 'spree_zero_stock_products'
```ruby
# config/initializers/spree.rb
Spree.config do |config|
  config.show_zero_stock_products = false # Default is true
end
```
  1. If you were using the Spree::Config.allow_backorders = false option, rails g migration SetBackorderableToFalse:
Spree::StockItem.update_all(backorderable: false)
Spree::StockLocation.update_all(backorderable_default: false)

Shanghai Internet speed - Updated for 2013

Well things have got better in Shanghai, when I left 18 months ago I have 2M ADSL, then in Hangzhou we got 10M ADSL which they then decided was too fast and said their equipment could only handle 4M so after the end of the first year they downgraded us. Now back in Shanghai we have 20M fiber to the building (FTTB). It flies! I have also noticed that Akamai is now doing a much better job in China, Apple App Store updates are now whizzing down the line at nearly the full 20M.

We launched our wholesale site - Zooron.com

Its been a busy few months, first Gina and I moved back to Shanghai after finding a great apartment here, then we launched our first B2B wholesale site Zooron.com. We launched Zooron because we are fed up of people getting ripped off when buying from China. China is an amazing place with a massive selection of products ready to be sold by retailers but we think it needs someone to organise and safely dispatch those products. We are launching with 300 products, mainly in the business and industrial categories and plan to add many more from our trusted supplier we have worked with over the last 9 years.

Developing a new business park, China style

The pace that China is expanding and growing is still staggering, even after eight years of living here. Sometimes it sneaks up on you, over lunch we realised that the business park, well really a business district, we have our office in has really taken off in the last 6 months. We have been here for just over a year and when we moved in we were one of the first, when you looked up and down the street there was no one about, most of the buildings you see today were there but no one was working in them, one of those ghost towns you hear about, everything was ready and waiting to take off.

Fast forward a year and there are now signs of life but its taken a lot to kick start, for example people don’t get here by themselves they are all bused in and out of this side of town, so at 5.30 the whole street is lined up and down with buses, there is only one convenience store in a 1km radius and we have to go into Alibaba’s corporate office to have a coffee. Now that the larger offices have staff we are seeing the street vendors turn up with their electric trikes serving dirt cheap Chinese food, 6RMB gets you any noodles or fried rice dish and 1.5RMB gets you a north west China style flat bread not unlike a plain naan with a few sesame seeds thrown in for good measure.

The only downside of moving in early is that we got stung with higher than now the average rent, we have 2,000 m2 office / warehouse on the ground floor which we rent for 1.6RMB per m2 per day which as far as rents go is not bad but we learn now the average for the area is 1RMB / m2 / day which would also explain why businesses are moving in now, rather than 18 months ago when these buildings were finished and how they can afford to bus everyone over from down town, have a look at the map to see the commute, it should take 30 mins but in rush hour its more like an hour each way.


View Larger Map

Which is more or less like it works back in the UK, I remember people catching the staff bus from the Leeds city center to the business parks outside the city each day for work.

Getting RVM

I’m not sure where to put this, but I wanted to share this tidbit of info.

I was having an issue where I would see all these hanging /bin/bash processes calling runner and rake that were being fired off by cron. Turns out my .rvmrc file in my app folder was causing the problem. Rvm by default will prompt to trust or not trust the file. This causes bash to hang. The solution I used was to disable the prompt by adding this line to my user rvm file in ~/.rvmrc

rvm_trust_rvmrcs_flag=1

Have a read of the RVM Wiki

Upgrading Spree from 1.1.2 to 1.1.3

If you upgraded Spree from 1.1.2 to 1.1.3 and found that the checkout didn’t work with some error about zones

1
2
3
4
ActiveRecord::StatementInvalid (PG::Error: ERROR:  column "zone_members_count" does not exist
LINE 1: ...LECT "spree_zones".* FROM "spree_zones" ORDER BY zone_membe...
^
: SELECT "spree_zones".* FROM "spree_zones" ORDER BY zone_members_count, created_at):

Then perhaps you missed that you needed to run some migrations

1
2
rake railties:install:migrations
rake db:migrate

Google Analytics Funnel Setup for Spree Commerce

During the setup of any new ecommerce system you will need to monitor your checkout for cart abandonment to find the drop off points and carefully fix them, here is how I setup analytics for Spree Commerce 1.1.x.

  • Goal Type: URL Destination
  • Goal URL: /orders/R[0-9]{1,8}
  • Use funnel: Ticked
  • Required Step: Ticked
  • Step 1: /checkout
  • Step 2: /checkout/delivery
  • Step 3: /checkout/payment

Thanks to Ryan for the RegEx

Non-www to www nginx redirect

Add this small block of code into your server{} block to redirect all non-www to the www version of your website.

1
2
3
if ($host != 'www.samhamilton.co.uk' ) {
rewrite ^/(.*)$ http://www.samhamilton.co.uk/$1 permanent;
}

Proudly powered by Hexo and Theme by Hacker
© 2024 Sam Hamilton