<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Facebook Pixel conversion tracking for Dynamic Product Ads</name>
<version>1.2.1.5</version>
<code>1457131549756</code>
<author>Jorim van Hove</author>
<link>http://www.jorimvanhove.com/plugins/fbcp</link>

	<file path="catalog/controller/checkout/cart.php">
		<operation>
			<search index="0"><![CDATA[$json['total']]]></search>
			<add position="before" ><![CDATA[
			$store_id = $this->config->get('config_store_id');
			if ($store_id == 0 ) {
				$pixels = $this->config->get('config_pixel');
			} else {
				$this->load->model('setting/setting');
				$pixels =  $this->model_setting_setting->getConversionPixel($store_id);
			}
			
			if (!empty($pixels)) {
				foreach ($pixels as $pixel) {
					if ($pixel['category'] == 'adds_to_cart') {
						$json['pixel_id'] = $pixel['pixel_id'];
						$json['pixel_value'] = empty($product_info['special']) ? $product_info['price'] : $product_info['special'];
						$json['pixel_currency'] = $this->config->get('config_currency');
					}
				}
			}
			$json['product_id'] = $product_id;
			$json['product_name'] = $product_info['name'];
			$facebook_product_catalog_id = $this->config->get('config_fb_product_catalog_id');
			$json['product_catalog_id'] = !empty($facebook_product_catalog_id) ? $facebook_product_catalog_id : '' ;
			$json['pixel_value'] = empty($product_info['special']) ? $product_info['price'] : $product_info['special'];
			$json['pixel_currency'] = $this->config->get('config_currency');
			]]></add>
		</operation>
	</file>
	<file path="catalog/view/theme/*/template/product/product.tpl">
		<operation>
			<search><![CDATA[$('#cart > button').html]]></search>
			<add position="after"><![CDATA[
			if(json['product_id']) {
				fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
			}
			]]></add>
		</operation>
	</file>
	<file path="catalog/view/theme/journal/template/product/product.tpl">
		<operation>
			<search><![CDATA[$('#cart-total').html]]></search>
			<add position="after"><![CDATA[
			if(json['product_id']) {
				fbq(['track', 'AddToCart', { 'value' : '' + json['pixel_value'] + '', 'currency': '' + json['pixel_currency'] + '', content_ids: ['' + json['product_id'] + ''], content_type: 'product', content_name: ''+ json['product_name'] +'' , product_catalog_id:'' + json['product_catalog_id'] + ''}]);
			}
			]]></add>
		</operation>
	</file>
	<file path="catalog/controller/account/wishlist.php">
		<operation>
			<search index="0"><![CDATA[$json['total']]]></search>
			<add  position="before" ><![CDATA[			
			$json['product_id'] = $product_id;
			$json['product_name'] = $product_info['name'];
			$facebook_product_catalog_id = $this->config->get('config_fb_product_catalog_id');
			$json['product_catalog_id'] = !empty($facebook_product_catalog_id) ? $facebook_product_catalog_id : '' ;
			$json['pixel_value'] = empty($product_info['special']) ? $product_info['price'] : $product_info['special'];
			$json['pixel_currency'] = $this->config->get('config_currency');
			]]></add>
		</operation>
	</file>
	<file path="catalog/view/theme/*/template/checkout/checkout.tpl">
		<operation>
			<search index="1"><![CDATA[<?php if ($shipping_required) { ?>]]></search>
			<add position="before" ><![CDATA[			
			fbq(['track', 'AddPaymentInfo']);			
			]]></add>
		</operation>
		<operation>
			<search index="2"><![CDATA[<?php if ($shipping_required) { ?>]]></search>
			<add position="before" ><![CDATA[			
			fbq(['track', 'AddPaymentInfo']);			
			]]></add>
		</operation>
	</file>
	<file path="catalog/view/theme/*/template/common/header.tpl">
		<operation>
			<search><![CDATA[<head>]]></search>
			<add position="after"><![CDATA[
			<?php if (!empty($fb_pixel)) { echo ('<!-- Facebook Pixel Conversion Tracking -->' . $fb_pixel . '<!-- END Facebook Pixel Conversion Tracking --> '); } ?>
		]]></add>
		</operation>
	</file>
	<file path="catalog/controller/common/header.php">
		<operation>
			<search><![CDATA[// Menu]]></search>
			<add position="before"><![CDATA[
			
			$store_id = $this->config->get('config_store_id');
						
			if ($store_id == 0 ) {
				$facebook_pixel_id = $this->config->get('config_custom_audience_pixel');
				$product_catalog_id = $this->config->get('config_fb_product_catalog_id');
				$pixels = $this->config->get('config_pixel');
			} else {
				$this->load->model('setting/setting');
				$facebook_pixel_id = $this->model_setting_setting->getPixel($store_id);
				$product_catalog_id = $this->model_setting_setting->getCatalogID($store_id);
				$pixels =  $this->model_setting_setting->getConversionPixel($store_id);
			}
			
			$data['facebook_pixel_id'] = $facebook_pixel_id;
			$data['fb_currency'] = $fb_currency = $this->config->get('config_currency');
			
			$fb_pixel_code = "<script>
							!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
							n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
							n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
							t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
							document,'script','//connect.facebook.net/en_US/fbevents.js');
							fbq('init', '%s');";
							
			$fb_pixel_code_postfix = '</script>';
			
			/* URL path */
			
			$config_url = $this->config->get('config_url');
			$seo_urls = $this->config->get('config_seo_url');
			
			if(isset($this->request->get['route'])) {
				$route = $this->request->get['route'];
			} else {
				$route = '';
			}
			
			$data['route'] = $route;
			
			if (!empty($_SERVER['HTTPS'])) {
				$pre = 'https';
			} else { 
				$pre = 'http';
			}
			$full_url = $pre . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
					
			if ($server == $full_url) {
				$url_path =	"home";
			} elseif (substr($route, -7) == 'success' ) { 
				$url_path =	$route;
			} else {
				$url_path = substr(parse_url($_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"], PHP_URL_QUERY), strlen('=route'));
			}

			if (substr($url_path, -1) == '/') {
				$url_path = substr($url_path, 0, -1);
			}
			
			$data['url_path'] = $url_path;
			
			if (!empty($facebook_pixel_id)) {			
					$facebook_pixel = str_replace('%s', $facebook_pixel_id , $fb_pixel_code);
					$cart_products = '';
					$product_count = 0;
					$options = '';
					
					switch ($route) {
						case 'checkout/success' : 
							if (!empty($_SESSION['last_order_id'])) {
								// Complete Checkout
								$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE order_id = '" . (int)$_SESSION['last_order_id'] . "'");
								foreach($query->rows as $product) {
									$cart_products .= '\'' . $product['product_id'] . '\',';
									$product_count += $product['quantity'];
								}
					
								$query = $this->db->query("SELECT total FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$_SESSION['last_order_id'] . "'");
								if ($query->num_rows) {
									$order_value = $query->row['total'];
								} else { 
									$order_value = "0.00";
								}
					
								$options .= 'value: \'' . number_format($order_value,2,'.','') . '\'';
								$options .= ', currency: \'' . $fb_currency . '\'';
								$options .= ', num_items: \'' . $product_count . '\'';
								$options .= ', content_ids: [' . $cart_products . ']';
								$options .= ', content_type: \'product\'';
								$options .= !empty($product_catalog_id) ? ', product_catalog_id: \''. $product_catalog_id . '\'' : '';
					
								$event = 'fbq(\'track\', \'Purchase\', {' . $options . '});';
							}
							break;
						case 'product/product':
							// View Content
							$this->load->model('catalog/product');
							$product = $this->model_catalog_product->getProduct($this->request->get['product_id']);
												
							$options .= 'value: \'' . number_format((empty($product['special']) ? $product['price'] : $product['special']),2,'.','') . '\'';
							$options .= ', currency: \'' . $fb_currency . '\'';
							$options .= ', content_ids: [\'' . $product['product_id'] . '\']';
							$options .= ', content_name: \'' . htmlspecialchars($product['name']) . '\'';
							$options .= ', content_type: \'product\'';
							$options .= !empty($product_catalog_id) ? ', product_catalog_id:\''. $product_catalog_id . '\'' : '';
						
							$event = "fbq('track', 'ViewContent', {" . $options . "});";
							
							break;
							
						case 'quickcheckout/checkout';
							// Initiate Checkout
							$cart = $this->cart->getProducts();
							foreach($cart as $product) {
								$cart_products .= '\'' . $product['product_id'] . '\',';
							}
							$this->load->model('setting/setting');
							$options .= 'value: \'' . number_format($this->model_setting_setting->getCartTotal(),2,'.','') . '\'';
							$options .= ', currency: \'' . $fb_currency . '\'';
							$options .= ', content_ids: [' . $cart_products  . ']';
							$options .= ', num_items: \'' . $this->cart->countProducts() . '\'';
							$options .= ', content_type: \'product\'';
							$options .= !empty($product_catalog_id) ? ', product_catalog_id:\''. $product_catalog_id . '\'' : '';
						
							$event = "fbq('track', 'InitiateCheckout', {" . $options . "});";	
						
							break;
							
						case 'checkout/checkout';
							// Initiate Checkout
							$cart = $this->cart->getProducts();
							foreach($cart as $product) {
								$cart_products .= '\'' . $product['product_id'] . '\',';
							}
							$this->load->model('setting/setting');
							$options .= 'value: \'' . number_format($this->model_setting_setting->getCartTotal(),2,'.','') . '\'';
							$options .= ', currency: \'' . $fb_currency . '\'';
							$options .= ', content_ids: [' . $cart_products  . ']';
							$options .= ', num_items: \'' . $this->cart->countProducts() . '\'';
							$options .= ', content_type: \'product\'';
							$options .= !empty($product_catalog_id) ? ', product_catalog_id:\''. $product_catalog_id . '\'' : '';
						
							$event = "fbq('track', 'InitiateCheckout', {" . $options . "});";	
						
							break;
						case 'account/success';
							// Complete Registration
							$options .= 'value: 0.00';
							$options .= ', currency: \'' . $fb_currency . '\'';
						
							$event = "fbq('track', 'CompleteRegistration', {" . $options . "});";
						
							break;
						case 'affiliate/success';
							// Complete Registration
							$options .= 'value: 0.00';
							$options .= ', currency: \'' . $fb_currency . '\'';
						
							$event = "fbq('track', 'CompleteRegistration', {" . $options . "});";
						
							break;
								
						case 'product/search';
							// Search	
							if (!empty($this->request->get['search'])) {					
								$options .= ' value: 0.00';
								$options .= ', currency: \'' . $fb_currency . '\'';
								$options .= ', search_string: \'' . $this->request->get['search'] . '\'';
						
								$event = 'fbq(\'track\', \'Search\', {' . $options . '});';
							}
							break;
						
						default :
							// Default PageView, displayed on all pages
							$options .= ' value: 0.00';
							$options .= ', currency: \'' . $fb_currency . '\'';
							$event = 'fbq(\'track\', \'PageView\', {'. $options . '});';							
					
					}
					
				// Bring all parts together	
				if(!empty($event)) {
					$data['fb_pixel'] = $facebook_pixel . $event . $fb_pixel_code_postfix;
				}
			}
			
			]]></add>
		</operation>
	</file>
	<file path="catalog/model/setting/setting.php">
		<operation>
			<search><![CDATA[public function getSetting($code, $store_id = 0) {]]></search>
			<add position="before"><![CDATA[
		public function getCartTotal() {
			
			// Totals
			$order_data = array();

			$totals = array();
			$taxes = $this->cart->getTaxes();
			$total = 0;

			// Because __call can not keep var references so we put them into an array. 
			$total_data = array(
				'totals' => &$totals,
				'taxes'  => &$taxes,
				'total'  => &$total
			);
			
			$this->load->model('extension/extension');

			$sort_order = array();

			$results = $this->model_extension_extension->getExtensions('total');

			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
			}

			array_multisort($sort_order, SORT_ASC, $results);

			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					if (version_compare(VERSION, '2.3.0.0', '>=')) {
						$this->load->model('extension/total/' . $result['code']);
						$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
					} else {
						$this->load->model('total/' . $result['code']);
						$this->{'model_total_' . $result['code']}->getTotal($total_data);
					}
				}
			}

			$sort_order = array();

			foreach ($totals as $key => $value) {
				$sort_order[$key] = $value['sort_order'];
			}

			array_multisort($sort_order, SORT_ASC, $totals);

			//$order_data['totals'] = $totals;

			$cart_total = end($totals);
	
			return $cart_total['value'];

		}
		
		public function getPixel($store_id) {
			$query = $this->db->query("SELECT value FROM " . DB_PREFIX . "setting WHERE `store_id` = '" . (int)$store_id . "' AND `key` = 'config_custom_audience_pixel'");
			return empty($query->num_rows) ? false : $query->row['value'];
		}
		
		public function getConversionPixel($store_id) {
			$query = $this->db->query("SELECT value FROM " . DB_PREFIX . "setting WHERE `store_id` = '" . (int)$store_id . "' AND `key` = 'config_pixel'");
			
			if ($query->num_rows) {
				$pixel_data = $query->row['value'];
				return unserialize($pixel_data);
			} else {
				return false;
			}
		}
		
		public function getCatalogID($store_id) {
			$query = $this->db->query("SELECT value FROM " . DB_PREFIX . "setting WHERE `store_id` = '" . (int)$store_id . "' AND `key` = 'config_fb_product_catalog_id'");
			return empty($query->num_rows) ? false : $query->row['value'];
		}
			]]></add>
		</operation>
	</file>
	
	<file path="system/library/cart/cart.php">
		<operation>
			<search ><![CDATA[public function countProducts() {]]></search>
			<add position="before"><![CDATA[ 
			public function getTotalValue($order_id) {
				$query = $this->db->query("SELECT value FROM `" . DB_PREFIX . "order_total` WHERE `code` = 'sub_total' AND `order_id` = '" . $order_id . "'");
				return $query->row;
			}
  			]]></add>
		</operation>
	</file>
	<file path="catalog/model/checkout/order.php">
		<operation>
			<search><![CDATA[
			$order_info = $this->getOrder($order_id);
			]]></search>
			<add position="after"><![CDATA[
			$order_total = $this->cart->getTotalValue($order_id);
			$_SESSION["orderValue"] = $order_total['value'];
			$_SESSION['last_order_id'] = $order_id;
			]]></add>
		</operation>
	</file>
	<file path="admin/controller/setting/setting.php">
        <operation>
			<search><![CDATA[$data['tab_server'] = $this->language->get('tab_server');]]></search>
			<add  position="after"><![CDATA[
			$data['tab_facebook_pixel'] = $this->language->get('tab_facebook_pixel');
			$data['entry_facebook_pixel'] = $this->language->get('entry_facebook_pixel');
			$data['help_facebook_pixel'] = $this->language->get('help_facebook_pixel');
			$data['entry_fb_product_catalog_id'] = $this->language->get('entry_fb_product_catalog_id');
			$data['help_catalog_id'] = $this->language->get('help_catalog_id');
			
			$data['entry_pixel_ID'] = $this->language->get('entry_pixel_ID');
			$data['entry_pixel_category'] = $this->language->get('entry_pixel_category');
			$data['entry_pixel_path'] = $this->language->get('entry_pixel_path');
			$data['entry_pixel_value'] = $this->language->get('entry_pixel_value');
			$data['text_enabled'] = $this->language->get('text_enabled');
			$data['text_disabled'] = $this->language->get('text_disabled');
			
			$data['text_order_total'] = $this->language->get('text_order_total');
			$data['text_product_price'] = $this->language->get('text_product_price');
			$data['text_default_success'] = $this->language->get('text_default_success');
			$data['text_custom_success'] = $this->language->get('text_custom_success');
			$data['text_new_customer'] = $this->language->get('text_new_customer');
			$data['text_new_affiliate'] = $this->language->get('text_new_affiliate');
			$data['text_product_page'] = $this->language->get('text_product_page');
			$data['text_category_page'] = $this->language->get('text_category_page');
			$data['text_manufacturer_page'] = $this->language->get('text_manufacturer_page');
			$data['text_information_page'] = $this->language->get('text_information_page');
			
			$data['entry_status'] = $this->language->get('entry_status');
			$data['entry_sort_order'] = $this->language->get('entry_sort_order');
			
			$data['button_save'] = $this->language->get('button_save');
			$data['button_cancel'] = $this->language->get('button_cancel');
			$data['button_add_pixel'] = $this->language->get('button_add_pixel');
			$data['button_remove'] = $this->language->get('button_remove');
			
			$data['pixel_category'] = array (
					'checkouts' 	=> $this->language->get('entry_cat_checkouts'),
					'registrations' => $this->language->get('entry_cat_registrations'),
					'leads' 		=> $this->language->get('entry_cat_leads'),
					'key_page_views' => $this->language->get('entry_cat_keypageviews'),
					'adds_to_cart' 	=> $this->language->get('entry_cat_addstocart'),
					'other' 		=> $this->language->get('entry_cat_other')
			);
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[$data['header'] = $this->load->controller('common/header');]]></search>
			<add position="before"><![CDATA[
			
			if (isset($this->request->post['config_custom_audience_pixel'])) {
			$data['custom_audience_pixel'] = $this->request->post['config_custom_audience_pixel'];
			} elseif ($this->config->get('config_custom_audience_pixel')) { 
				$data['custom_audience_pixel'] = $this->config->get('config_custom_audience_pixel');
			} else {
				$data['custom_audience_pixel'] = '';
			}		
			
			if (isset($this->request->post['config_fb_product_catalog_id'])) {
			$data['config_fb_product_catalog_id'] = $this->request->post['config_fb_product_catalog_id'];
			} elseif ($this->config->get('config_fb_product_catalog_id')) { 
				$data['config_fb_product_catalog_id'] = $this->config->get('config_fb_product_catalog_id');
			} else {
				$data['config_fb_product_catalog_id'] = '';
			}
			
			if (isset($this->request->post['config_pixel'])) {
				$pixels_data = $this->request->post['config_pixel'];
			} elseif ($this->config->get('config_pixel')) {
				$pixels_data = $this->config->get('config_pixel');
			} else {
				$pixels_data = array();
			}
			
			if (!empty($pixels_data)) {
				foreach ($pixels_data as $key => $pixel) {
					if($pixel['category'] == 'key_page_views') {
						$data['pixels'][] = array(
							'key'       => $key,
							'pixel_id' 	=> $pixel['pixel_id'],
							'category'  => $pixel['category'],
							'view'		=> $pixel['view'],
							'view-name'	=> $pixel['view-name'],
							'path'    	=> $pixel['path'],
							'value' 	=> $pixel['value'],
							'status' 	=> $pixel['status']
						);
					} else {
						$data['pixels'][] = array(
							'key'       => $key,
							'pixel_id' 	=> $pixel['pixel_id'],
							'category'  => $pixel['category'],
							'path'    	=> $pixel['path'],
							'value' 	=> $pixel['value'],
							'status' 	=> $pixel['status']
						);
					}
				}
			} else {
				$data['pixels'] = array();
			}
			
			]]></add>
		</operation>	
	</file>
	<file path="admin/view/template/setting/setting.tpl">
			<operation>
				<search><![CDATA[<li><a href="#tab-server" data-toggle="tab"><?php echo $tab_server; ?></a></li>]]></search>
				<add position="after"><![CDATA[
				<li><a href="#tab-facebook-pixel" data-toggle="tab"><?php echo $tab_facebook_pixel; ?></a></li>
				]]></add>
			</operation>
			<operation>
				<search><![CDATA[</form>]]></search>
				<add position="before" offset="1"><![CDATA[
				<div class="tab-pane" id="tab-facebook-pixel">
					<p>To create your Facebook pixel:
						<ol>
						<li>Go to your Facebook Pixel tab in Ads Manager.</li>
						<li>Click <b>Create a Pixel</b>.</li>
						<li>Enter a name for your pixel. There's only one pixel per ad account, so choose a name that represents your business.</li>
						<li>Make sure you've checked the box to accept the terms.</li>
						<li>Click <b>Create Pixel</b>.</li>
						<li>Copy the Pixel ID and paste it here.</li>
						</ol>
						<a href="https://www.facebook.com/business/help/952192354843755" target="_blank">More</a>
					</p>
					<hr />
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-facebook-pixel"><span data-toggle="tooltip" data-html="true" data-trigger="click" title="<?php echo htmlspecialchars($help_facebook_pixel); ?>"><?php echo $entry_facebook_pixel; ?></span></label>
						<div class="col-sm-10">
						  <input name="config_custom_audience_pixel" type="text" placeholder="<?php echo $entry_facebook_pixel; ?>" id="input-facebook-pixel" class="form-control" value="<?php echo $custom_audience_pixel; ?>">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-fb-product-catalog-id"><span data-toggle="tooltip" data-html="true" data-trigger="click" title="<?php echo htmlspecialchars($help_catalog_id); ?>"><?php echo $entry_fb_product_catalog_id; ?></span></label>
						<div class="col-sm-10">
						  <input name="config_fb_product_catalog_id" type="text" placeholder="<?php echo $entry_fb_product_catalog_id; ?>" id="input-fb-product-catalog-id" class="form-control" value="<?php echo $config_fb_product_catalog_id; ?>">
						</div>
					</div>
					<hr />
					<p>v.1.2.1.5 - <a href="http://jorimvanhove.com" target="_blank">Jorim van Hove</a> © 2014-<?php echo(date('Y')); ?> - <a href="http://jorimvanhove.com/plugins/fbcp" target="_blank">Online documentation</a> - <a href="https://www.facebook.com/business/help/search/?query=facebook+pixel&ss=1450967422" target="_blank">Facebook Help Center</a></p>
				</div>
				]]></add>
			</operation>
			<operation>
				<search><![CDATA[<?php echo $footer; ?>]]></search>
				<add position="before"><![CDATA[
				<script type="text/javascript"><!--
					var pixel_row = <?php echo $pixel_row; ?>;

					// Functions
					
					function addPixel() {	
						html  = '<tbody id="pixel-row' + pixel_row + '">';
						html += '  <tr>';
						html += '    <td class="left"><input type="text" class="form-control" name="config_pixel[' + pixel_row + '][pixel_id]" value="" maxlength="13"/></td>'; 
						html += '	 <td class="left"><select id="category-' + pixel_row + '" name="config_pixel[' + pixel_row + '][category]" class="form-control" onchange="setPath(' + pixel_row + ');">';
						html += '	 	<option value="<?php echo 'checkouts'; ?>" ><?php echo $pixel_category['checkouts']; ?></option>';
						html += '		<option value="<?php echo 'registrations'; ?>" ><?php echo $pixel_category['registrations']; ?></option>';
						html += '		<option value="<?php echo 'leads'; ?>" ><?php echo $pixel_category['leads']; ?></option>';
						html += '		<option value="<?php echo 'key_page_views'; ?>" ><?php echo $pixel_category['key_page_views']; ?></option>';
						html += '		<option value="<?php echo 'adds_to_cart'; ?>"><?php echo $pixel_category['adds_to_cart']; ?></option>';
						html += '		<option value="<?php echo 'other'; ?>" ><?php echo $pixel_category['other']; ?></option>';
						html += '    </select></td>';
						html += '	 <td class="left" id="pixel-path-input-' + pixel_row + '"><input type="text" id="pixel-path-' + pixel_row + '" class="form-control"  name="config_pixel[' + pixel_row + '][path]" value="" size="40" /></td>';
						html += '	 <td class="left"><input type="text" id="pixel-value-' + pixel_row + '" class="form-control"  name="config_pixel[' + pixel_row + '][value]" value="" size="10" /></td>';
						html += '    <td class="left"><select name="config_pixel[' + pixel_row + '][status]" class="form-control" >';
						html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
						html += '      <option value="0"><?php echo $text_disabled; ?></option>';
						html += '    </select></td>';
						html += '    <td class="left"><a onclick="$(\'#pixel-row' + pixel_row + '\').remove();" class="btn btn-danger"><i class="fa fa-minus-circle"></i></a></td>';
						html += '  </tr>';
						html += '</tbody>';
	
						$('#fcpixel tfoot').before(html);
						
						$(function () {
							setPath(pixel_row);
						});
						
						pixel_row++;
					}
					
					function setPath(row) {
						
						var category = $('#category-' + row).val();
						
						resetInputs(row, category);
						
						if (category === 'checkouts') {
							addCheckoutSelect(row);
							setCheckoutPath(row);
							$('#checkout-' + row).change();
						}
						
						if (category === 'registrations') {
							addRegSelect(row);
							setRegistrationPath(row);
							$('#registration-' + row).change();
						}					
						
						if (category === 'leads') {
							setLeadsPath(row);
						}
						
						if (category === 'key_page_views') {
							addPageViewsSelect(row);
							setPageViewsPath(row);
							$('#key-page-views-' + row).change();
							
						}
						
						if (category === 'adds_to_cart') {
						 	setCartAddsPath(row);
						}
					}
					
					function addCheckoutSelect(row) {
						html  = '<div>';
						html += '<select id="checkout-' + row + '" onchange="setCheckoutPath(' + row + ');" class="form-control">';
						html += '<option value="default"><?php echo $text_default_success; ?></option> ';
						html += '<option value="custom"><?php echo $text_custom_success; ?></option> ';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
					}
					
					function setCheckoutPath(row) {
						var checkoutselect = $('#checkout-' + row).val();
						
						if (checkoutselect === 'default') {
							$('#pixel-path-' + row).val('checkout/success');
							$('#pixel-path-' + row).prop('readonly', true);
						} else {
							$('#pixel-path-' + row).val('');
							$('#pixel-path-' + row).prop('readonly', false);
						}
						
						$('#pixel-value-' + row).val('<?php echo $text_order_total; ?>');
						$('#pixel-value-' + row).prop('readonly', true);
					}
					
					function setRegistrationPath(row) {
						var registrationselect = $('#registration-' + row).val();
						
						$('#pixel-value-' + row).prop('readonly', false).val('');
							if (registrationselect === 'account') {
								$('#pixel-path-' + row).val('account/success');
								$('#pixel-path-' + row).prop('readonly', true).addClass('form-control');
								
							} else if (registrationselect == 'affiliate') {
								$('#pixel-path-' + row).val('affiliate/success');
								$('#pixel-path-' + row).prop('readonly', true).addClass('form-control');
							}		
					}
					
					function addRegSelect(row) {
						html  = '<div>';
						html += '<select id="registration-' + row + '" onchange="setRegistrationPath(' + row + ');" class="form-control">';
						html += '<option value="account"><?php echo $text_new_customer; ?></option> ';
						html += '<option value="affiliate"><?php echo $text_new_affiliate; ?></option> ';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
					}
					
					function setLeadsPath(row) {
					
					}
					
					function setPageViewsPath(row) {
						
						
					}
					
					function setCartAddsPath(row) {
						$('#pixel-path-' + row).val('');
						$('#pixel-path-' + row).prop('readonly', true);
						$('#pixel-value-' + row).val('<?php echo $text_product_price; ?>');
						$('#pixel-value-' + row).prop('readonly', true);
					}
					
					function addPageViewsSelect(row) {
						html  = '<div>';
						html += '<select id="key-page-views-' + row + '" name="config_pixel[' + row + '][view]" onchange="setACInputClass(' + row + ');" class="form-control">';
						html += '<option value="product"><?php echo $text_product_page; ?></option>';
						html += '<option value="category"><?php echo $text_category_page; ?></option>';
						html += '<option value="manufacturer"><?php echo $text_manufacturer_page; ?></option>';
						html += '<option value="information"><?php echo $text_information_page; ?></option>';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
						
						replaceInput(row);
						
					}
					
					function replaceInput(row) {
						var view = $('#key-page-views-' + row).val();
						
						$('#pixel-path-' + row).prop('type', 'hidden');
						html = '<input type="text" id="autocomplete-input-' + row + '" class="autocomplete autocomplete-' + view + ' form-control" name="config_pixel[' +row + '][view-name]" size="40" onkeyup="autocompleteInput(' + row + ', \'' + view + '\');" />';
						$('#pixel-path-input-' + row).append(html);
						
					}
					
					function setACInputClass(row) {
						var view = $('#key-page-views-' + row).val();
						$('#autocomplete-input-' + row).replaceWith('<input type="text" id="autocomplete-input-' + row + '" class="autocomplete autocomplete-' + view + ' form-control" name="config_pixel[' +row + '][view-name]" size="40" onkeyup="autocompleteInput(' + row + ', \'' + view + '\');"/>');
					}
					
					function initRows(row) {
						var category = $('#category-' + row).val();
						
						if (category === 'checkouts') {
							initCheckoutPath(row);
						}
						
						if (category === 'registrations') {
							initRegPath(row);
						}
						
						if (category === 'key_page_views') {
							initPageViewsPath(row);
						}
						if (category === 'adds_to_cart') {
							setCartAddsPath(row);
						}
						
					}
					
					function initCheckoutPath(row) {
						addCheckoutSelect(row);
						if ($('#pixel-path-' + row).val() === 'checkout/success') {
							$('#checkout-' + row).val('default');
							$('#pixel-path-' + row).prop('readonly', true);
						}
						if ($('#pixel-path-' + row).val() !== 'checkout/success') {
							$('#checkout-' + row).val('custom');
							$('#pixel-path-' + row).prop('readonly', false);
						}
						
						$('#pixel-value-' + row).prop('readonly', true);
						
					}
					
					function initRegPath(row) {
						addRegSelect(row);
						$('#pixel-path-' + row).prop('readonly', true);
						if ($('#pixel-path-' + row).val() === 'account/success') {
							$('#registration-' + row).val('account');
						}
						if ($('#pixel-path-' + row).val() === 'affiliate/success') {
							$('#registration-' + row).val('affiliate');
						}
						
					}
					
					function initPageViewsPath(row) {
						replaceInput(row);
						$('#autocomplete-input-' + row).val($('#pixel-view-name-' + row).val());	
					}
					
					function resetInputs(row, category) {
						
						if (category != 'checkouts') {
							$('#checkout-' + row).remove();
						}
						
						if (category != 'registrations') {
							$('#registration-' + row).remove();
						}
						
						if (category != 'key_page_views') {
							$('#key-page-views-' + row).remove();
						}
						
						$('#pixel-value-' + row).prop('readonly', false).val('');
						$('#pixel-path-' + row).prop('readonly', false).removeClass().val('');
						$('#pixel-path-' + row).addClass('form-control');
						
						$('#pixel-path-' + row).prop('type', 'text');
						$('#autocomplete-input-' + row).remove();
					}
					
					function autocompleteInput(row, view) {
						
						$('.autocomplete-' + view).autocomplete({
							delay: 300,
							source: function(request, response) {
								$.ajax({
									url: 'index.php?route=catalog/product/autocompleteView&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request) + '&view=' + view,
									dataType: 'json',
									success: function(json) {	
										response($.map(json, function(item) {
											return {
												label: item.name,
												value: item.id
											}
										}));
									}
								});
							}, 
							select: function(item) {
								$('#autocomplete-input-' + row).val(item['label']);
								$('#pixel-path-' + row).val(getURLPrefix(view) + item['value']);
								return false;
							},
							minLength: 2
						});
					
					}
					
					function getURLPrefix(view) {
						
						if (view === 'product') {
							return 'product_id=';
						} else if (view === 'category') {
							return 'category_id=';
						} else if (view === 'manufacturer') {
							return 'manufacturer_id=';
						} else if (view === 'information') {
							return 'information_id=';
						}
					
					}		
					
					// Document Ready
					
					$(function () {
						var pixels = <?php echo $pixel_row; ?>;
						for (i = 0; i < pixels; i++) {
							initRows(i);
						}
					});		

					//--></script>
					]]></add>
			</operation>	
	</file>
	<file path="admin/language/*/setting/setting.php">
		<operation>
		<search><![CDATA[// Entry]]></search>
				<add position="after"><![CDATA[
				$_['tab_facebook_pixel']        = 'Facebook Pixel(s)';
$_['entry_facebook_pixel'] = 'Facebook Pixel ID';
$_['help_facebook_pixel']		= 'Login to your <a href="https://www.facebook.com/ads/manager/pixel/facebook_pixel/" target="_blank"><u>Facebook Ads Management</u></a> account and select the Pixel ID, in the upper right corner. Paste the Pixel ID into this field.';
$_['entry_fb_product_catalog_id'] = 'Facebook Product Catalog ID';
$_['help_catalog_id']			= 'The Product Catalog ID is required for Dynamic Product Ads';

$_['entry_pixel_ID']            = 'Facebook Conversion Pixel ID';
$_['button_add_pixel']		 	= 'Add Conversion Pixel';
$_['entry_pixel_category']		= 'Conversion Pixel Category';
$_['entry_pixel_path']			= 'Path';
$_['entry_pixel_value']			= 'Value';
$_['entry_cat_checkouts'] 		= 'Checkouts';
$_['entry_cat_registrations'] 	= 'Registrations';
$_['entry_cat_leads'] 			= 'Leads';
$_['entry_cat_keypageviews'] 	= 'Key Page Views';
$_['entry_cat_addstocart'] 		= 'Adds To Cart';
$_['entry_cat_other'] 			= 'Other';
$_['entry_status']        		= 'Status';

$_['text_order_total'] 			= 'Order Total';
$_['text_product_price'] 		= 'Prod. Price';
$_['text_new_customer'] 		= 'New Customer';
$_['text_new_affiliate'] 		= 'New Affiliate';
$_['text_product_page']			= 'Product Page';
$_['text_category_page'] 		= 'Category Page';
$_['text_manufacturer_page'] 	= 'Manufacturer Page';
$_['text_information_page']		= 'Information Page';
				]]></add>
		</operation>
	</file>
	<file path="admin/controller/setting/store.php">
		<operation>
			<search><![CDATA[$data['tab_server'] = $this->language->get('tab_server');]]></search>
			<add position="after"><![CDATA[
			$data['tab_facebook_pixel'] = $this->language->get('tab_facebook_pixel');
			$data['entry_facebook_pixel'] = $this->language->get('entry_facebook_pixel');
			$data['help_facebook_pixel'] = $this->language->get('help_facebook_pixel');
			$data['entry_fb_product_catalog_id'] = $this->language->get('entry_fb_product_catalog_id');
			$data['help_catalog_id'] = $this->language->get('help_catalog_id');
			
			$data['entry_pixel_ID'] = $this->language->get('entry_pixel_ID');
			$data['entry_pixel_category'] = $this->language->get('entry_pixel_category');
			$data['entry_pixel_path'] = $this->language->get('entry_pixel_path');
			$data['entry_pixel_value'] = $this->language->get('entry_pixel_value');
			$data['text_enabled'] = $this->language->get('text_enabled');
			$data['text_disabled'] = $this->language->get('text_disabled');
			
			$data['text_order_total'] = $this->language->get('text_order_total');
			$data['text_product_price'] = $this->language->get('text_product_price');
			$data['text_default_success'] = $this->language->get('text_default_success');
			$data['text_custom_success'] = $this->language->get('text_custom_success');
			$data['text_new_customer'] = $this->language->get('text_new_customer');
			$data['text_new_affiliate'] = $this->language->get('text_new_affiliate');
			$data['text_product_page'] = $this->language->get('text_product_page');
			$data['text_category_page'] = $this->language->get('text_category_page');
			$data['text_manufacturer_page'] = $this->language->get('text_manufacturer_page');
			$data['text_information_page'] = $this->language->get('text_information_page');
			
			$data['entry_status'] = $this->language->get('entry_status');
			$data['entry_sort_order'] = $this->language->get('entry_sort_order');
			
			$data['button_save'] = $this->language->get('button_save');
			$data['button_cancel'] = $this->language->get('button_cancel');
			$data['button_add_pixel'] = $this->language->get('button_add_pixel');
			$data['button_remove'] = $this->language->get('button_remove');
			
			$data['pixel_category'] = array (
					'checkouts' 	=> $this->language->get('entry_cat_checkouts'),
					'registrations' => $this->language->get('entry_cat_registrations'),
					'leads' 		=> $this->language->get('entry_cat_leads'),
					'key_page_views' => $this->language->get('entry_cat_keypageviews'),
					'adds_to_cart' 	=> $this->language->get('entry_cat_addstocart'),
					'other' 		=> $this->language->get('entry_cat_other')
			); 
			]]></add>
		</operation>
		<operation>
			<search><![CDATA[$data['token']]]></search>
			<add position="after"><![CDATA[
			
			if (isset($this->request->post['config_custom_audience_pixel'])) {
				$data['custom_audience_pixel'] = $this->request->post['config_custom_audience_pixel'];
			} elseif (isset($store_info['config_custom_audience_pixel'])) { 
				$data['custom_audience_pixel'] = $store_info['config_custom_audience_pixel'];
			} else {
				$data['custom_audience_pixel'] = '';
			}
			
			if (isset($this->request->post['config_fb_product_catalog_id'])) {
				$data['config_fb_product_catalog_id'] = $this->request->post['config_fb_product_catalog_id'];
			} elseif (isset($store_info['config_fb_product_catalog_id'])) { 
				$data['config_fb_product_catalog_id'] = $store_info['config_fb_product_catalog_id'];
			} else {
				$data['config_fb_product_catalog_id'] = '';
			}
			
			if (isset($this->request->post['config_pixel'])) {
				$pixels_data = $this->request->post['config_pixel'];
			} elseif (isset($store_info['config_pixel'])) {
				$pixels_data = $store_info['config_pixel'];
			} else {
				$pixels_data = array();
			}
			
			if (!empty($pixels_data)) {
				foreach ($pixels_data as $key => $pixel) {
					if($pixel['category'] == 'key_page_views') {
						$data['pixels'][] = array(
							'key'       => $key,
							'pixel_id' 	=> $pixel['pixel_id'],
							'category'  => $pixel['category'],
							'view'		=> $pixel['view'],
							'view-name'	=> $pixel['view-name'],
							'path'    	=> $pixel['path'],
							'value' 	=> $pixel['value'],
							'status' 	=> $pixel['status']
						);
					} else {
						$data['pixels'][] = array(
							'key'       => $key,
							'pixel_id' 	=> $pixel['pixel_id'],
							'category'  => $pixel['category'],
							'path'    	=> $pixel['path'],
							'value' 	=> $pixel['value'],
							'status' 	=> $pixel['status']
						);
					}
				}
			} else {
				$data['pixels'] = array();
			}
			]]></add>
		</operation>	
	</file>
	<file path="admin/view/template/setting/store_form.tpl">
			<operation>
				<search><![CDATA[<a href="#tab-server" data-toggle="tab">]]></search>
				<add position="after"><![CDATA[
				<li><a href="#tab-facebook-pixel" data-toggle="tab"><?php echo $tab_facebook_pixel; ?></a></li>
				]]></add>
			</operation>
			<operation>
				<search><![CDATA[</form>]]></search>
				<add position="before" offset="1"><![CDATA[
				<div class="tab-pane" id="tab-facebook-pixel">
					<p>To create your Facebook pixel:
						<ol>
						<li>Go to your Facebook Pixel tab in Ads Manager.</li>
						<li>Click <b>Create a Pixel</b>.</li>
						<li>Enter a name for your pixel. There's only one pixel per ad account, so choose a name that represents your business.</li>
						<li>Make sure you've checked the box to accept the terms.</li>
						<li>Click <b>Create Pixel</b>.</li>
						<li>Copy the Pixel ID and paste it here.</li>
						</ol>
						<a href="https://www.facebook.com/business/help/952192354843755" target="_blank">More</a>
					</p>
					<hr />
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-facebook-pixel"><span data-toggle="tooltip" data-html="true" data-trigger="click" title="<?php echo htmlspecialchars($help_facebook_pixel); ?>"><?php echo $entry_facebook_pixel; ?></span></label>
						<div class="col-sm-10">
						  <input name="config_custom_audience_pixel" type="text" placeholder="<?php echo $entry_facebook_pixel; ?>" id="input-facebook-pixel" class="form-control" value="<?php echo $custom_audience_pixel; ?>">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-fb-product-catalog-id"><span data-toggle="tooltip" data-html="true" data-trigger="click" title="<?php echo htmlspecialchars($help_catalog_id); ?>"><?php echo $entry_fb_product_catalog_id; ?></span></label>
						<div class="col-sm-10">
						  <input name="config_fb_product_catalog_id" type="text" placeholder="<?php echo $entry_fb_product_catalog_id; ?>" id="input-fb-product-catalog-id" class="form-control" value="<?php echo $config_fb_product_catalog_id; ?>">
						</div>
					</div>
					
					<hr />
					<p>v.1.2.1.5 - <a href="http://jorimvanhove.com" target="_blank">Jorim van Hove</a> © 2015 - <a href="http://jorimvanhove.com/plugins/fbcp" target="_blank">Online documentation</a> - <a href="https://www.facebook.com/business/help/search/?query=facebook+pixel&ss=1450967422" target="_blank">Facebook Help Center</a></p>
				</div>
				]]></add>
			</operation>
			<operation>
				<search><![CDATA[<?php echo $footer; ?>]]></search>
				<add position="before"><![CDATA[
				<script type="text/javascript"><!--
					var pixel_row = <?php echo $pixel_row; ?>;

					// Functions
					
					function addPixel() {	
						html  = '<tbody id="pixel-row' + pixel_row + '">';
						html += '  <tr>';
						html += '    <td class="left"><input type="text" class="form-control" name="config_pixel[' + pixel_row + '][pixel_id]" value="" maxlength="13"/></td>'; 
						html += '	 <td class="left"><select id="category-' + pixel_row + '" name="config_pixel[' + pixel_row + '][category]" class="form-control" onchange="setPath(' + pixel_row + ');">';
						html += '	 	<option value="<?php echo 'checkouts'; ?>" ><?php echo $pixel_category['checkouts']; ?></option>';
						html += '		<option value="<?php echo 'registrations'; ?>" ><?php echo $pixel_category['registrations']; ?></option>';
						html += '		<option value="<?php echo 'leads'; ?>" ><?php echo $pixel_category['leads']; ?></option>';
						html += '		<option value="<?php echo 'key_page_views'; ?>" ><?php echo $pixel_category['key_page_views']; ?></option>';
						html += '		<option value="<?php echo 'adds_to_cart'; ?>"><?php echo $pixel_category['adds_to_cart']; ?></option>';
						html += '		<option value="<?php echo 'other'; ?>" ><?php echo $pixel_category['other']; ?></option>';
						html += '    </select></td>';
						html += '	 <td class="left" id="pixel-path-input-' + pixel_row + '"><input type="text" id="pixel-path-' + pixel_row + '" class="form-control"  name="config_pixel[' + pixel_row + '][path]" value="" size="40" /></td>';
						html += '	 <td class="left"><input type="text" id="pixel-value-' + pixel_row + '" class="form-control"  name="config_pixel[' + pixel_row + '][value]" value="" size="10" /></td>';
						html += '    <td class="left"><select name="config_pixel[' + pixel_row + '][status]" class="form-control" >';
						html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
						html += '      <option value="0"><?php echo $text_disabled; ?></option>';
						html += '    </select></td>';
						html += '    <td class="left"><a onclick="$(\'#pixel-row' + pixel_row + '\').remove();" class="btn btn-danger"><i class="fa fa-minus-circle"></i></a></td>';
						html += '  </tr>';
						html += '</tbody>';
	
						$('#fcpixel tfoot').before(html);
						
						$(function () {
							setPath(pixel_row);
						});
						
						pixel_row++;
					}
					
					function setPath(row) {
						
						var category = $('#category-' + row).val();
						
						resetInputs(row, category);
						
						if (category === 'checkouts') {
							addCheckoutSelect(row);
							setCheckoutPath(row);
							$('#checkout-' + row).change();
						}
						
						if (category === 'registrations') {
							addRegSelect(row);
							setRegistrationPath(row);
							$('#registration-' + row).change();
						}					
						
						if (category === 'leads') {
							setLeadsPath(row);
						}
						
						if (category === 'key_page_views') {
							addPageViewsSelect(row);
							setPageViewsPath(row);
							$('#key-page-views-' + row).change();
							
						}
						
						if (category === 'adds_to_cart') {
						 	setCartAddsPath(row);
						}
					}
					
					function addCheckoutSelect(row) {
						html  = '<div>';
						html += '<select id="checkout-' + row + '" onchange="setCheckoutPath(' + row + ');" class="form-control">';
						html += '<option value="default"><?php echo $text_default_success; ?></option> ';
						html += '<option value="custom"><?php echo $text_custom_success; ?></option> ';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
					}
					
					function setCheckoutPath(row) {
						var checkoutselect = $('#checkout-' + row).val();
						
						if (checkoutselect === 'default') {
							$('#pixel-path-' + row).val('checkout/success');
							$('#pixel-path-' + row).prop('readonly', true);
						} else {
							$('#pixel-path-' + row).val('');
							$('#pixel-path-' + row).prop('readonly', false);
						}
						
						$('#pixel-value-' + row).val('<?php echo $text_order_total; ?>');
						$('#pixel-value-' + row).prop('readonly', true);
					}
					
					function setRegistrationPath(row) {
						var registrationselect = $('#registration-' + row).val();
						
						$('#pixel-value-' + row).prop('readonly', false).val('');
							if (registrationselect === 'account') {
								$('#pixel-path-' + row).val('account/success');
								$('#pixel-path-' + row).prop('readonly', true).addClass('form-control');
								
							} else if (registrationselect == 'affiliate') {
								$('#pixel-path-' + row).val('affiliate/success');
								$('#pixel-path-' + row).prop('readonly', true).addClass('form-control');
							}		
					}
					
					function addRegSelect(row) {
						html  = '<div>';
						html += '<select id="registration-' + row + '" onchange="setRegistrationPath(' + row + ');" class="form-control">';
						html += '<option value="account"><?php echo $text_new_customer; ?></option> ';
						html += '<option value="affiliate"><?php echo $text_new_affiliate; ?></option> ';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
					}
					
					function setLeadsPath(row) {
					
					}
					
					function setPageViewsPath(row) {
						
						
					}
					
					function setCartAddsPath(row) {
						$('#pixel-path-' + row).val('');
						$('#pixel-path-' + row).prop('readonly', true);
						$('#pixel-value-' + row).val('<?php echo $text_product_price; ?>');
						$('#pixel-value-' + row).prop('readonly', true);
					}
					
					function addPageViewsSelect(row) {
						html  = '<div>';
						html += '<select id="key-page-views-' + row + '" name="config_pixel[' + row + '][view]" onchange="setACInputClass(' + row + ');" class="form-control">';
						html += '<option value="product"><?php echo $text_product_page; ?></option>';
						html += '<option value="category"><?php echo $text_category_page; ?></option>';
						html += '<option value="manufacturer"><?php echo $text_manufacturer_page; ?></option>';
						html += '<option value="information"><?php echo $text_information_page; ?></option>';
						html += '</select></div>';
						
						$('#category-' + row).after(html);
						
						replaceInput(row);
						
					}
					
					function replaceInput(row) {
						var view = $('#key-page-views-' + row).val();
						
						$('#pixel-path-' + row).prop('type', 'hidden');
						html = '<input type="text" id="autocomplete-input-' + row + '" class="autocomplete autocomplete-' + view + ' form-control" name="config_pixel[' +row + '][view-name]" size="40" onkeyup="autocompleteInput(' + row + ', \'' + view + '\');" />';
						$('#pixel-path-input-' + row).append(html);
						
					}
					
					function setACInputClass(row) {
						var view = $('#key-page-views-' + row).val();
						$('#autocomplete-input-' + row).replaceWith('<input type="text" id="autocomplete-input-' + row + '" class="autocomplete autocomplete-' + view + ' form-control" name="config_pixel[' +row + '][view-name]" size="40" onkeyup="autocompleteInput(' + row + ', \'' + view + '\');"/>');
					}
					
					function initRows(row) {
						var category = $('#category-' + row).val();
						
						if (category === 'checkouts') {
							initCheckoutPath(row);
						}
						
						if (category === 'registrations') {
							initRegPath(row);
						}
						
						if (category === 'key_page_views') {
							initPageViewsPath(row);
						}
						if (category === 'adds_to_cart') {
							setCartAddsPath(row);
						}
						
					}
					
					function initCheckoutPath(row) {
						addCheckoutSelect(row);
						if ($('#pixel-path-' + row).val() === 'checkout/success') {
							$('#checkout-' + row).val('default');
							$('#pixel-path-' + row).prop('readonly', true);
						}
						if ($('#pixel-path-' + row).val() !== 'checkout/success') {
							$('#checkout-' + row).val('custom');
							$('#pixel-path-' + row).prop('readonly', false);
						}
						
						$('#pixel-value-' + row).prop('readonly', true);
						
					} 
					
					function initRegPath(row) {
						addRegSelect(row);
						$('#pixel-path-' + row).prop('readonly', true);
						if ($('#pixel-path-' + row).val() === 'account/success') {
							$('#registration-' + row).val('account');
						}
						if ($('#pixel-path-' + row).val() === 'affiliate/success') {
							$('#registration-' + row).val('affiliate');
						}
						
					}
					
					function initPageViewsPath(row) {
						replaceInput(row);
						$('#autocomplete-input-' + row).val($('#pixel-view-name-' + row).val());	
					}
					
					function resetInputs(row, category) {
						
						if (category != 'checkouts') {
							$('#checkout-' + row).remove();
						}
						
						if (category != 'registrations') {
							$('#registration-' + row).remove();
						}
						
						if (category != 'key_page_views') {
							$('#key-page-views-' + row).remove();
						}
						
						$('#pixel-value-' + row).prop('readonly', false).val('');
						$('#pixel-path-' + row).prop('readonly', false).removeClass().val('');
						$('#pixel-path-' + row).addClass('form-control');
						
						$('#pixel-path-' + row).prop('type', 'text');
						$('#autocomplete-input-' + row).remove();
					}
					
					function autocompleteInput(row, view) {
						
						$('.autocomplete-' + view).autocomplete({
							delay: 300,
							source: function(request, response) {
								$.ajax({
									url: 'index.php?route=catalog/product/autocompleteView&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request) + '&view=' + view,
									dataType: 'json',
									success: function(json) {	
										response($.map(json, function(item) {
											return {
												label: item.name,
												value: item.id
											}
										}));
									}
								});
							}, 
							select: function(item) {
								$('#autocomplete-input-' + row).val(item['label']);
								$('#pixel-path-' + row).val(getURLPrefix(view) + item['value']);
								return false;
							},
							minLength: 2
						});
					
					}
					
					function getURLPrefix(view) {
						
						if (view === 'product') {
							return 'product_id=';
						} else if (view === 'category') {
							return 'category_id=';
						} else if (view === 'manufacturer') {
							return 'manufacturer_id=';
						} else if (view === 'information') {
							return 'information_id=';
						}
					
					}		
					
					// Document Ready
					
					$(function () {
						var pixels = <?php echo $pixel_row; ?>;
						for (i = 0; i < pixels; i++) {
							initRows(i);
						}
					});
					
					//--></script>
					]]></add>
			</operation>	
	</file>
	<file path="admin/language/*/setting/store.php">
		<operation>
		<search><![CDATA[// Entry]]></search>
		<add position="after"><![CDATA[
$_['tab_facebook_pixel']        = 'Facebook Pixel(s)';
$_['entry_facebook_pixel'] = 'Facebook Pixel ID';
$_['help_facebook_pixel']		= 'Login to your <a href="https://www.facebook.com/ads/manager/pixel/facebook_pixel/" target="_blank"><u>Facebook Ads Management</u></a> account and select the Pixel ID, in the upper right corner. Paste the Pixel ID into this field.';
$_['entry_fb_product_catalog_id'] = 'Facebook Product Catalog ID';
$_['help_catalog_id']			= 'The Product Catalog ID is required for Dynamic Product Ads';

$_['entry_pixel_ID']            = 'Facebook Conversion Pixel ID';
$_['button_add_pixel']		 	= 'Add Conversion Pixel';
$_['entry_pixel_category']		= 'Conversion Pixel Category';
$_['entry_pixel_path']			= 'Path';
$_['entry_pixel_value']			= 'Value';
$_['entry_cat_checkouts'] 		= 'Checkouts';
$_['entry_cat_registrations'] 	= 'Registrations';
$_['entry_cat_leads'] 			= 'Leads';
$_['entry_cat_keypageviews'] 	= 'Key Page Views';
$_['entry_cat_addstocart'] 		= 'Adds To Cart';
$_['entry_cat_other'] 			= 'Other';
$_['entry_status']        		= 'Status';

$_['text_order_total'] 			= 'Order Total';
$_['text_product_price'] 		= 'Prod. Price';
$_['text_new_customer'] 		= 'New Customer';
$_['text_new_affiliate'] 		= 'New Affiliate';
$_['text_product_page']			= 'Product Page';
$_['text_category_page'] 		= 'Category Page';
$_['text_manufacturer_page'] 	= 'Manufacturer Page';
$_['text_information_page']		= 'Information Page';
				]]></add>
		</operation>
	</file>	
	<file path="admin/controller/catalog/product.php">
		<operation>
		<search><![CDATA[public function autocomplete() {]]></search>
		<add position="before"><![CDATA[
		public function autocompleteView() {
			
			$view = $this->request->get['view'];
			
			switch ($view) {
				case 'product': 
					$json = $this->getAutocompleteData($view, 0);
					break;
				case 'category':
					$json = $this->getAutocompleteData($view, 1);
					break;
				case 'manufacturer':
					$json =  $this->getAutocompleteData($view, 2);
					break;	
				case 'information':
					$json =  $this->getAutocompleteData($view, 3);
					break;
			}

		 $this->response->setOutput(json_encode($json));
		 
		}
		
		protected function getAutocompleteData($view, $switch) {
			
			$json = array();
			
			if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_model'])) {
				$this->load->model('catalog/' . $view);

				if (isset($this->request->get['filter_name'])) {
					$filter_name = $this->request->get['filter_name'];
				} else {
					$filter_name = '';
				}

				if (isset($this->request->get['filter_model'])) {
					$filter_model = $this->request->get['filter_model'];
				} else {
					$filter_model = '';
				}

				if (isset($this->request->get['limit'])) {
					$limit = $this->request->get['limit'];	
				} else {
					$limit = 20;	
				}			

				$data = array(
					'filter_name'  => $filter_name,
					'filter_model' => $filter_model,
					'start'        => 0,
					'limit'        => $limit
				);
				
				switch ($switch) {
					case 0: 
						$results = $this->model_catalog_product->getProducts($data);
						break;
					case 1: 
						$results = $this->model_catalog_category->getCategories($data);
						break;
					case 2: 
						$results = $this->model_catalog_manufacturer->getManufacturers($data);
						break;
					case 3: 
						$results = $this->model_catalog_information->getInformations($data);
						break;
				}
				
				
		
				foreach ($results as $result) {
				
					$json[] = array(
						'id' 	=> $result[$view . '_id'],
						'name'  => strip_tags(html_entity_decode(($switch == 3 ? $result['title'] : $result['name']), ENT_QUOTES, 'UTF-8'))
					);	
				}
			}

		return $json;
				
		}
		
		]]></add>
	</operation>
	</file>	
</modification>