输出购买订单Woocommerce Tracking标签脚本中的项目数据

时间:2018-06-08 15:41:17

标签: javascript php wordpress woocommerce tracking

我正在尝试在结帐时在订单确认页面上添加一个woocommerce结帐通话中的商品,但它只是输出订购商品的空白值。

以下是代码:

    <!--Onefeed Tracking -->

<?php
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
    $product_id = $item['product_id'];
}
$items_ordered = implode(',', $items);
?>

<script type="text/javascript" language="javascript">
     var of_ssid = 'xxxx';
     var cs = '<?php echo $order->get_total(); ?>';
     var oi = '<?php echo $order->get_order_number(); ?>';
     var it = '<?php echo $order->get_item_count(); ?>';
     var pr = '<?php echo $items_ordered(); ?>';
     var nc = '';
</script>

<script type="text/javascript" language="javascript" src="https://tracking.onefeed.co.uk/t.js"></script>
<noscript>
    <img border="0" src="https://tracking.onefeed.co.uk/ProcessProductCheckout.ashx?of_ssid=xxxx&cs=<?php echo $order->get_total(); ?>&it=<?php echo $order->get_item_count() ?>&oi=<?php echo $order->get_order_number(); ?>&nc=&pr =<?php echo $items_ordered(); ?>" />
</noscript>
<!--End Onefeed Tracking -->

1 个答案:

答案 0 :(得分:1)

我们不知道您的脚本需要什么订单商品数据,以及如何格式化...

尝试以下方法:

add_action('woocommerce_thankyou', 'thankyou_tracking_onefeed_script', 20, 1 );
function thankyou_tracking_onefeed_script( $order_id ) {
    if ( ! $order_id )
        return;

    // Get an instance of the WC_Product object
    $order = wc_get_order( $order_id );

    // Get Order total amount and Order transaction ID
    $transaction_id = $order->get_transaction_id();

    $order_items = array();

    foreach ( $order->get_items() as $item_id => $item ) {
        $product    = $item->get_product();
        $product_id = $item->get_product_id();

        // Set unprotected item data in an array
        $order_items[]  = json_encode($item->get_data());
    }

    $order_items = implode( ',', $order_items );

    ?>
    <script type="text/javascript" language="javascript" src="https://tracking.onefeed.co.uk/t.js"></script>
    <script type="text/javascript" language="javascript">
         var of_ssid = 'xxxx';
         var cs = '<?php echo $order->get_total(); ?>';
         var oi = '<?php echo $order_id; ?>';
         var it = '<?php echo $order->get_item_count(); ?>';
         var pr = '<?php echo $order_items; ?>';
         var nc = '';
    </script>
    <noscript>
        <img border="0" src="https://tracking.onefeed.co.uk/ProcessProductCheckout.ashx?of_ssid=xxxx&cs=<?php echo $order->get_total(); ?>&it=<?php echo $order->get_item_count() ?>&oi=<?php echo $order->get_order_number(); ?>&nc=&pr =<?php echo $order_items; ?>" />
    </noscript>
    <?php
        echo '<p>BLABLA</p>';
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试,代码输出订单项数据。

有用的帖子:How to get WooCommerce order details