简体   繁体   English

Woocommerce获取订单感谢您的页面并传递数据javascript片段

[英]Woocommerce Get Orders on Thank you page and pass data javascript snippet

I am trying to read from thankyou.php orders and populate order_id, sku, price, quantity to a javascript snipper. 我试图从thankyou.php订单中读取并将order_id,sku,价格,数量填充到javascript snipper。 The problem for me is how to "catch" the loop in php and pass those variables to JavaScript Snipper. 对我来说问题是如何“捕获”php中的循环并将这些变量传递给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 更新
As you are finding hard to locate functions.php file you can created a plugin. 当您发现很难找到functions.php文件时,您可以创建一个插件。 Create a PHP file as wh-thankyou-tracking.php under /wp-content/plugins/ and copy paste the below code to it and save it. /wp-content/plugins/下创建一个PHP文件作为wh-thankyou-tracking.php ,然后将下面的代码复制粘贴到它并保存。 And form admin panel Activate WH Order Tracking JS plugin 并且表单管理面板激活WH Order Tracking JS插件

<?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');

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 javascript 代码插入 woocommerce 感谢页面的头部标签中 - Insert javascript code into the head tags of the woocommerce thank you page 避免在Woocommerce谢谢页面中加载脚本 - Avoid loading a script in Woocommerce thank you page JavaScript-使用HTML5本地存储来存储表单数据并将其显示在“谢谢”页面上 - JavaScript - Using HTML5 Local Storage to store form data and to present it on the “Thank You” page 如何在感谢页面上获得 Shopify 交易通道? - How to get Shopify transaction getway on thank you page? 重定向用户以感谢页面 - Redirect user to thank you page 如何将 Google Ads 转化购买事件添加到我的 WooCommerce 感谢页面 - How to add Google Ads conversion purchase event to my WooCommerce Thank you Page 将值从电子商务结帐页面传递到感谢页面以进行 Google Analytics 跟踪 - Pass Values from an eCommerce Checkout Page to Thank You Page for Google Analytics tracking PHP / WordPress:捕获OnClick事件&#39;下载URL; 并将其传递到“谢谢”页面以初始化下载 - PHP/Wordpress: Capture OnClick Event 'Download URL; and Pass it To 'Thank You' Page for Initializing Download PHP + JS结帐,谢谢页面 - php + js checkout and thank you page Google跟踪代码管理器表单-谢谢页面 - Google Tag Manager Form - Thank You Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM