Skip to content

Instantly share code, notes, and snippets.

@rsiddle
Created October 24, 2016 15:54
Show Gist options
  • Save rsiddle/50d7b814f455804517fa03afa007366b to your computer and use it in GitHub Desktop.
Save rsiddle/50d7b814f455804517fa03afa007366b to your computer and use it in GitHub Desktop.
Ecommerce Solidus SEO - XML Sitemap Generation
# Copyright (c) 2016, Ryan Siddle. Part of the http://merj.com/digital/solidus-seo
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SitemapGenerator::Sitemap.default_host = "http://#{Spree::Store.default.url}"
module SolidusSitemap::SolidusDefaults
include Spree::Core::Engine.routes.url_helpers
include Spree::BaseHelper # for meta_data
def add_taxon(taxon, options = {})
# Reduce the priority level as more children are added.
priority = ((options[:priority] * 0.9) || 0.9).round(2)
add(nested_taxons_path(taxon.permalink), options.merge!(priority: priority, lastmod: taxon.products.last_updated)) if taxon.permalink.present?
taxon.children.each { |child| add_taxon(child, options) }
end
end
SitemapGenerator::Sitemap.create do
Spree::Taxon.roots.each do |taxon|
group(sitemaps_path: 'sitemaps', filename: "category-#{taxon.name.downcase.gsub(/[^0-9A-Za-z]/, '')}") do
add(nested_taxons_path(taxon.permalink), { priority: 0.8, lastmod: taxon.products.last_updated }) if taxon.permalink.present?
taxon.children.each { |child| add_taxon(child, { priority: 0.8, lastmod: child.products.last_updated }) }
end
end
group(sitemaps_path: 'sitemaps', filename: 'products') do
add_products
end
group(sitemaps_path: 'sitemaps', filename: 'pages') do
add_pages
add_login
add_signup
add_account
add_password_reset
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment