WooCommerce - get_order()不起作用 [英] WooCommerce - get_order() is not working

查看:156
本文介绍了WooCommerce - get_order()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个函数,通过它的ID检索订单。出于某种原因,我无法使WooCommerce全局函数 get_order 正常工作。我将一个有效的订单 id 传递给该函数并尝试打印出来以验证它是否正常工作。该函数已放置在 functions.php 文件中。

 函数getWC_order_details($ id){
global $ woocommerce;

$ order = get_order($ id);

print< pre>;
print_r($ order);
打印< / pre>;
}

我已经测试过从函数中回显其他数据的问题。

解决方案

首先让功能如下所示:

  function getWC_order_details($ order_id){
$ order = new WC_Order($ order_id);
var_dump($ order);
}

然后,将它用于 woo_commerce 操作过滤器

  function use_after_cart_table ){
getWC_order_details(40);
}
add_action('woocommerce_after_cart_table','use_after_cart_table');

因此,在将任何产品添加到购物车后,您会在购物车表格后看到一个数组包含所有详细信息。


$ b 注意:您可以使用任何其他操作或过滤器,并且可以找到它们这里



编辑:

  function getWC_order_details($ order_id){
$ order = new WC_Order($ order_id);
// var_dump($ order);
$ order_shipping_total = $ order-> get_shipping();
$ order_shipping_method = $ order-> get_shipping_methods();
var_dump($ order_shipping_total); //将其用于调试目的或查看该数组中的详细信息
var_dump($ order_shipping_method); //将其用于调试目的或查看该数组中的详细信息

$ _order = $ order-> get_items(); //获取有关产品的信息
foreach($ _ order as $ order_product_detail){
// var_dump($ order_product_detail);
echo< b>产品ID:< / b>。$ order_product_detail ['product_id']。< br>;
echo< b> Product Name:< / b>。$ order_product_detail ['name']。< br>< br>;
}
// var_dump($ _ order);
}


I'm trying to create a function that will retrieve an order by its ID. For some reason I can't get the WooCommerce global function get_order to work. I'm passing a valid order id to the function and trying to print it out to verify that it's working. The function has been placed in my functions.php file.

function getWC_order_details($id){
    global $woocommerce;

    $order = get_order( $id );

    print "<pre>";
    print_r($order);
    print "</pre>";
}

I have tested echoing other data out of the function without a problem.

解决方案

First of all make function like this :

function getWC_order_details($order_id) { 
    $order = new WC_Order( $order_id );
    var_dump($order);
}

After that, use it with some woo_commerce action or filter.

function use_after_cart_table(){
    getWC_order_details(40);
}
add_action( 'woocommerce_after_cart_table', 'use_after_cart_table' );

So after adding any product to the cart, you will see after cart table that there is one array containing all the details.

NOTE : You can use any other action or filter and you can find them here.

EDITED:

function getWC_order_details($order_id) { 
    $order = new WC_Order( $order_id );
    //var_dump($order);
    $order_shipping_total = $order->get_shipping();
    $order_shipping_method = $order->get_shipping_methods();
    var_dump($order_shipping_total);//Use it for debugging purpose or to see details in that array
    var_dump($order_shipping_method);//Use it for debugging purpose or to see details in that array

    $_order =   $order->get_items(); //to get info about product
    foreach($_order as $order_product_detail){
        //var_dump($order_product_detail);
        echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
        echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
    }
    //var_dump($_order);
}

这篇关于WooCommerce - get_order()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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