Php 创建包含订单详细信息的Woocommerce短代码

Php 创建包含订单详细信息的Woocommerce短代码,php,wordpress,woocommerce,shortcode,Php,Wordpress,Woocommerce,Shortcode,我正在尝试创建一些与woocommerce订单数据相关的短代码。 我有一个自定义页面,客户在订单完成时被重定向到该页面。来宾结账已禁用,因此所有购买的客户都将拥有一个帐户。在页面中,我想通过快捷码从订单中插入一些数据。下面是一个例子: “您好[custom Woocmerce name],感谢您的购买。我们已通过[custom Woocmerce payment]收到您的[custom Woocmerce total]付款。 一封电子邮件已发送到[自定义woocommerce电子邮件],诸如



我正在尝试创建一些与woocommerce订单数据相关的短代码。

我有一个自定义页面,客户在订单完成时被重定向到该页面。来宾结账已禁用,因此所有购买的客户都将拥有一个帐户。在页面中,我想通过快捷码从订单中插入一些数据。下面是一个例子:

“您好[custom Woocmerce name],感谢您的购买。我们已通过[custom Woocmerce payment]收到您的[custom Woocmerce total]付款。 一封电子邮件已发送到[自定义woocommerce电子邮件],诸如此类。您的订单#[自定义woocommerce orderid]已打包诸如此类。“

因此,我要查找的是访问以下数据:

$order->get_billing_first_name();
$order->get_total();
$order->get_payment_method();
$order->get_billing_email();
$order->get_id();

我有一个正在工作的php代码段,它为wordpress用户名创建了一个短代码:

add_shortcode( ‘custom-wordpress-name' , ‘custom_user_name' );
function custom_user_name(){
    $user = wp_get_current_user();
    return $user->user_firstname;
}
我曾尝试对此进行调整,但我对php的理解非常有限,这会产生错误。

add_shortcode( ‘custom-woocommerce-name' , ‘custom_first_name' );
function custom_first_name(){
   $order = wc_get_order( $order_id );
   return $order->get_billing_first_name();
}
我哪里出错了?

谢谢,

您可以这样尝试:

add_shortcode( 'custom-woocommerce-name' , 'custom_first_name' );
function custom_first_name(){
    $customer_id = get_current_user_id();
    $order = wc_get_customer_last_order( $customer_id );
    return $order->get_billing_first_name();
}

在您的第二个代码片段中,如何获取
$order\u id
值?我没有,我猜错了。@Dmitry已经解决了它!太棒了!工作起来很有魅力,我还使用了代码来收集其他数据。只需将代码中的
$order->get\u billing first\u name();
替换为任何其他“$order”对象即可。