Woocommerce 在感谢页面上获取订单并传递数据 javascript 片段 [英] Woocommerce Get Orders on Thank you page and pass data javascript snippet

查看:16
本文介绍了Woocommerce 在感谢页面上获取订单并传递数据 javascript 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 thankyou.php 订单中读取并将 order_id、sku、价格、数量填充到 javascript 狙击手.我的问题是如何在 php 中捕获"循环并将这些变量传递给 JavaScript Snipper.

I am trying to read from thankyou.php orders and populate order_id, sku, price, quantity to a javascript snipper. The problem for me is how to "catch" the loop in php and pass those variables to JavaScript Snipper.

<?php

function CustomReadOrder ( $order_id ) {
    // Lets grab the order
    $order = wc_get_order( $order_id );
    // This is the order total
    $order->get_total();

    // This is how to grab line items from the order 
    $line_items = $order->get_items();

    // This loops over line items
    foreach ( $line_items as $item ) {
        // This will be a product
        $product = $order->get_product_from_item( $item );

        // This is the products SKU
        $sku = $product->get_sku();

        // This is the qty purchased
        $qty = $item['qty'];

        // Line item total cost including taxes and rounded
        $total = $order->get_line_total( $item, true, true );

        // Line item subtotal (before discounts)
        $subtotal = $order->get_line_subtotal( $item, true, true );

        echo $order_id."-";
        echo $sku."-";
        echo $qty;
    }
}
?>

<?php add_action( 'woocommerce_thankyou', 'CustomReadOrder' ); ?>

<script type="text/javascript">
order.purchase = {
    currency: 'EUR',
    transactionId: '<?php echo $order->id ?>',
    products: [{
        id: '{{PRODUCT_SKU}}',
        category: '{{PRODUCT_CATEGORY}}',
        brand: '{{PRODUCT_BRAND}}',
        price: '{{PRODUCT_PRICE}}',
        quantity: '{{PRODUCT_QUANTITY}}'
    }, {
        id: '{{PRODUCT_SKU}}',
        category: '{{PRODUCT_CATEGORY}}',
        brand: '{{PRODUCT_BRAND}}',
        price: '{{PRODUCT_PRICE}}',
        quantity: '{{PRODUCT_QUANTITY}}'
    }]
};
</script>

推荐答案

UPDATED
当您发现很难找到functions.php 文件时,您可以创建一个插件.在 /wp-content/plugins/ 下创建一个 PHP 文件作为 wh-thankyou-tracking.php 并将以下代码复制粘贴到其中并保存.和表单管理面板 Activate WH Order Tracking JS 插件

UPDATED
As you are finding hard to locate functions.php file you can created a plugin. Create a PHP file as wh-thankyou-tracking.php under /wp-content/plugins/ and copy paste the below code to it and save it. And form admin panel Activate WH Order Tracking JS plugin

<?php
/**
 * Plugin Name: WH Order Tracking JS
 * Version: 0.1
 * Description: This plugin will add a JS tracking code to WooCommerce Thankyou page.
 * Author: Raunak Gupta
 * Author URI: https://www.webhat.in/
 * Text Domain: wh
 */
if (!defined('ABSPATH'))
{
    exit;
} // Exit if accessed directly

if (!class_exists('WooCommerce'))
{
    exit;
}// Exit if WooCommerce is not active

function wh_CustomReadOrder($order_id)
{
    //getting order object
    $order = wc_get_order($order_id);

    $items = $order->get_items();
    $product_js = [];

    foreach ($items as $item_id => $item_data)
    {
        //getting product object
        $_product = wc_get_product($item_data['item_meta']['_product_id'][0]);

        //getting all the product category
        $pro_cat_array = wp_get_post_terms($_product->ID, 'product_cat');

        $sku = $sku = $_product->get_sku();
        $qty = $item_data['item_meta']['_qty'][0];
        $pro_cat = implode(',', $pro_cat_array);
        $pro_brand = $_product->get_attribute('pa_brand'); //replace it with your brand attribute slug
        $pro_price = $item_data['item_meta']['_line_total'][0];

        //storing all the line item as a string form
        $product_js[] = '{id: "' . $sku . '",category:"' . $pro_cat . '",brand:"' . $pro_brand . '",price: "' . $pro_price . '"quantity:"' . $qty . '"}';
    }

    ?>
    <script type="text/javascript">
        order.purchase = {
            currency: 'EUR',
            transactionId: '<?= $order->id ?>',
            products: [<?= implode(',', $product_js) ?>]
        };
    </script>
    <?php
}

add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

希望这有帮助!

这篇关于Woocommerce 在感谢页面上获取订单并传递数据 javascript 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆