【问题标题】:WooCommerce: Custom Ecommerce Tracking - almost thereWooCommerce:自定义电子商务跟踪 - 几乎就在那里
【发布时间】:2018-05-31 21:24:38
【问题描述】:

使用下面引用的优秀资源和有用的 cmets,我构建了一个 Wordpress 插件(根据代码)将 WooCommerce 交易数据发送到自定义 CRM。来自感谢页面。

但是我被卡住了。我可以发送交易金额、税金和运费详情,但我似乎无法致电或发送:
a) 产品名称
b) 产品类别
c) 产品编号
d) 多个产品之间的产品拆分详情

非常感谢任何想法、链接或建议。刚接触这个并试图通过战斗。

<?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
 }

【问题讨论】:

    标签: php wordpress woocommerce google-analytics custom-taxonomy


    【解决方案1】:

    更新:兼容 Woocommerce 版本 3+

    我想您无法获取产品“在多个产品之间拆分”的产品名称、产品类别、产品 ID 和详细信息,因为您可以在一个订单中拥有多个产品,唯一的方法是运行一个foreach循环

    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 … */
    }
    

    这只是一个例子。您必须通过互联网或在此处搜索“woocommerce 从订单中获取产品”或“woocommerce 从订单中获取产品详细信息”,以选择满足您需求的产品。


    作为参考:How to get WooCommerce order details

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2011-05-25
      相关资源
      最近更新 更多