public function add_pushes($session_id) { $purchase = new WPSC_Purchase_Log($session_id, 'sessionid'); $purchase_id = $purchase->get('id'); $data = new WPSC_Checkout_Form_Data($purchase_id); $output = ''; $city = $data->get('billingcity'); $state = $data->get('billingstate'); $country = $data->get('billingcountry'); $state = !empty($state) ? wpsc_get_state_by_id($state, 'name') : ''; $cart_items = $purchase->get_cart_contents(); $total_shipping = wpsc_get_total_shipping($purchase_id); $total_tax = $total_price = 0; foreach ($cart_items as $item) { /* For backwards compatibility, convert objects to arrays */ $item = (array) $item; $total_tax += $item['tax_charged']; $total_price += absint($item['quantity']) * $item['price']; } if ($this->is_theme_tracking || $this->advanced_code) { $output .= "<script type='text/javascript'>\n\r"; } add_filter('wpsc_toggle_display_currency_code', array($this, 'remove_currency_and_html')); if ($this->use_universal_analytics()) { // Yoast GA Plugin switched to it's own object name __gaTracker - assign it to our ga object if it exists $output .= "var ga = typeof ga === 'undefined' && typeof __gaTracker !== 'undefined' ? __gaTracker : ga;"; $output .= "ga('require', 'ecommerce');\n\r"; $output .= "ga('ecommerce:addTransaction', {\n\t\t\t\t'id': '" . $purchase_id . "', // Transaction ID. Required.\n\t\t\t\t'affiliation': '" . wp_specialchars_decode($this->get_site_name()) . "', // Affiliation or store name.\n\t\t\t\t'revenue': '" . number_format($total_price, 2, '.', '') . "', // Grand Total.\n\t\t\t\t'shipping': '" . wpsc_currency_display($total_shipping) . "', // Shipping.\n\t\t\t\t'tax': '" . wpsc_currency_display($total_tax) . "' // Tax.\n\t\t\t});\n\r"; } else { $output .= "\n\t\t\t\t_gaq.push(['_addTrans',\n\t\t\t\t'" . $purchase_id . "', // order ID - required\n\t\t\t\t'" . wp_specialchars_decode($this->get_site_name()) . "', // affiliation or store name\n\t\t\t\t'" . number_format($total_price, 2, '.', '') . "', // total - required\n\t\t\t\t'" . wpsc_currency_display($total_tax) . "', // tax\n\t\t\t\t'" . wpsc_currency_display($total_shipping) . "', // shipping\n\t\t\t\t'" . wp_specialchars_decode($city) . "', // city\n\t\t\t\t'" . wp_specialchars_decode($state) . "', // state or province\n\t\t\t\t'" . wp_specialchars_decode($country) . "' // country\n\t\t\t]);\n\r"; } remove_filter('wpsc_toggle_display_currency_code', array($this, 'remove_currency_and_html')); foreach ($cart_items as $item) { /* For backwards compatibility, convert objects to arrays */ $item = (array) $item; $category = wp_get_object_terms($item['prodid'], 'wpsc_product_category', array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'all_with_object_id')); $item['sku'] = get_post_meta($item['prodid'], '_wpsc_sku', true); if (empty($item['sku'])) { $item['sku'] = $item['prodid']; } if ($category) { $item['category'] = $category[0]->name; } else { $item['category'] = ''; } $item = apply_filters('wpsc_google_analytics_pushed_product', array_map('wp_specialchars_decode', $item), $item, $this); if ($this->use_universal_analytics()) { $output .= "ga('ecommerce:addItem', {" . "'id': '" . $purchase_id . "'," . "'name': '" . $item['name'] . "'," . "'sku': '" . $item['sku'] . "'," . "'category': '" . $item['category'] . "'," . "'price': '" . $item['price'] . "'," . "'quantity': '" . $item['quantity'] . "'" . "});\n\r"; } else { $output .= "_gaq.push(['_addItem'," . "'" . $purchase_id . "'," . "'" . $item['sku'] . "'," . "'" . $item['name'] . "'," . "'" . $item['category'] . "'," . "'" . $item['price'] . "'," . "'" . $item['quantity'] . "']);\n\r"; // Item Quantity } } if ($this->use_universal_analytics()) { $output .= "ga('ecommerce:send');\n\r"; } else { $output .= "_gaq.push(['_trackTrans']);\n\r"; } if ($this->is_theme_tracking || $this->advanced_code) { $output .= "</script>\n\r"; } return $output; }
public function save_order($purchase_log) { $apiKey = get_option('retargeting_domain_api'); $token = get_option('retargeting_token'); if ($purchase_log instanceof WPSC_Purchase_Log) { $order = array('line_items' => array()); $checkout_form = new WPSC_Checkout_Form_Data($purchase_log->get('id')); $products = $purchase_log->get_cart_contents(); if (is_array($products)) { foreach ($products as $product) { $parent = $this->get_parent_post($product->prodid); if ($parent) { $product_id = $parent->ID; $product_name = $parent->post_title; } else { $product_id = $product->prodid; $product_name = $product->name; } $line_item = array('id' => (int) $product_id, 'quantity' => (int) $product->quantity, 'price' => $this->format_price($product->price), 'variation_code' => ''); $order['line_items'][] = $line_item; } } if ($apiKey && $apiKey != "" && $token && $token != "") { require_once "/lib/Retargeting_REST_API_Client.php"; $orderInfo = array("order_no" => $purchase_log->get('id'), "lastname" => $checkout_form->get('billinglastname'), "firstname" => $checkout_form->get('billingfirstname'), "email" => $checkout_form->get('billingemail'), "phone" => $checkout_form->get('billingphone'), "state" => $checkout_form->get('shippingstate'), "city" => $checkout_form->get('shippingcity'), "address" => $checkout_form->get('billingaddress'), "discount_code" => $purchase_log->get('discount_data'), "discount" => $purchase_log->get('discount_value'), "shipping" => $purchase_log->get('total_shipping'), "total" => $purchase_log->get('totalprice')); $orderClient = new Retargeting_REST_API_Client($apiKey, $token); $orderClient->setResponseFormat('json'); $orderClient->setDecoding(false); $response = $orderClient->order->save($orderInfo, $order['line_items']); } echo '<script type="text/javascript"> var _ra = _ra || {}; _ra.saveOrderInfo = { "order_no": ' . $purchase_log->get('id') . ', "lastname": "' . $checkout_form->get('billinglastname') . '", "firstname": "' . $checkout_form->get('billingfirstname') . '", "email": "' . $checkout_form->get('billingemail') . '", "phone": "' . $checkout_form->get('billingphone') . '", "state": "' . $checkout_form->get('shippingstate') . '", "city": "' . $checkout_form->get('shippingcity') . '", "address": "' . $checkout_form->get('billingaddress') . '", "discount_code": "' . $purchase_log->get('discount_data') . '", "discount": "' . $purchase_log->get('discount_value') . '", "shipping": "' . $purchase_log->get('total_shipping') . '", "total": "' . $purchase_log->get('totalprice') . '" }; _ra.saveOrderProducts = ' . json_encode($order['line_items'], JSON_PRETTY_PRINT) . ' if( _ra.ready !== undefined ){ _ra.saveOrder(_ra.saveOrderInfo, _ra.saveOrderProducts); } </script>'; } }