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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add allow list for optional attributes in google merchant center (#14332)
Damian Legawieca4b0c77e8f97
diff --git a/spree/core/app/presenters/spree/data_feeds/google_presenter.rb b/spree/core/app/presenters/spree/data_feeds/google_presenter.rbindex ca19dcafde..6efb66d4c1 100644--- a/spree/core/app/presenters/spree/data_feeds/google_presenter.rb+++ b/spree/core/app/presenters/spree/data_feeds/google_presenter.rb@@ -3,6 +3,22 @@ require 'nokogiri'module Spreemodule DataFeedsclass GooglePresenter < BasePresenter+ # Optional Google Merchant Center product attributes sourced from+ # metafields. See https://support.google.com/merchants/answer/7052112+ OPTIONAL_ATTRIBUTES = %w[+ brand gtin mpn identifier_exists condition adult multipack is_bundle+ age_group color gender material pattern size size_type size_system+ product_length product_width product_height product_weight+ google_product_category product_type sale_price sale_price_effective_date+ cost_of_goods_sold unit_pricing_measure unit_pricing_base_measure+ shipping shipping_label shipping_weight shipping_length shipping_width+ shipping_height ships_from_country transit_time_label max_handling_time+ min_handling_time tax tax_category energy_efficiency_class+ min_energy_efficiency_class max_energy_efficiency_class+ gtin_source expiration_date custom_label_0 custom_label_1 custom_label_2+ custom_label_3 custom_label_4 mobile_link additional_image_link+ ].freeze+# @return [String] RSS XML feed for Google Merchant Centerdef callbuilder = Nokogiri::XML::Builder.new do |xml|@@ -59,10 +75,21 @@ module Spreedef build_optional_attributes(xml, product)product.public_metafields.each do |metafield|- xml['g'].send(metafield.metafield_definition.key.parameterize.underscore, metafield.value)+ key = metafield.metafield_definition.key.parameterize.underscore+ next unless OPTIONAL_ATTRIBUTES.include?(key)++ append_g_element(xml, key, metafield.value)endend+ def append_g_element(xml, name, value)+ parent = xml.parent+ node = Nokogiri::XML::Node.new(name, parent.document)+ node.namespace = parent.namespace_scopes.find { |ns| ns.prefix == 'g' }+ node.content = value+ parent << node+ end+def format_title(product, variant)parts = [product.name]variant.option_values.each do |option_value|diff --git a/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rb b/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rbindex 206093fb36..3b1a999f1b 100644--- a/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rb+++ b/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rb@@ -114,6 +114,42 @@ RSpec.describe Spree::DataFeeds::GooglePresenter doendend+ context 'metafield with a key that is not a known Google attribute' do+ let(:product) { create(:product) }+ let(:metafield_definition) { create(:metafield_definition, name: 'Internal', key: 'internal_note') }+ let!(:metafield) { create(:metafield, resource: product, metafield_definition: metafield_definition, value: 'secret') }++ it 'omits the metafield from the feed' do+ expect(xml).not_to include('internal_note')+ expect(xml).not_to include('secret')+ end+ end++ context 'metafield whose key collides with a Ruby method name' do+ let(:product) { create(:product) }+ let(:metafield_definition) do+ create(:metafield_definition, name: 'System', key: 'brand').tap do |definition|+ definition.update_column(:key, 'system')+ end+ end+ let!(:metafield) { create(:metafield, resource: product, metafield_definition: metafield_definition, value: 'id') }++ it 'does not dispatch the key as a method and omits it from the feed' do+ expect(xml).not_to include('<g:system>')+ end+ end++ context 'metafield whose key matches a required attribute' do+ let(:product) { create(:product) }+ let(:metafield_definition) { create(:metafield_definition, name: 'Item Group', key: 'item_group_id') }+ let!(:metafield) { create(:metafield, resource: product, metafield_definition: metafield_definition, value: 'injected') }++ it 'does not emit the metafield value, only the required item_group_id' do+ expect(xml).to include("<g:item_group_id>#{product.id}</g:item_group_id>")+ expect(xml).not_to include('<g:item_group_id>injected</g:item_group_id>')+ end+ end+context 'title generation does not mutate product name' doit 'generates independent titles for multiple variants' dooption_type = create(:option_type)