Untitled
unknown
plain_text
2 years ago
3.8 kB
13
Indexable
// tracking script for Everflow
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
//Everflow order objects
$efOrder = array();
$efOrder['items'] = array();
$efOrder['oid'] = $order_id;
$efOrder['amt'] = $order->get_total();
$efOrder['bs'] = $order->get_billing_state();
$efOrder['bc'] = $order->get_billing_country();
$customer_ordered_already = lostemp_customer_already_bought_product($order->billing_email);
// Determine if any coupons were used for this transaction
$coupons = "";
$couponCount = 0;
foreach ($order->get_used_coupons() as $coupon) {
$couponCount++;
if($couponCount > 1) { // do not add comma unless more than one coupon
$coupons .= ',';
}
$coupons .= $coupon;
}
$efOrder['cc'] = $coupons;
// This is how to grab line items from the order
$line_items = $order->get_items();
$javascriptCode = '<script type="text/javascript" src="https://www.lcp5trk.com/scripts/sdk/everflow.js"></script>';
$skusQuantity = array();
foreach ( $line_items as $item ) {
$product = $order->get_product_from_item( $item );
if ( $product->get_type() === 'variation' ) {
$sku = $product->get_sku();
} else {
$sku = $product->get_sku();
}
if (!isset($skusQuantity[$sku])) {
$skusQuantity[$sku] = 0;
}
$skusQuantity[$sku]++;
}
// This loops over line items
foreach ( $line_items as $item ) {
//Init Everflow item
$efItem = array();
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU (variant or parent)
$efItem['vs'] = $efItem['ps'] = $adv2 = '';
if ( $product->get_type() === 'variation' ) {
$efItem['vs'] = $product->get_sku();
$adv2 = $efItem['vs'];
} else {
$efItem['ps'] = $product->get_sku();
$adv2 = $efItem['ps'];
}
// This is the qty purchased
$efItem['qty'] = $skusQuantity[$efItem['ps']];
$adv3 = $efItem['qty'];
// Line item total cost including taxes and rounded
//$efItem['p'] = $order->get_line_total( $item, true, true );
$efItem['p'] = $order->get_line_total( $item, false, true );
$efItems = array();
$efItems[] = $efItem;
$efOrder['items'] = $efItems;
// CUSTOMER ORDER COUNT
$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );
// GET CANCELLED ORDERS FOR CUSTOMER
$args = array(
'customer_id' => $current_user->ID,
'post_status' => 'cancelled',
'post_type' => 'shop_order',
'return' => 'ids',
);
$numorders_cancelled = 0;
$numorders_cancelled = count( wc_get_orders( $args ) ); // count the array of orders
// NON-CANCELLED equals TOTAL minus CANCELLED
$num_not_cancelled = $numorders - $numorders_cancelled;
$javascriptCode .= '
<script type="text/javascript">
EF.conversion({
aid: 1,
amount: '.$efItem['p'].',
order: '.json_encode($efOrder).',
adv1: "'.$product->get_name().'",
adv2: "'.$adv2.'",
adv3: "'.$adv3.'",
adv4: "'.$customer_ordered_already.'",
email: "'.$order->billing_email.'",
});
</script>';
}
echo $javascriptCode;
}
Editor is loading...