Sha256: 08ce20dea0dc3de7498a15950540e27e026544b2eed283627c348845c2f45557

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

module Spree
  module StructuredDataHelper
    def products_structured_data(products)
      content_tag :script, type: 'application/ld+json' do
        raw(
          products.map do |product|
            structured_product_hash(product)
          end.to_json
        )
      end
    end

    private

    def structured_product_hash(product)
      Rails.cache.fetch("spree/structured-data/#{product.cache_key}") do
        {
          '@context': 'https://schema.org/',
          '@type': 'Product',
          '@id': "#{spree.root_url}product_#{product.id}",
          url: spree.product_url(product),
          name: product.name,
          image: structured_images(product),
          description: product.description,
          sku: product.sku,
          offers: {
            '@type': 'Offer',
            price: product.price,
            priceCurrency: current_currency,
            availability: product.in_stock? ? 'InStock' : 'OutOfStock',
            url: spree.product_url(product),
            availabilityEnds: product.discontinue_on ? product.discontinue_on.strftime('%F') : ''
          }
        }
      end
    end

    def structured_images(product)
      image = product.variant_images.first

      return '' unless image

      main_app.rails_blob_url(image.attachment)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spree_frontend-4.0.9 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.8 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.7.1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.7 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.6 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.5 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.4 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.3 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.2 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.0.0 app/helpers/spree/structured_data_helper.rb