samedi 30 avril 2016

PHP Tracking Script - Error sending multiple products WooCommerce

I now have the below which is sending WooCommerce order details to a CRM...

However, when more than 1 item is in the basket and checked out, only the first item reaches the CRM. Any second, third items do not get sent.

I was hoping there's an obvious mistake that could easily be spotted by someone far more talented than I??

<?php
/**
 * Plugin Name: eCommerce Tracking
 * Plugin URI: 
 * Description: Send shopping data to CRM
 * Version: 1.0
 * Author: Fly
 * Author URI: 
 * License: GPL2
 */

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {

// Lets grab the order
    $order = new WC_Order( $order_id );

    // Order ID
    $order_id = $order->get_order_number();

    // Order total
    $order_total = $order->get_total();

    // Order e-mail
    $order_email = $order->billing_email;

    // Order Billing First Name
    $order_fname = $order->billing_first_name;

    // Order Billing Last Name
    $order_lname = $order->billing_last_name;

    // Order Billing Address 1
    $order_bill1 = $order->billing_address_1;

    // Order Billing Address 2
    $order_bill2 = $order->billing_address_2;

    // Billing City
    $order_billcity = $order->billing_city;

    // Billing State
    $order_billstate = $order->billing_state;

    // Billing Postcode
    $order_billpostcode = $order->billing_postcode;

    // Billing Country
    $order_billcountry = $order->billing_country;

    // Billing Phone
    $order_billphone = $order->billing_phone;

    // Order Tax Cost
    $order_tax = $order->get_total_tax();

    // Order Shipping Cost
    $order_shippingcost = $order->order_shipping;

    // Order Currency
    $order_currency = $order->order_currency;

    // Product Category
    $order_category = $order->term_id;

    // Product Name
    $order_product = $order->item_id;

    // Product Quantity
    $order_quantity = $order->quantity;

    // Product SKU
    $order_sku = $order->sku;

    // Product Price
    $order_price = $order->price;


?>




    <!-- Start Tracking code -->
 <script type="text/javascript">
var _ss = _ss || [];
_ss.push(['_setDomain', 'http://ift.tt/1r5Beju']);
_ss.push(['_setAccount', 'KOI-XXXXXXXX']);
_ss.push(['_trackPageView']);
(function() {
    var ss = document.createElement('script');
    ss.type = 'text/javascript'; ss.async = true;

    ss.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'koi-3Q40EJNC5K.marketingautomation.services/client/ss.js?ver=1.1.1';
    var scr = document.getElementsByTagName('script')[0];
    scr.parentNode.insertBefore(ss, scr);
})();
</script>



<script type='text/javascript'>

    // SECOND EXAMPLE, USES CALLBACKS TO ENSURE ORDERING, AS OF Version 2.1 of the tracking (you will have to update your embeds)

    _ss.push(['_setTransaction', {
        'transactionID': '<?php echo $order_id; ?>',
        'storeName': 'Reco Surfaces',
        'total': '<?php echo $order_total; ?>',
        'tax': '<?php echo $order_tax; ?>',
        'shipping': '<?php echo $order_shippingcost; ?>',
        'city': '<?php echo htmlspecialchars($order_billcity); ?>', 
        'state': '<?php echo $order_billstate; ?>',
        'zipcode': '<?php echo $order_billpostcode; ?>',
        'country': 'UK',
        // the following params can be used for creating/updating
        // a contact in the context of the supplied transaction data.
        // if this data is omitted, the underlying contact/tracking data
        // associated with the visitors browser session is used automatically.
        'firstName' : '<?php echo $order_fname; ?>', // optional parameter
        'lastName' : '<?php echo $order_lname; ?>', // optional parameter
        'emailAddress' : '<?php echo $order_email ?>' // optional parameter
    }, function() {

<?php
$products = $order->get_items();


$count = 0;
 foreach( $products as $item_id => $item ) {
 $count++;
$product = $order->get_product_from_item( $item ); 
       $terms = get_the_terms( $item['product_id'], 'product_cat' );
$product_cat = '';
        foreach ($terms as $term) {
            $product_cat = $term->name;
            break;
        }
?>
        _ss.push(['_addTransactionItem', {
            'transactionID': '<?php echo $order_id; ?>',
            'itemCode': '<?php echo $product->get_sku(); ?>',
            'productName': '<?php echo $item['name']; ?>',
            'category': '<?php echo $product_cat; ?>',
            'price': '<?php echo $order->get_line_subtotal( $item ) / $item['qty']; ?>',
            'quantity': '<?php echo $item['qty']; ?>'
        }]);

<?php 
} 
?>

        _ss.push(['_completeTransaction', {
            'transactionID': '<?php echo $order_id; ?>'
        }]);

    }]);

</script>


    <!-- End Tracking code -->
<?php


}

?>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire