Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Woocommerce在感谢页面上获取订单并传递数据javascript片段_Javascript_Php_Wordpress_Woocommerce - Fatal编程技术网

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

Woocommerce在感谢页面上获取订单并传递数据javascript片段,javascript,php,wordpress,woocommerce,Javascript,Php,Wordpress,Woocommerce,我试图阅读thankyou.php订单,并将订单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 to

我试图阅读thankyou.php订单,并将订单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>

order.purchase={
货币:欧元,
事务ID:“”,
产品:[{
id:“{PRODUCT_SKU}}}”,
类别:{{PRODUCT_category}}},
品牌:{{PRODUCT_brand}}},
价格:{{PRODUCT_price}}},
数量:“{{PRODUCT_quantity}}”
}, {
id:“{PRODUCT_SKU}}}”,
类别:{{PRODUCT_category}}},
品牌:{{PRODUCT_brand}}},
价格:{{PRODUCT_price}}},
数量:“{{PRODUCT_quantity}}”
}]
};

更新
当您发现很难找到functions.php文件时,您可以创建一个插件。在
/wp content/plugins/
下创建一个PHP文件,名为
wh thankyou tracking.PHP
,复制粘贴下面的代码并保存。和表单管理面板激活WH订单跟踪JS插件


order.purchase={
货币:欧元,
事务ID:“”,
产品:[]
};

非常感谢我的朋友。。。。如果我的主题没有functions.php怎么办?我必须手动创建它吗?我搜索了它所有的ftp文件夹结构…};如果有帮助的话,别忘了接受我的答案并投票。让我试试@raunak gupta-trued,插件无法激活?我们应该试试无插件解决方案吗?我一定会投赞成票。是否缺少“>”php关闭标记?真的,我在努力。。。。我可以让你通过ftp访问这些文件!!!!:)你有Skype ID吗?那就告诉我。激活插件时您面临的问题是什么?非常感谢!!!:)
<?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');