簡體   English   中英

Woocommerce獲取訂單感謝您的頁面並傳遞數據javascript片段

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

我試圖從thankyou.php訂單中讀取並將order_id,sku,價格,數量填充到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>

更新
當您發現很難找到functions.php文件時,您可以創建一個插件。 /wp-content/plugins/下創建一個PHP文件作為wh-thankyou-tracking.php ,然后將下面的代碼復制粘貼到它並保存。 並且表單管理面板激活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');

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM