public function getEventParameters()

in 2.0.x-2.2.x/upload/catalog/model/module/facebook_business.php [329:882]


    public function getEventParameters() {
        $route = (array_key_exists('route', $this->request->get)) ? $this->request->get['route'] : null;
    
        $facebook_pixel_event_params = null;
    
        // This grabs events stored on redirects
        if (array_key_exists('facebook_pixel_event_params', $this->session->data)) {
            $facebook_pixel_event_params = $this->session->data['facebook_pixel_event_params'];
        }

        $event_name = 'ViewContent';
        $event_id = $this->generateEventId();
    
        // Checking route and handling the events fired accordingly
        switch ($route) {
            case 'checkout/success':
                $event_name = 'Purchase';

                $this->load->language('checkout/success');

                $contents = array();
                $content_ids = array();
                $value = 0;
                $num_items = 0;
                $currency = $this->session->data['currency'];

                if (isset($this->session->data['facebook_business_order_id'])) {
                    $order_id = $this->session->data['facebook_business_order_id'];
                    unset($this->session->data['facebook_business_order_id']);

                    $this->load->model('checkout/order');
                    $this->load->model('account/order');

                    $order_info = $this->model_checkout_order->getOrder($order_id);

                    if ($order_info) {
                        $order_products = $this->model_account_order->getOrderProducts($order_id);

                        foreach ($order_products as $product) {
                            $content_ids[] = (string)$product['product_id'];
                            $num_items += $product['quantity'];
                            $contents[] = array(
                                'id'       => $product['product_id'],
                                'quantity' => $product['quantity']
                            );
                        }

                        $value = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
                        $currency = $order_info['currency_code'];
                    }
                } 

                $facebook_pixel_event_params = array(
                    'event_name'    => $event_name,
                    'content_ids'   => $content_ids,
                    'content_name'  => $this->formatString($this->language->get('heading_title')),
                    'content_type'  => 'product',
                    'contents'      => $contents,
                    'currency'      => strtoupper($currency),
                    'num_items'     => $num_items,
                    'value'         => $value,
                    'event_id'      => $event_id
                );

                break;

            case 'product/product':
                $event_name = 'ViewContent';

                $this->load->model('catalog/product');

                $product_id = isset($this->request->get['product_id']) ? (int)$this->request->get['product_id'] : 0;

                $product_info = $this->model_catalog_product->getProduct($product_id);

                if ($product_info) {
                    if ((float)$product_info['special']) {
                        $price = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    } else {
                        $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    }

                    $facebook_pixel_event_params = array(
                        'event_name'       => $event_name,
                        'content_ids'      => array((string)$product_id),
                        'content_name'     => $this->formatString($product_info['name']),
                        'content_type'     => 'product',
                        'currency'         => strtoupper($this->session->data['currency']),
                        'value'            => $price,
                        'event_id'         => $event_id
                    );
                }

                break;

            case 'checkout/cart':
                $event_name = 'AddToCart';

                $this->load->language('checkout/cart');

                $contents = array();
                $content_ids = array();
                $value = 0.0;
                $num_items = 0;

                foreach ($this->cart->getProducts() as $product) {
                    $content_ids[] = (string)$product['product_id'];
                    $value += $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    $num_items += $product['quantity'];
                    $contents[] = array(
                        'id'       => $product['product_id'],
                        'quantity' => $product['quantity']
                    );
                }

                $facebook_pixel_event_params = array(
                    'event_name'    => $event_name,
                    'content_ids'   => $content_ids,
                    'content_name'  => $this->formatString($this->language->get('heading_title')),
                    'content_type'  => 'product',
                    'contents'      => $contents,
                    'currency'      => strtoupper($this->session->data['currency']),
                    'value'         => $value,
                    'event_id'      => $event_id
                );

                break;

            case 'account/order/reorder':
                $event_name = 'AddToCart';

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

                $this->load->model('account/order');

                $order_info = $this->model_account_order->getOrder($order_id);

                if ($order_info) {
                    if (isset($this->request->get['order_product_id'])) {
                        $order_product_id = $this->request->get['order_product_id'];
                    } else {
                        $order_product_id = 0;
                    }
              
                    $order_product_info = $this->model_account_order->getOrderProduct($order_id, $order_product_id);

                    if ($order_product_info) {
                        $this->load->model('catalog/product');
                
                        $product_info = $this->model_catalog_product->getProduct($order_product_info['product_id']);
                
                        if ($product_info) {
                            if ((float)$product_info['special']) {
                                $price = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                            } else {
                                $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                            }

                            $price = $price * $order_product_info['quantity'];

                            $contents = array(
                                'id'       => $product_info['product_id'],
                                'quantity' => $order_product_info['quantity']
                            );

                            $facebook_pixel_event_params = array(
                                'event_name'    => $event_name,
                                'content_ids'   => array((string)$product_info['product_id']),
                                'content_name'  => $this->formatString($product_info['name']),
                                'content_type'  => 'product',
                                'contents'      => $contents,
                                'currency'      => strtoupper($this->session->data['currency']),
                                'value'         => $this->currency->format($this->tax->calculate($price, $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false),
                                'event_id'      => $event_id
                            );
                        }
                    }
                }

                break;

            case 'checkout/checkout':
                $event_name = 'InitiateCheckout';

                $this->load->language('checkout/checkout');

                $contents = array();
                $content_ids = array();
                $value = 0;
                $num_items = 0;

                foreach ($this->cart->getProducts() as $product) {
                    $content_ids[] = (string)$product['product_id'];
                    $value += $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    $num_items += $product['quantity'];
                    $contents[] = array(
                        'id'       => $product['product_id'],
                        'quantity' => $product['quantity']
                    );
                }

                $facebook_pixel_event_params = array(
                    'event_name'    => $event_name,
                    'content_name'  => $this->formatString($this->language->get('heading_title')),
                    'content_ids'   => $content_ids,
                    'content_type'  => 'product',
                    'contents'      => $contents,
                    'currency'      => strtoupper($this->session->data['currency']),
                    'num_items'     => $num_items,
                    'value'         => $value,
                    'event_id'      => $event_id
                );

                break;
    
            case 'product/search':
                $event_name = 'Search';

                $this->load->model('catalog/product');
                $this->load->language('product/search');

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

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

                if (isset($this->request->get['description'])) {
                    $description = $this->request->get['description'];
                } else {
                    $description = '';
                }
            
                if (isset($this->request->get['category_id'])) {
                    $category_id = $this->request->get['category_id'];
                } else {
                    $category_id = 0;
                }
            
                if (isset($this->request->get['sub_category'])) {
                    $sub_category = $this->request->get['sub_category'];
                } else {
                    $sub_category = '';
                }

                $page_filter_data = array(
                    'filter_name'         => $search,
                    'filter_tag'          => $tag,
                    'filter_description'  => $description,
                    'filter_category_id'  => $category_id,
                    'filter_sub_category' => $sub_category
                );

                $filter_data = $this->getFilterData($page_filter_data);

                $products = $this->model_catalog_product->getProducts($filter_data);

                $contents = array();
                $content_ids = array();
                $value = 0.0;
                $num_items = 0;

                foreach ($products as $product) {
                    $content_ids[] = $product['product_id'];

                    if ((float)$product['special']) {
                        $value += $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    } else {
                        $value += $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                    }

                    $num_items++;
                    $contents[] = array(
                        'id'        => $product['product_id'],
                        'quantity'  => 1
                    );
                }

                if (isset($this->request->get['search'])) {
                    $search_string = $this->request->get['search'];
                } elseif (isset($this->request->get['tag'])) {
                    $search_string = $this->request->get['tag'];
                } else {
                    $search_string = '';
                }

                $facebook_pixel_event_params = array(
                    'event_name'    => $event_name,
                    'content_ids'   => $content_ids,
                    'content_name'  => $this->formatString($this->language->get('heading_title')),
                    'content_type'  => 'product',
                    'contents'      => $contents,
                    'currency'      => strtoupper($this->session->data['currency']),
                    'search_string' => $search_string,
                    'value'         => $value,
                    'num_items'     => $num_items,
                    'event_id'      => $event_id
                );

                break;

            case 'product/category':
                $event_name = 'ViewCategory';

                $this->load->model('catalog/category');

                if (isset($this->request->get['path'])) {
                    $parts = explode('_', (string)$this->request->get['path']);

                    $category_id = (int)end($parts);
                } else {
                    $category_id = 0;
                }

                $category_info = $this->model_catalog_category->getCategory($category_id);

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

                    $page_filter_data = array(
                        'filter_category_id'     => $category_id,
                        'filter_filter'          => $filter,
                    );

                    $filter_data = $this->getFilterData($page_filter_data);

                    $products = $this->model_catalog_product->getProducts($filter_data);

                    $contents = array();
                    $content_ids = array();
                    $value = 0.0;
                    $num_items = 0;
    
                    foreach ($products as $product) {
                        $content_ids[] = $product['product_id'];
    
                        if ((float)$product['special']) {
                            $value += $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                        } else {
                            $value += $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                        }
    
                        $num_items++;
                        $contents[] = array(
                            'id'       => $product['product_id'],
                            'quantity' => 1,
                        );
                    }

                    $facebook_pixel_event_params = array(
                        'event_name'        => $event_name,
                        'content_name'      => $this->formatString($category_info['name']),
                        'content_category'  => $this->formatString($category_info['name']),
                        'content_ids'       => $content_ids,
                        'content_type'      => 'product',
                        'contents'          => $contents,
                        'currency'          => strtoupper($this->session->data['currency']),
                        'value'             => $value,
                        'num_items'         => $num_items,
                        'event_id'          => $event_id
                    );
                } else {
                    $facebook_pixel_event_params = array(
                        'event_name'        => $event_name,
                        'event_id'          => $event_id
                    );
                }
              
                break;

            case 'account/wishlist':
                $event_name = 'AddToWishlist';

                $this->load->language('account/wishlist');

                if (version_compare(VERSION, '2.0.3.1') <= 0) {
                    if (isset($this->session->data['wishlist'])) {
                        $wishlist = array_map(
                            function($product_id) {
                                return array('product_id' => $product_id);
                            },
                            $this->session->data['wishlist']
                        );
                    } else {
                        $wishlist = array();
                    }
                } else {
                    $this->load->model('account/wishlist');

                    $wishlist = $this->model_account_wishlist->getWishlist();
                }

                $this->load->model('catalog/product');

                $contents = array();
                $content_ids = array();
                $value = 0.0;
                $num_items = 0;

                foreach ($wishlist as $data) {
                    $product_info = $this->model_catalog_product->getProduct($data['product_id']);

                    if ($product_info) {
                        $content_ids[] = $product_info['product_id'];
                        
                        if ((float)$product_info['special']) {
                            $value += $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                        } else {
                            $value += $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                        }

                        $num_items++;

                        $contents[] = array(
                            'id'       => $product_info['product_id'],
                            'quantity' => 1
                        );
                    }
                }

                $facebook_pixel_event_params = array(
                    'event_name'    => $event_name,
                    'content_name'  => $this->formatString($this->language->get('heading_title')),
                    'content_ids'   => $content_ids,
                    'content_type'  => 'product',
                    'contents'      => $contents,
                    'currency'      => strtoupper($this->session->data['currency']),
                    'value'         => $value,
                    'num_items'     => $num_items,
                    'event_id'      => $event_id
                );

                break;

            case 'account/success':
                $event_name = 'CompleteRegistration';

                if ($this->customer->isLogged()) {
                    $this->language->load('account/success');

                    $facebook_pixel_event_params = array(
                        'event_name'        => $event_name,
                        'content_name'      => $this->formatString($this->language->get('heading_title')),
                        'currency'          => strtoupper($this->session->data['currency']),
                        'status'            => true,
                        'event_id'          => $event_id
                    );
                }

                break;
    
            case 'information/contact/success':
                $event_name = 'Contact';

                $facebook_pixel_event_params = array(
                    'event_name' => $event_name,
                    'event_id'   => $event_id
                );

                break;
            
            case 'product/manufacturer/info':
                $event_name = 'ViewBrand';

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

                if ($manufacturer_id) {
                    $this->load->model('catalog/manufacturer');

                    $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);

                    if ($manufacturer_info) {
                        $this->load->model('catalog/product');

                        $page_filter_data = array(
                            'filter_manufacturer_id'     => $manufacturer_id
                        );
    
                        $filter_data = $this->getFilterData($page_filter_data);
                        
                        $products = $this->model_catalog_product->getProducts($filter_data);

                        $contents = array();
                        $content_ids = array();
                        $value = 0.0;
                        $num_items = 0;
        
                        foreach ($products as $product) {
                            $content_ids[] = $product['product_id'];
        
                            if ((float)$product['special']) {
                                $value += $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                            } else {
                                $value += $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'], '', false);
                            }
        
                            $num_items++;
                            $contents[] = array(
                                'id'       => $product['product_id'],
                                'quantity' => 1,
                            );
                        }

                        $facebook_pixel_event_params = array(
                            'event_name'        => $event_name,
                            'content_name'      => $this->formatString($manufacturer_info['name']),
                            'content_category'  => $this->formatString($manufacturer_info['name']),
                            'content_ids'       => $content_ids,
                            'content_type'      => 'product',
                            'contents'          => $contents,
                            'currency'          => strtoupper($this->session->data['currency']),
                            'value'             => $value,
                            'num_items'         => $num_items,
                            'event_id'          => $event_id
                        );
                    } else {
                        $facebook_pixel_event_params = array(
                            'event_name'        => $event_name,
                            'event_id'          => $event_id
                        );
                    }
                }

                break;
        }
    
        $this->trackPixel($facebook_pixel_event_params, $event_name, $event_id);

        if ($facebook_pixel_event_params) {
            return addslashes(json_encode($facebook_pixel_event_params));
        } else {
            return $facebook_pixel_event_params;
        }
    }