| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action('wp_footer', 'voelgoed_meta_pixel_initiate_checkout', 20);
|
| function voelgoed_meta_pixel_initiate_checkout() {
|
|
|
| if (is_admin() || wp_doing_ajax()) return;
|
|
|
| if (!function_exists('is_checkout') || !is_checkout()) return;
|
| if (function_exists('is_order_received_page') && is_order_received_page()) return;
|
|
|
| if (!function_exists('WC') || !WC()->cart || WC()->cart->is_empty()) return;
|
|
|
| $cart = WC()->cart;
|
|
|
| $content_ids = [];
|
| $contents = [];
|
|
|
| foreach ($cart->get_cart() as $cart_item) {
|
| $product_id = !empty($cart_item['product_id']) ? (int) $cart_item['product_id'] : 0;
|
| $variation_id = !empty($cart_item['variation_id']) ? (int) $cart_item['variation_id'] : 0;
|
| $id = $variation_id ? $variation_id : $product_id;
|
|
|
| $qty = !empty($cart_item['quantity']) ? (int) $cart_item['quantity'] : 0;
|
|
|
| $product = !empty($cart_item['data']) ? $cart_item['data'] : null;
|
| $unit_price = 0.0;
|
| if ($product && is_object($product) && method_exists($product, 'get_price')) {
|
| $unit_price = (float) $product->get_price();
|
| }
|
|
|
| if ($id > 0 && $qty > 0) {
|
| $content_ids[] = (string) $id;
|
| $contents[] = [
|
| 'id' => (string) $id,
|
| 'quantity' => $qty,
|
| 'item_price' => round((float) $unit_price, 2),
|
| ];
|
| }
|
| }
|
|
|
| $content_ids = array_values(array_unique($content_ids));
|
|
|
| $value = (float) $cart->get_total('edit');
|
| $currency = function_exists('get_woocommerce_currency') ? get_woocommerce_currency() : 'ZAR';
|
| $num_items = (int) $cart->get_cart_contents_count();
|
| ?>
|
| <script>
|
| (function () {
|
| if (typeof fbq !== 'function') return;
|
|
|
|
|
| var key = 'vg_fb_initiatecheckout_fired';
|
| try {
|
| if (sessionStorage.getItem(key) === '1') return;
|
| sessionStorage.setItem(key, '1');
|
| } catch (e) {}
|
|
|
| fbq('track', 'InitiateCheckout', {
|
| content_ids: <?php echo wp_json_encode($content_ids); ?>,
|
| content_type: 'product',
|
| contents: <?php echo wp_json_encode($contents); ?>,
|
| value: <?php echo wp_json_encode($value); ?>,
|
| currency: <?php echo wp_json_encode($currency); ?>,
|
| num_items: <?php echo (int) $num_items; ?>
|
| });
|
| })();
|
| </script>
|
| <?php
|
| }
|
|
|
| |
| |
Comments