首页 > 解决方案 > 使用订单详细信息创建 Woocommerce 短代码

问题描述



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

我有一个自定义页面,客户在完成订单后被重定向到该页面。客人结账功能被禁用,因此所有购买的客户都将拥有一个帐户。在页面中,我想通过简码从订单中插入一些数据。这是一个示例:

“嗨 [custom-woocommerce-name],感谢您的购买。我们已收到您通过 [custom-woocommerce-payment] 支付的 [custom-woocommerce-total] 款项。一封电子邮件已发送到 [custom-woocommerce-email],等等等等。您的订单 #[custom-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();
}

我哪里错了?

谢谢,

标签: phpwordpresswoocommerceshortcode

解决方案


你可以这样试试:

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();
}

推荐阅读