Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Created February 25, 2011 13:55
Show Gist options
  • Save resistorsoftware/843812 to your computer and use it in GitHub Desktop.
Save resistorsoftware/843812 to your computer and use it in GitHub Desktop.
How to add Meta Descriptions to a Product
{% if template == 'product' %}
{% assign mf = product.metafields.meta_description %}
{% unless mf == empty %}
{% for mf in product.metafields.meta_description %}
<meta name="Fubar2 Ronnie James Dio" content="{{mf.last}}" />
{% endfor %}
{% endunless %}
{% endif %}
# snippet by Caroline...
# extended by Dave
require 'rubygems'
require 'shopify_api'
APIKEY = 'APIKEY'
PASSWORD = 'PASSWORD'
SHOPNAME = 'SHOPNAME'
# Telling your shop who's boss.
ShopifyAPI::Base.site = "http://#{APIKEY}:#{PASSWORD}@#{SHOPNAME}.myshopify.com/admin"
product_count = ShopifyAPI::Product.count
nb_pages = (product_count / 250.0).ceil
raise "Yo man. You don't have any product in your shop. duh!" if product_count.zero?
# these keys point at meta_descriptions. Keys are product ID numbers. You can get those pretty easily from your store export CSV.
my_products = {
"66655544" => "I am a meta_description for plan B from Outer Space C",
"23432144" => "You're looking at some pretty rich stuff here amigo, buy it",
"21212211" => "This is not antique in the sense that antique is antique"
}
start_time = Time.now
1.upto(nb_pages) do |page|
unless page == 1
stop_time = Time.now
processing_duration = stop_time - start_time
wait_time = CYCLE - processing_duration
sleep wait_time
start_time = Time.now
end
products = ShopifyAPI::Product.find( :all, :params => { :limit => 250, :page => page } )
products.each do |product|
# clear out any old meta_descriptions, so we can just save new and fresh ones
product.metafields.each do |mf|
if mf.namespace == 'meta_description'
ShopifyAPI::Metafield.delete(mf.id)
end
end
# create new meta_description for product
data = {
:namespace => 'meta_description',
:value_type => 'string',
:key => 'description',
:value => my_products[product.id.to_s]
}
mf = product.add_metafield(ShopifyAPI::Metafield.new(data))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment