php - 使用订单详细信息创建 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();
}
我哪里错了?
谢谢,
解决方案
你可以这样试试:
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();
}
推荐阅读
- javascript - 以隐身模式打开时无法在第二个选项卡中访问 localStorage [Mozilla Firefox]
- html - 如何使用 HTML 和 CSS 将背景图像添加到标题中的文本?
- winexe - winexe 错误:无法连接到 svcctl 管道。NT_STATUS_ACCESS_DENIED
- html - 带有可滚动内容窗口的 CSS/HTML 响应式固定侧边栏导航
- vba - 如何在 Word 的绘图对象文本框中使用 Selection.Find
- laravel - Laravel 查询 - andWhere
- python - 如何在包含的库中模拟变量
- ios - kivy-ios 上带有 ios 配方的超链接
- c# - 属性更改时的 MultiBinding 颜色更改 - 如何务实地清除?
- android - AdvertisingIdClient:从 SharedPreferences 读取时出错 java.lang.SecurityException:不再支持 MODE_WORLD_READABLE