Skip to content

Instantly share code, notes, and snippets.

@mkllnk
Last active August 4, 2022 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkllnk/5f50a11ea6f780e4f769330d668390d1 to your computer and use it in GitHub Desktop.
Save mkllnk/5f50a11ea6f780e4f769330d668390d1 to your computer and use it in GitHub Desktop.
Exporting in a format that's fit for importing products
producer sku name display_name category description units unit_type variant_unit_name price on_hand available_on on_demand shipping_category tax_category
Freddy's Farm Shop Fuji Apple Fruit 1 g 5.00 0 2022-07-27 01:59:03.412543 1 Default Tax Category
Freddy's Farm Shop Mushrooms Fungi 1 g 50.00 0 2022-07-27 01:59:03.540333 1 Default Tax Category
Fredo's Farm Hub Carrots Vegetables 1 g 3.00 0 2022-07-27 01:59:03.662664 1 Default Tax Category
Fredo's Farm Hub Potatoes Vegetables 1 g 2.00 0 2022-07-27 01:59:03.78174 1 Default Tax Category
Fredo's Farm Hub Tomatoes Vegetables 1 g 2.00 0 2022-07-27 01:59:03.900057 1 Default Tax Category
Fred's Farm TestSKU Garlic Vegetables 1 g 20.00 0 2022-07-27 01:59:03.212884 1 Default Tax Category
-- copy (
SELECT
e.name as producer,
v.sku,
p.name,
v.display_name,
t.name as category,
p.description,
v.unit_value as units,
case
when p.variant_unit = 'weight' and p.variant_unit_scale = 1 then 'g'
when p.variant_unit = 'weight' and p.variant_unit_scale = 1000 then 'kg'
when p.variant_unit = 'weight' and p.variant_unit_scale = 1000000 then 'T'
when p.variant_unit = 'weight' and p.variant_unit_scale = 28.35 then 'oz'
when p.variant_unit = 'weight' and p.variant_unit_scale = 453.6 then 'lb'
when p.variant_unit = 'volume' and p.variant_unit_scale = 0.001 then 'mL'
when p.variant_unit = 'volume' and p.variant_unit_scale = 1 then 'L'
when p.variant_unit = 'volume' and p.variant_unit_scale = 1000 then 'kL'
else 'items'
end as unit_type,
p.variant_unit_name,
pr.amount as price,
s.count_on_hand as on_hand,
p.available_on,
CASE
WHEN s.backorderable=TRUE THEN 1
ELSE 0
END AS on_demand,
sc.name as shipping_category,
tc.name as tax_category
from spree_variants as v
left join spree_products as p on v.product_id = p.id
left join enterprises as e on p.supplier_id=e.id
left join spree_taxons as t on p.primary_taxon_id= t.id
left join spree_stock_items as s on v.id = s.variant_id
left join spree_prices as pr on v.id = pr.variant_id
left join spree_shipping_categories as sc on p.shipping_category_id = sc.id
left join spree_tax_categories as tc on p.tax_category_id = tc.id
where p.supplier_id in (4,2,3)
and v.is_master = false
and v.deleted_at is null
-- ) to '/tmp/output.csv' with CSV HEADER;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment