Product Catalog for Facebook

Use XMLWriter

	//https://developers.facebook.com/docs/marketing-api/dynamic-product-ads/product-catalog#feed-format
	function catalog(){
		$writer = new XMLWriter();  
		$writer->openURI('php://output');  
		$writer->startDocument('1.0','UTF-8');  
		$writer->setIndent(4);   
		$writer->startElement('rss');
			$writer->writeAttribute('xmlns:g', 'http://base.google.com/ns/1.0');
			$writer->writeAttribute('version', '2.0');

			$site = $this->options_m->get_site_settings(2);
			$writer->startElement('channel');
				$writer->writeElement('title', $site->title);
				$writer->writeElement('link', 'https://wecharming.life');
				$writer->writeElement('description', $site->site_desc);

				$prods = $this->products_m->catalog();
				foreach ($prods as $key => $prod) {
					$writer->startElement('item');
						$writer->writeElement('g:id', $prod->id);
						$writer->writeElement('g:title', $prod->title);
						$writer->writeElement('g:description', $prod->summary);
						$writer->writeElement('g:link', base_url() . 'shop/product/' . $prod->id);
						$writer->writeElement('g:image_link', $prod->photo);
						$writer->writeElement('g:brand', 'WeCharming');
						$writer->writeElement('g:condition', 'new');
						$writer->writeElement('g:availability', 'in stock');
						$writer->writeElement('g:price', $prod->price . ' TWD');
						
						//https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt
						$writer->writeElement('g:google_product_category', 'Health & Beauty > Personal Care > Cosmetics > Skin Care');
					$writer->endElement();
				}
			$writer->endElement();

		$writer->endElement();
		$writer->endDocument();
		$writer->flush();
	}