<?xml version="1.0" encoding="UTF-8"?>
<!--
	Google Merchant Center (RSS 2.0 with the g: namespace). In Merchant Center add a
	product source of type "Add products from a file", choose "Enter a link to your file" and use
	https://www.your-domain.com/google-merchant-center.xml with a daily fetch.

	g:brand, g:gtin, g:identifier_exists, g:gender, g:age_group and g:google_product_category come
	from product custom fields with those codes. Import google-merchant-custom-fields.xml first to
	create them, then fill them in on each product (a variant's part product can have its own GTIN).
	Each is left out of a row when it is empty. g:mpn is mapped from the product SKU; change
	source="sku" to source="mpn" to use the mpn custom field instead. g:color, g:size, g:material and
	g:pattern come from the variant attributes with those codes (each variant's option prompt, e.g.
	"Red"); change source= to your own attribute codes.

	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>google_merchant_feed</Code>
			<Name>Google Merchant Center Feed</Name>
			<Module>mivalabsfeedbuilder</Module>
			<Settings>
				<Trigger>google_merchant_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>/google-merchant-center.xml</Name>
			</File>
		</Feed_Add>

		<Page_Update code="FEEDBUILDER_google_merchant_feed">
			<Item code="feedbuilder_page_builder">
				<SourceType>product</SourceType>
				<Format>xml</Format>
				<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="g:id" />
					<Field source="name" output="title" />
					<Field source="descrip" output="description" transform="strip_html" />
					<Field source="product_url" output="link" />
					<Field source="__main_image__" image_type="main" output="g:image_link" />
					<Field source="__additional_images__" image_types="__untyped__" output="g:additional_image_link" />
					<Field source="in_stock" output="g:availability">
						<Condition field="in_stock" operator="equals" value="in stock" output="in_stock" />
						<ConditionsDefault>out_of_stock</ConditionsDefault>
					</Field>
					<Field source="price" output="g:price" />
					<Field source="sale_price" output="g:sale_price">
						<Condition field="sale_price" operator="is_empty" output="" />
					</Field>
					<Field source="brand" output="g:brand" />
					<Field source="gtin" output="g:gtin">
						<Condition field="gtin" operator="is_empty" output="" />
					</Field>
					<Field source="sku" output="g:mpn" />
					<Field source="identifier_exists" output="g:identifier_exists">
						<Condition field="identifier_exists" operator="is_empty" output="" />
					</Field>
					<Field source="__constant__" output="g:condition" constant="new" />
					<Field source="item_group_id" output="g:item_group_id" />
					<Field source="color" output="g:color">
						<Condition field="color" operator="is_empty" output="" />
					</Field>
					<Field source="size" output="g:size">
						<Condition field="size" operator="is_empty" output="" />
					</Field>
					<Field source="material" output="g:material">
						<Condition field="material" operator="is_empty" output="" />
					</Field>
					<Field source="pattern" output="g:pattern">
						<Condition field="pattern" operator="is_empty" output="" />
					</Field>
					<Field source="gender" output="g:gender">
						<Condition field="gender" operator="is_empty" output="" />
					</Field>
					<Field source="age_group" output="g:age_group">
						<Condition field="age_group" operator="is_empty" output="" />
					</Field>
					<Field source="google_product_category" output="g:google_product_category">
						<Condition field="google_product_category" operator="is_empty" output="" />
					</Field>
					<Field source="category_path" output="g:product_type" />
					<Field source="shipping_length" output="g:shipping_length">
						<Condition field="shipping_length" operator="is_empty" output="" />
					</Field>
					<Field source="shipping_width" output="g:shipping_width">
						<Condition field="shipping_width" operator="is_empty" output="" />
					</Field>
					<Field source="shipping_height" output="g:shipping_height">
						<Condition field="shipping_height" operator="is_empty" output="" />
					</Field>
				</FieldMap>
				<HeaderText><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>{{store_name}}</title>
<link>{{store_url}}</link>
<description>Google Merchant Center product feed</description>
]]></HeaderText>
				<FooterText><![CDATA[</channel>
</rss>]]></FooterText>
			</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 Google Merchant Center Feed]]></Description>
			<Enabled>Yes</Enabled>
			<Trigger>google_merchant_feed_trig</Trigger>
			<Module>stdschtasks</Module>
			<Operation>feed</Operation>
			<RunInterval units="fixeddaily">
				<Hour>2</Hour>
				<Minute>0</Minute>
			</RunInterval>
			<Settings>
				<Feed>google_merchant_feed</Feed>
			</Settings>
		</ScheduledTask_Add>

	</Store>
</Provision>
