【问题标题】:Create Woocommerce shortcodes with order details使用订单详细信息创建 Woocommerce 短代码
【发布时间】:2020-02-03 09:18:23
【问题描述】:



我正在尝试创建一些与 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 sn-p,它为 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();
}

我哪里出错了?

谢谢,

【问题讨论】:

  • 在你的第二个sn-p中,你如何获得$order_id值?
  • 我没有,我猜错了。 @Dmitry 已经解决了!

标签: php wordpress woocommerce shortcode


【解决方案1】:

你可以试试这个方法:

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->get_billing_first_name(); 替换为任何其他“$order”对象即可。
猜你喜欢
  • 2017-01-17
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2015-01-28
  • 2016-12-28
  • 2023-03-30
相关资源
最近更新 更多