Sha256: ea0f680f8586f622dfeca7dc1ba23af8d8750d2806d31f98dc250772a059a455

Contents?: true

Size: 1.51 KB

Versions: 37

Compression:

Stored size: 1.51 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(common_product_cache_keys + ["spree/structured-data/#{product.cache_key_with_version}"]) 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: structured_sku(product),
          offers: {
            '@type': 'Offer',
            price: product.default_variant.price_in(current_currency).amount,
            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_sku(product)
      product.default_variant.sku? ? product.default_variant.sku : product.sku
    end

    def structured_images(product)
      image = default_image_for_product_or_variant(product)

      return '' unless image

      main_app.rails_blob_url(image.attachment)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
spree_frontend-4.1.15 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.7 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.3 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.7.0 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.6.0 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.5.0 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.6 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.2 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.4.0 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.0 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.0.rc3 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.0.rc2 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.3.0.rc1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.5 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.1.14 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.4 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.3.1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.1.13.1 app/helpers/spree/structured_data_helper.rb
spree_frontend-4.2.3 app/helpers/spree/structured_data_helper.rb