<?xml version="1.0" encoding="UTF-8"?>
<!--
	Large-catalog version of microsoft-merchant-center-tab.xml. Data Builder stores
	the finished tab-delimited row of every product and variant and recomputes a row only when that
	product, its variants, stock or images change. The feed only reads the stored rows.

	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">

		<!-- Data Builder: stores the finished row of every product and variant. The field is
		     listed on the MivaLabs Data Builder tab under Settings > Utilities. -->
		<Module code="mivalabsfeedbuilder" feature="util">
			<Field_Add>
				<Code>microsoft_merchant_data</Code>
				<Name>Microsoft Merchant Center Data</Name>
				<SourceType>Product</SourceType>
				<Trigger>microsoft_merchant_data_trig</Trigger>
			</Field_Add>
		</Module>

		<Page_Update code="FEEDDATAFIELD_microsoft_merchant_data">
			<Item code="feeddatafield_settings">
				<Format>csv</Format>
				<Delimiter>tab</Delimiter>
				<ExpandVariants>Yes</ExpandVariants>
				<FieldMap>
					<Field source="code" output="id" />
					<Field source="name" output="title" />
					<Field level="master" source="descrip" output="description" transform="strip_html" />
					<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="in_stock" output="availability">
						<Condition field="in_stock" operator="equals" value="in stock" output="in_stock" />
						<ConditionsDefault>out_of_stock</ConditionsDefault>
					</Field>
					<Field source="price" output="price" />
					<Field source="sale_price" output="sale_price">
						<Condition field="sale_price" operator="is_empty" output="" />
					</Field>
					<Field level="master" source="brand" output="brand" />
					<Field source="gtin" output="gtin">
						<Condition field="gtin" operator="is_empty" output="" />
					</Field>
					<Field source="sku" output="mpn" />
					<Field level="master" source="identifier_exists" output="identifier_exists">
						<Condition field="identifier_exists" operator="is_empty" output="" />
					</Field>
					<Field source="__constant__" output="condition" constant="new" />
					<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 level="master" source="gender" output="gender">
						<Condition field="gender" operator="is_empty" output="" />
					</Field>
					<Field level="master" source="age_group" output="age_group">
						<Condition field="age_group" operator="is_empty" output="" />
					</Field>
					<Field level="master" source="google_product_category" output="google_product_category">
						<Condition field="google_product_category" operator="is_empty" output="" />
					</Field>
					<Field level="master" source="category_path" output="product_type" />
					<Field source="shipping_length" output="shipping_length">
						<Condition field="shipping_length" operator="is_empty" output="" />
					</Field>
					<Field source="shipping_width" output="shipping_width">
						<Condition field="shipping_width" operator="is_empty" output="" />
					</Field>
					<Field source="shipping_height" output="shipping_height">
						<Condition field="shipping_height" operator="is_empty" output="" />
					</Field>
				</FieldMap>
			</Item>
		</Page_Update>

		<!-- Queued Products: recomputes products as they change (the field's Trigger runs it), and
		     finishes a whole-store refresh the All Products task starts. Leave it enabled. -->
		<ScheduledTask_Add>
			<Description><![CDATA[Update Microsoft Merchant Center Data (Queued Products)]]></Description>
			<Enabled>Yes</Enabled>
			<Trigger>microsoft_merchant_data_trig</Trigger>
			<Module>mivalabsfeedbuilder</Module>
			<Operation>databuilder_recompute</Operation>
			<RunInterval units="manual" />
			<Settings>
				<FieldCode>microsoft_merchant_data</FieldCode>
				<Generation>queued</Generation>
			</Settings>
		</ScheduledTask_Add>

		<!-- All Products: computes every product and variant. Installed disabled. Run it once after
		     importing this file (Settings > Store Settings > Scheduled Tasks, select it, Run
		     Scheduled Task(s)); after that, only when the whole store's data needs a refresh, for
		     example after changing the field's mapping. One run is enough: whatever it doesn't finish
		     in its time limit (4 minutes per run), the Queued Products task finishes automatically, so
		     expect the Queued Products task to run right after this one, until every product is done. -->
		<ScheduledTask_Add>
			<Description><![CDATA[Rebuild Microsoft Merchant Center Data (All Products)]]></Description>
			<Enabled>No</Enabled>
			<Module>mivalabsfeedbuilder</Module>
			<Operation>databuilder_recompute</Operation>
			<RunInterval units="manual" />
			<Settings>
				<FieldCode>microsoft_merchant_data</FieldCode>
				<Generation>all</Generation>
			</Settings>
		</ScheduledTask_Add>

		<!-- Feed Builder: writes the stored rows as the feed, one line per product or variant.
		     Its rebuild waits 5 minutes after a change so the stored rows are updated first. -->
		<Feed_Add>
			<Code>microsoft_merchant_tab_feed</Code>
			<Name>Microsoft Merchant Center Feed (Tab)</Name>
			<Module>mivalabsfeedbuilder</Module>
			<Settings>
				<Trigger>microsoft_merchant_tab_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>/microsoft-merchant-center.txt</Name>
			</File>
		</Feed_Add>

		<Page_Update code="FEEDBUILDER_microsoft_merchant_tab_feed">
			<Item code="feedbuilder_page_builder">
				<SourceType>product</SourceType>
				<Format>csv</Format>
				<Delimiter>tab</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="microsoft_merchant_data" output="row" transform="raw_row" />
				</FieldMap>
				<HeaderText><![CDATA[id{{tab}}title{{tab}}description{{tab}}link{{tab}}image_link{{tab}}additional_image_link{{tab}}availability{{tab}}price{{tab}}sale_price{{tab}}brand{{tab}}gtin{{tab}}mpn{{tab}}identifier_exists{{tab}}condition{{tab}}item_group_id{{tab}}color{{tab}}size{{tab}}material{{tab}}pattern{{tab}}gender{{tab}}age_group{{tab}}google_product_category{{tab}}product_type{{tab}}shipping_length{{tab}}shipping_width{{tab}}shipping_height{{newline}}]]></HeaderText>
			</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 Microsoft Merchant Center Feed (Tab)]]></Description>
			<Enabled>Yes</Enabled>
			<Trigger delay="300">microsoft_merchant_tab_feed_trig</Trigger>
			<Module>stdschtasks</Module>
			<Operation>feed</Operation>
			<RunInterval units="fixeddaily">
				<Hour>2</Hour>
				<Minute>0</Minute>
			</RunInterval>
			<Settings>
				<Feed>microsoft_merchant_tab_feed</Feed>
			</Settings>
		</ScheduledTask_Add>

	</Store>
</Provision>
