我想在订单成功(谢谢)页面上运行一些像素代码。为此,我正在收集必要的数据并调用javascript代码。问题在于,尽管没有代码调用任何突变/更新代码,但是订购商品的名称会自动更改为“ checkout”。
function my_custom_tracking($order_id) {
$order = wc_get_order($order_id);
$coupons = array();
foreach($order->get_used_coupons() as $used_coupon) {
$coupons[] = "$used_coupon";
}
$coupon = implode(',', $coupons);
$items = array();
foreach ($order->get_items() as $item_id => $item_data) {
$product = $item_data->get_product();
$variation = new WC_Product_Variation($product->get_variation_id());
$terms = get_the_terms($product->get_id(), 'product_cat');
$categories = array();
foreach($terms as $term) {
$categories[] = $term->name;
}
$item = [];
$item['subTotal'] = $item_data->get_total();
$item['quantity'] = $item_data->get_quantity();
$item['sku'] = $variation->get_sku();
$item['category'] = implode('/', $categories);
$items[] = $item;
}
$orderNumber = $order->get_order_number();
$items = json_encode($items);
wp_register_script( 'order-success-pixel-tracking', '', [], '', true );
wp_enqueue_script( 'order-success-pixel-tracking' );
wp_add_inline_script( 'order-success-pixel-tracking', <<<EOL
ire('trackConversion', 19124, {
orderId: "$orderNumber",
orderPromoCode: "$coupon",
items: $items
});
EOL);
}
add_action('woocommerce_thankyou', 'my_custom_tracking');
产品名称应保持不变。