{* # Description This is a Facebook Marketing widget for all pages. [Facebook Marketing](https://developers.facebook.com/docs/marketing-api/dynamic-product-ads/product-audiences/). The file needs to be included in index.tpl before tag. With include tag ex. {include file='modules/widgets/facebook/facebook.tpl' key='[YOUR KEY]'} ## Date last modified 2019-02-28 ## Resource + [Facebook Marketing](https://developers.facebook.com/docs/marketing-api/dynamic-product-ads/product-audiences/) ## Primary variables + $key - Facebook customer key + $page - Global scope variable containing all information about the page type + $text - Global scope variable containing translations ## Partials (templates) No extra templates required for this template *} {if isset($key)} {*** Helper function to generate 'content_category' in product page section ***} {function breadcrumbCategory id=0}{strip} {entity assign=category controller=productCategory id=$id} {if $id != 0 && $category} {if $id != $product->CategoryId}|{/if} {$category->Title|unescape:"htmlall"} {breadcrumbCategory id=$category->ParentId} {/if} {/strip}{/function} {*** Facebook Marketing pixel code ***} {literal} {/literal} {*** Facebook Marketing for product ***} {if $page.isProduct && isset($page.paths.2)} {controller assign=productCtrl type=product primary=true} {entity assign=product controller=$productCtrl} {if $product} {$responseData = [ 'currency' => $general.currencyIso ]} {$productTitle = $product->Title} {*** content_category data ***} {entity assign=category controller=productCategory id=$product->Id} {capture "foo"}{breadcrumbCategory id=$product->CategoryId}{/capture} {$categoryList = explode("|",$smarty.capture.foo)} {$categoryList = array_reverse($categoryList)} {$content_category = implode(" > ", $categoryList)} {*** value data ***} {if isset($smarty.get.variantId)} {$responseData['content_type'] = 'product'} {entity assign=variant controller=productVariant productId=$product->Id variantId=(int)$smarty.get.variantId} {*** contet_name data ***} {collection assign=variantTypes controller=productVariantType productId=$product->Id} {$variantTitle = $productTitle} {foreach $variantTypes->getData() as $variantType} {entity assign=variantData controller=productVariantData productId=$product->Id variantId=(int)$smarty.get.variantId typeId=$variantType->Id} {$variantTitle = "$variantTitle `$variantData->Title`"} {/foreach} {$responseData['content_name'] = $variantTitle} {*** content_category data ***} {$responseData['content_category'] = $content_category} {$responseData['id'] = "{$product->Id}-{$variant->Id}"} {$responseData['content_ids'] = [$responseData['id']]} {if (((!$settings.shop_b2b_hidden_prices or $user) or $settings.shop_b2b_hidden_prices === "partly") and $product->Type !== 'discontinued' ) and !$product->CallForPrice} {collection assign=prices controller=productPrice productId=$product->Id variantId=(int)$smarty.get.variantId} {$priceData = $prices->getData()} {$priceInterval = $priceData[0]} {$responseData['value'] = $priceInterval->PriceMin} {$responseData['item_price'] = $priceInterval->PriceMin} {else} {$responseData['value'] = 0} {$responseData['item_price'] = 0} {/if} {$responseData['quantity'] = $variant->Stock} {elseif $product->Type == 'variant'} {$content_ids = []} {$contents = []} {$responseData['id'] = "{$product->Id}"} {$responseData['item_group_id'] = "{$product->Id}"} {$responseData['content_type'] = 'product'} {*** Add variant data ***} {collection assign=variants controller=productVariant productId=$product->Id} {foreach $variants->getData() as $variant} {$contentItem = [ 'id' => "{$product->Id}-{$variant->Id}", 'item_price' => 0, 'value' => 0, 'quantity' => $variant->Stock ]} {*** content price for variant ***} {if (((!$settings.shop_b2b_hidden_prices or $user) or $settings.shop_b2b_hidden_prices === "partly") and $product->Type !== 'discontinued' ) and !$product->CallForPrice} {collection assign=prices controller=productPrice productId=$product->Id variantId=$variant->Id} {$priceData = $prices->getData()} {$priceInterval = $priceData[0]} {$contentItem['item_price'] = $priceInterval->PriceMin} {$contentItem['value'] = $priceInterval->PriceMin} {/if} {*** content_category data ***} {$contentItem['content_category'] = $content_category} {** content_name data ***} {collection assign=variantTypes controller=productVariantType productId=$product->Id} {$variantTitle = $productTitle} {foreach $variantTypes->getData() as $variantType} {entity assign=variantData controller=productVariantData productId=$product->Id variantId=$variant->Id typeId=$variantType->Id} {$variantTitle = "$variantTitle `$variantData->Title`"} {/foreach} {$contentItem['content_name'] = $variantTitle} {$content_ids[] = $contentItem['id']} {$contents[] = $contentItem} {/foreach} {$responseData['content_ids'] = $content_ids} {$responseData['contents'] = $contents} {/if} {$responseData = [ 'id' => $product->Id, 'currency' => $general.currencyIso, 'content_type' => 'product', 'content_category' => $content_category, 'content_name' => $productTitle, 'content_ids' => [$product->Id], 'value' => 0, 'item_price' => 0 ]} {if $product->Type == 'normal'} {$responseData['quantity'] = $product->Stock} {/if} {if (((!$settings.shop_b2b_hidden_prices or $user) or $settings.shop_b2b_hidden_prices === "partly") and $product->Type !== 'discontinued' ) and !$product->CallForPrice} {collection assign=prices controller=productPrice productId=$product->Id} {$priceData = $prices->getData()} {$priceInterval = $priceData[0]} {$responseData['value'] = $priceInterval->PriceMin} {$responseData['item_price'] = $priceInterval->PriceMin} {/if} {/if} {/if} {*** Facebook Marketing for purchase complete ***} {if $page.isCheckoutComplete && !empty($page.orderId)} {collection controller=orderLine assign=orderLine orderId=$page.orderId repay=false} {$orderList = $orderLine->groupByClass()} {if $orderLine->getActualSize() gt 0} {if isset($orderList['CollectionOrderLine'])} {$content_ids = []} {$contents = []} {$responseData = [ 'content_type' => 'product', 'currency' => $general.currencyIso, 'value' => $orderList.CollectionOrderTotalLine.0->Price ]} {foreach $orderList.CollectionOrderLine as $line} {$contentItem = []} {if isset($line->ProductId)} {$contentItem['id'] = (string)$line->ProductId} {$contentItem['item_price'] = $line->Price} {$contentItem['value'] = $line->Price} {$contentItem['quantity'] = $line->Amount} {$contentItem['content_name'] = $line->ProductTitle} {if $line->VariantId} {$contentItem['id'] = "{$contentItem['id']}-{$line->VariantId}"} {$contentItem['content_name'] = "{$contentItem['content_name']} {$line->VariantTitle}"} {/if} {*** content_category data ***} {entity assign=product controller=product id=$line->ProductId} {entity assign=category controller=productCategory id=$line->ProductId} {capture "foo"}{breadcrumbCategory id=$product->CategoryId}{/capture} {$categoryList = explode("|",$smarty.capture.foo)} {$categoryList = array_reverse($categoryList)} {$contentItem['content_category'] = implode(" > ", $categoryList)} {$content_ids[] = $contentItem['id']} {$contents[] = $contentItem} {/if} {/foreach} {$responseData['contents'] = $contents} {$responseData['content_ids'] = $content_ids} {/if} {/if} {/if} {*** Facebook Marketing for add to cart ***} {if $message && in_array($message->type(), ["cart_update_success", "cart_plus_success", "cart_reorder_one_success", "cart_reorder_all_success", "cart_add_variant_success", "cart_add_success"])} {*** Get cart total ***} {collection controller=cart assign=cartData} {$cartList = $cartData->groupByClass()} {if isset($cartList.CartProductLine)} {$content_ids = []} {$contents = []} {$messageItem = $message->itemId()} {$responseData = [ 'content_type' => 'product', 'currency' => $general.currencyIso ]} {if count($messageItem) == 1} {$messageItem = array_pop($messageItem)} {/if} {if isset($messageItem.product)} {foreach $cartList.CartProductLine as $lineItem} {if ($lineItem->ProductId == $messageItem.product && !isset($messageItem.variant)) OR ($lineItem->ProductId == $messageItem.product && isset($messageItem.variant) && $lineItem->VariantId == $messageItem.variant)} {$responseData['content_name'] = $lineItem->Title} {$itemId = (string)$messageItem.product} {if $messageItem.variant} {$itemId = "{$itemId}-{$messageItem.variant}"} {$responseData['content_name'] = "{$responseData['content_name']} {$lineItem->VariantTitle}"} {/if} {*** content_category data ***} {entity assign=product controller=product id=$messageItem.product} {entity assign=category controller=productCategory id=$messageItem.product} {capture "foo"}{breadcrumbCategory id=$product->CategoryId}{/capture} {$categoryList = explode("|",$smarty.capture.foo)} {$categoryList = array_reverse($categoryList)} {$responseData['content_category'] = implode(" > ", $categoryList)} {$responseData['value'] = $lineItem->Price} {$content_ids[] = $itemId} {break} {/if} {/foreach} {else} {foreach $messageItem as $item} {$contentItem = []} {foreach $cartList.CartProductLine as $lineItem} {if ($lineItem->ProductId == $item.product && !isset($item.variant)) OR ($lineItem->ProductId == $item.product && isset($item.variant) && $lineItem->VariantId == $item.variant)} {$contentItem['id'] = (string)$lineItem->ProductId} {$contentItem['item_price'] = $lineItem->Price} {$contentItem['quantity'] = (int)$item.amount} {if $lineItem->VariantId} {$contentItem['id'] = $contentItem['id']|cat:"-{$lineItem->VariantId}"} {$contentItem['content_name'] = "{$contentItem['content_name']} {$lineItem->VariantTitle}"} {/if} {*** content_category data ***} {entity assign=product controller=product id=$item.product} {entity assign=category controller=productCategory id=$item.product} {capture "foo"}{breadcrumbCategory id=$product->CategoryId}{/capture} {$categoryList = explode("|",$smarty.capture.foo)} {$categoryList = array_reverse($categoryList)} {$contentItem['content_category'] = implode(" > ", $categoryList)} {$content_ids[] = $contentItem['id']} {break} {/if} {/foreach} {if !empty($contentItem)}{$contents[] = $contentItem}{/if} {/foreach} {$responseData['contents'] = $contents} {/if} {$responseData['content_ids'] = $content_ids} {/if} {/if} {/if}