<?xml version="1.0" encoding="UTF-8"?>
<!--
	Meta (Facebook and Instagram) catalog, CSV. In Commerce Manager open the catalog,
	Data sources, Add items, Data feed, and schedule https://www.your-domain.com/meta-catalog.csv
	daily. Meta requires id, title, description, availability, condition, price, link, image_link
	and brand (or gtin/mpn). Availability is "in stock" / "out of stock", which is what the in_stock
	field produces.

	Before importing:
	  - Replace YOUR_STORE_CODE with your store code.
	  - Change source="brand" to your brand custom field code, or to
	    <Field source="__constant__" output="..." constant="Your Brand" /> if every product has one brand.
	  - Main Image uses the image type with code "main". Additional Images uses images with no image
	    type; list more image type codes in image_types (comma-separated, up to 10) to include them.
	  - Prices are written as amount and currency (for example "15.90 USD") from the store's currency.
	    sale_price is the price group discount for a shopper without an account (as Template Based
	    Feed predicts it), left out when there is none. Run the "Pre-Calculate Discount Prices"
	    scheduled task on large catalogs so it is read instead of worked out per product.
	  - Shipping dimensions come from each product's shipping rules, with the store's dimension unit.
	  - The feed reads Available products: active and in no availability group, since channels
	    reject products shoppers can't buy. Change the Load tag to type="active" to include all.

	Install MivaLabs Feed Builder under Settings > Utilities before importing; the import does not
	install it.

	Import: Admin > Data Management > Import > XML Provisioning.
	For the *-data-builder.xml files: after importing, run the "Rebuild ... (All Products)" scheduled
	task once to compute every product, then the feed fills in automatically.
	The All Products task works for up to 4 minutes per run. If products are still left, it starts
	the "Update ... (Queued Products)" task, which keeps going 4 minutes at a time until every
	product is done (about 11 minutes for 57,000 products and variants). So the Queued Products
	task running right after All Products is expected, and the rebuild is done when it stops.
	Don't run All Products again in the meantime; if you do, it continues where it left off.
-->
<Provision>
	<Store code="YOUR_STORE_CODE">

		<Feed_Add>
			<Code>meta_catalog_feed</Code>
			<Name>Meta Catalog Feed</Name>
			<Module>mivalabsfeedbuilder</Module>
			<Settings>
				<Trigger>meta_catalog_feed_trig</Trigger>
			</Settings>
			<!-- Where and whether the file is saved is set here, as for any Miva feed. A folder in the
			     name (/feeds/name.csv) must already exist in the web root. -->
			<File>
				<Enabled>Yes</Enabled>
				<Location>script</Location>
				<Name>/meta-catalog.csv</Name>
			</File>
		</Feed_Add>

		<Page_Update code="FEEDBUILDER_meta_catalog_feed">
			<Item code="feedbuilder_page_builder">
				<SourceType>product</SourceType>
				<Format>csv</Format>
				<Delimiter>comma</Delimiter>
				<LineEnding>lf</LineEnding>
				<!-- Available: active products in no availability group, so every shopper can buy them.
				     Use type="active" to include every active product instead. -->
				<Load type="available" />
				<ExpandVariants>Yes</ExpandVariants>
				<Filter mode="all">
					<Rule field="price" operator="greater_than" value="0" />
				</Filter>
				<FieldMap>
					<Field source="code" output="id" />
					<Field source="name" output="title" />
					<Field source="descrip" output="description" transform="strip_html" />
					<Field source="in_stock" output="availability" />
					<Field source="__constant__" output="condition" constant="new" />
					<Field source="price" output="price" />
					<Field source="sale_price" output="sale_price">
						<Condition field="sale_price" operator="is_empty" output="" />
					</Field>
					<Field source="product_url" output="link" />
					<Field source="__main_image__" image_type="main" output="image_link" />
					<Field source="__additional_images__" image_types="__untyped__" output="additional_image_link" />
					<Field source="brand" output="brand" />
					<Field source="item_group_id" output="item_group_id" />
					<Field source="color" output="color">
						<Condition field="color" operator="is_empty" output="" />
					</Field>
					<Field source="size" output="size">
						<Condition field="size" operator="is_empty" output="" />
					</Field>
					<Field source="material" output="material">
						<Condition field="material" operator="is_empty" output="" />
					</Field>
					<Field source="pattern" output="pattern">
						<Condition field="pattern" operator="is_empty" output="" />
					</Field>
					<Field source="category_path" output="product_type" />
				</FieldMap>
			</Item>
		</Page_Update>

		<!-- Rebuilds the file every night at 2:00, and shortly after product, stock, variant or image
		     changes (Feed Builder fires this trigger when they happen). -->
		<ScheduledTask_Add>
			<Description><![CDATA[Build Meta Catalog Feed]]></Description>
			<Enabled>Yes</Enabled>
			<Trigger>meta_catalog_feed_trig</Trigger>
			<Module>stdschtasks</Module>
			<Operation>feed</Operation>
			<RunInterval units="fixeddaily">
				<Hour>2</Hour>
				<Minute>0</Minute>
			</RunInterval>
			<Settings>
				<Feed>meta_catalog_feed</Feed>
			</Settings>
		</ScheduledTask_Add>

	</Store>
</Provision>
