Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Basic support for open graph meta tags for PDP

Nathaniel Nande603adc48ac4e
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index aa21bb6eff..26e76cb633 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -57,8 +57,33 @@ module Spree
end
end
+ def object
+ instance_variable_get('@' + controller_name.singularize)
+ end
+
+ def og_meta_data
+ og_meta = {}
+
+ if object.is_a? Spree::Product
+ image = default_image_for_product_or_variant(object)
+ og_meta['og:image'] = main_app.url_for(image.attachment) if image&.attachment
+
+ og_meta['og:url'] = spree.url_for(object) if frontend_available? # url_for product needed
+ og_meta['og:type'] = object.class.name.demodulize.downcase
+ og_meta['og:title'] = object.name
+ og_meta['og:description'] = object.description
+
+ price = object.price_in(current_currency)
+ if price
+ og_meta['product:price:amount'] = price.amount
+ og_meta['product:price:currency'] = current_currency
+ end
+ end
+
+ og_meta
+ end
+
def meta_data
- object = instance_variable_get('@' + controller_name.singularize)
meta = {}
if object.is_a? ApplicationRecord
@@ -82,16 +107,10 @@ module Spree
meta
end
- def meta_image_url_path
- object = instance_variable_get('@' + controller_name.singularize)
- return unless object.is_a?(Spree::Product)
-
- image = default_image_for_product_or_variant(object)
- image&.attachment.present? ? main_app.url_for(image.attachment) : asset_path(Spree::Config[:logo])
- end
-
- def meta_image_data_tag
- tag('meta', property: 'og:image', content: meta_image_url_path) if meta_image_url_path
+ def og_meta_data_tags
+ og_meta_data.map do |property, content|
+ tag('meta', property: property, content: content) unless property.nil? || content.nil?
+ end.join("\n")
end
def meta_data_tags
diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb
index 3114d733ba..c32d304d94 100644
--- a/core/spec/helpers/base_helper_spec.rb
+++ b/core/spec/helpers/base_helper_spec.rb
@@ -121,6 +121,39 @@ describe Spree::BaseHelper, type: :helper do
end
end
+ context 'og_meta_data_tags' do
+ let(:current_currency) { 'USD' }
+ let(:image) { create(:image, position: 1) }
+ let(:product) do
+ create(:product).tap { |product| product.master.images << image }
+ end
+
+ it 'renders open graph meta data tags for PDP' do
+ # Because the controller_name method returns "test"
+ # controller_name is used by this method to infer what it is supposed
+ # to be generating og_meta_data_tags for
+ @test = product
+ tags = Nokogiri::HTML.parse(og_meta_data_tags)
+
+ meta_image = tags.css('meta[property="og:image"]').first['content']
+ meta_type = tags.css('meta[property="og:type"]').first['content']
+ meta_title = tags.css('meta[property="og:title"]').first['content']
+ meta_description = tags.css('meta[property="og:description"]').first['content']
+ meta_price_amount = tags.css('meta[property="product:price:amount"]').first['content']
+ meta_price_currency = tags.css('meta[property="product:price:currency"]').first['content']
+
+ expect(meta_image).to be_present
+
+ expect(meta_type).to eq('product')
+ expect(meta_title).to eq(product.name)
+ expect(meta_description).to eq(product.description)
+
+ default_price = product.master.default_price
+ expect(meta_price_amount).to eq(default_price.amount.to_s)
+ expect(meta_price_currency).to eq(default_price.currency)
+ end
+ end
+
# Regression test for #5384
context 'pretty_time' do
@@ -272,55 +305,4 @@ describe Spree::BaseHelper, type: :helper do
end
end
end
-
- describe '#meta_image_data_tag' do
- context 'when meta_image_url_path is present' do
- it 'returns meta tag' do
- allow_any_instance_of(Spree::BaseHelper).to receive(:meta_image_url_path).and_return('image_url')
-
- expect(meta_image_data_tag).to eq "<meta property=\"og:image\" content=\"image_url\" />"
- end
- end
-
- context 'when meta_image_url_path is absent' do
- it 'returns meta tag' do
- allow_any_instance_of(Spree::BaseHelper).to receive(:meta_image_url_path).and_return(nil)
-
- expect(meta_image_data_tag).to eq nil
- end
- end
- end
-
- describe '#meta_image_url_path' do
- context 'when object is not a product' do
- let!(:taxon) { build :taxon }
-
- it 'returns false' do
- allow_any_instance_of(Spree::BaseHelper).to receive(:instance_variable_get).and_return(taxon)
-
- expect(meta_image_url_path).to eq nil
- end
- end
-
- context 'when object is product' do
- let!(:product) { build :product }
- before do
- allow_any_instance_of(Spree::BaseHelper).to receive(:instance_variable_get).and_return(product)
- end
-
- context 'and has no images attached' do
- it 'returns spree logo url' do
- expect(meta_image_url_path).to eq asset_path(Spree::Config[:logo])
- end
- end
-
- context 'and has image attached' do
- let!(:image) { create :image, viewable: product.master }
-
- it 'returns main image url' do
- expect(meta_image_url_path).to eq asset_path(main_app.url_for(image.attachment))
- end
- end
- end
- end
end
diff --git a/frontend/app/views/spree/shared/_head.html.erb b/frontend/app/views/spree/shared/_head.html.erb
index 5a842f0dfb..700739c7f9 100644
--- a/frontend/app/views/spree/shared/_head.html.erb
+++ b/frontend/app/views/spree/shared/_head.html.erb
@@ -1,6 +1,6 @@
<title><%= title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
-<%== meta_image_data_tag %>
+<%== og_meta_data_tags %>
<%== meta_data_tags %>
<%= canonical_tag(current_store.url) %>
<%= favicon_link_tag 'favicon.ico' %>