| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action('woocommerce_thankyou', 'voelgoed_meta_pixel_purchase', 10, 1);
|
| function voelgoed_meta_pixel_purchase($order_id) {
|
|
|
| if (empty($order_id) || is_admin() || wp_doing_ajax()) {
|
| return;
|
| }
|
|
|
| if ( ! function_exists('wc_get_order') ) {
|
| return;
|
| }
|
|
|
| $order = wc_get_order($order_id);
|
| if (!$order) {
|
| return;
|
| }
|
|
|
|
|
| $status = $order->get_status();
|
| if ( ! in_array($status, ['processing', 'completed'], true) ) {
|
| return;
|
| }
|
|
|
|
|
| if (get_post_meta($order_id, '_vg_meta_pixel_purchase_tracked', true)) {
|
| return;
|
| }
|
|
|
| $content_ids = [];
|
| $contents = [];
|
|
|
| foreach ($order->get_items() as $item) {
|
| if (!is_a($item, 'WC_Order_Item_Product')) {
|
| continue;
|
| }
|
|
|
| $product_id = (int) $item->get_product_id();
|
| $variation_id = (int) $item->get_variation_id();
|
| $id = $variation_id ? $variation_id : $product_id;
|
|
|
| $qty = (int) $item->get_quantity();
|
| if ($id <= 0 || $qty <= 0) {
|
| continue;
|
| }
|
|
|
|
|
| $line_total = (float) $item->get_total();
|
| $unit_price = $qty > 0 ? ($line_total / $qty) : 0.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) $order->get_total();
|
| $currency = $order->get_currency() ? $order->get_currency() : 'ZAR';
|
|
|
|
|
| $event_id = 'order_' . (string) $order_id;
|
|
|
| ?>
|
| <script>
|
| (function () {
|
| if (typeof fbq !== 'function') return;
|
|
|
| fbq('track', 'Purchase', {
|
| 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); ?>
|
| }, {
|
| eventID: <?php echo wp_json_encode($event_id); ?>
|
| });
|
| })();
|
| </script>
|
| <?php
|
|
|
|
|
| update_post_meta($order_id, '_vg_meta_pixel_purchase_tracked', 1);
|
| }
|
|
|
| |
| |
Comments