dsn1327 2016-04-30 01:00
浏览 42

WooCommerce:自定义电子商务跟踪 - 几乎就在那里

Using an excellent resource cited below and helpful comments I've build out a Wordpress plugin (as per code) to send WooCommerce transaction data to a custom CRM. From the thank you page.

I've got stuck however. I can send the transaction value, tax and shipping details but I can't seem to call, or send:
a) Product Name
b) Product Category
c) Product ID
d) Details of product splits between more than one product

Any thoughts, links or advice much appreciated. New to this and trying to battle through.

<?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->order_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', 'xxxxxxxx']);
_ss.push(['_setAccount', 'xxxxxxx']);
_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'>

_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 $order_billcity; ?>',
    'state': '<?php echo $order_billstate; ?>',
    'zipcode': '<?php echo $order_billpostcode; ?>',
    'country': 'UK',
    'firstName' : '<?php echo $order_fname; ?>', 
    'lastName' : '<?php echo $order_lname; ?>', 
    'emailAddress' : '<?php echo $order_email ?>' 
}, function() {

    _ss.push(['_addTransactionItem', {
        'transactionID': '<?php echo $order_id; ?>',
        'itemCode': '<?php echo $order_sku; ?>',
        'productName': '<?php echo $order_product; ?>',
        'category': '<?php echo $order_category; ?>',
        'price': '<?php echo $order_price; ?>',
        'quantity': '<?php echo $order_quantity; ?>'
    }]);

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

}]);

</script>


 <!-- End Tracking code -->
 <?php
 }
  • 写回答

1条回答 默认 最新

  • drn34916 2016-04-30 02:23
    关注

    Updated: compatibility for Woocommerce version 3+

    I suppose that you can't get Product Name, Product Category, Product ID and Details of product "splits between more than one product", because you can have more than 1 product in an order and the only way is to run a foreach loop

    foreach ( $order->get_items() as $item_values ) {
        $product_id = $item_values['product_id']; // Get Product ID
        $product_variation_id = $item['variation_id']; // Get variation ID
        // Get product name
        $product_name = method_exists( $product, 'get_name' ) ? $item_values['data']->get_name() : $item_values['name'];
        // Get product categories
        if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
            $categories_array = explode( ',', $item_values['data']->get_categories() );
        } else {
            $categories_array = explode( ',', wc_get_product_category_list( $item_values['product_id'] ) );
        }
    
        /* … and so on … */
    }
    

    That is just an example. You have to search over internet or here on SO "woocommerce get products from order" or "woocommerce get products details from order" to chose something that will feet your needs.


    As a reference: How to get WooCommerce order details

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题