Skip to content

Instantly share code, notes, and snippets.

@octave
Created November 22, 2018 02:07
Show Gist options
  • Save octave/ce779607954995606954983c51e8ad35 to your computer and use it in GitHub Desktop.
Save octave/ce779607954995606954983c51e8ad35 to your computer and use it in GitHub Desktop.
Basic Facebook CSV Product Feed Support for Solidus
class FacebookDataFeedController < ApplicationController
def export
@products = Spree::Product.available
respond_to do |format|
format.csv { send_data @products.to_fb_csv, filename: "facebook-data-feed.csv" }
end
end
end
Spree::Product.class_eval do
def self.to_fb_csv
variants = all.uniq.map {|p| p.variants.present? ? p.variants : p.variant }.flatten(1)
CSV.generate do |csv|
csv << ['id','title','availability','description','condition','price','link','image_link','brand']
variants.each do |v|
csv << [v.sku, v.name, v.in_stock? ? 'in stock' : 'out of stock', v.product.meta_description, 'new', v.price, ENV['BASE_URL'] + v.slug, v.display_image.attachment.url(:original), ENV['BRAND_NAME']]
end
end
end
end
Rails.application.routes.draw do
get '/facebook-data-feed' => 'facebook_data_feed#export'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment