如何运行 WooCommerce 查看订单模板的短代码

发布于 2025-01-18 18:37:20 字数 1068 浏览 1 评论 0原文

我试图在 woocommerce view-order.php 模板中插入短代码,但它不起作用。 这是参考模板: https:// woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html

在functions.php文件中,我编写了以下代码:

add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
$customer_id = get_current_user_id();
$order = new WC_Order( $order_id ); //I think this is the problem I don't know if that's right
return $order->get_id();
}

短代码显示数字0,所以我没有收到订单id 在我的例子中是 40001。

为了构建代码,我遵循了以下参考文献:

  1. 如何为 Woocommerce 查看订单模板创建短代码?

  2. 使用订单创建 Woocommerce 短代码详情

也许我应该更改影响 < code>$order 部分,但我不确定。 我不明白我哪里错了,有人有建议吗?

I'm trying to insert a shortcode in the woocommerce view-order.php template but it doesn't work.
This is the reference template: https://woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html

In the functions.php file I wrote the following code:

add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
$customer_id = get_current_user_id();
$order = new WC_Order( $order_id ); //I think this is the problem I don't know if that's right
return $order->get_id();
}

The shortcode shows the number 0, so I'm not getting the order id which in my case is 40001.

To structure the code I followed these references:

  1. How to create a shortcode for Woocommerce view-order template?

  2. Create Woocommerce shortcodes with order details

Maybe I should change the line that affects the $order part, but I'm not sure.
I don't understand where I'm wrong, does anyone kindly have a suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2025-01-25 18:37:20

找到了解决方案。

经过一番研究,我遇到了这篇文章:我如何从WooCommerce中的订单密钥中获取订单ID?

之后,我尝试插入此$ order_id = absInt = absint = absint(get_query_var('view-order'view-order''));一切都很好。这是在同一情况下的任何人的解决方案。

add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){

// Get Order ID
$order_id = absint( get_query_var('view-order') );

// Then you can get the order object
$order = new WC_Order( $order_id );

// What you want to see, in my case the order ID
return $order->get_id();

}

在这里,您可以找到您有兴趣通过短代码显示的所有内容: https://www.businessbloomer.com/woocommerce-easily-get-cord-cord-cord-cord-total-total-items-etc-from-order-object/

记住,我写了代码在functions.php中。这使我可以将[order_view_id]快捷代码插入woocommerce view-order.php模板中。

Have found a solution.

After some research I came across this post: How can I get the order ID from the order Key in WooCommerce?

After that I tried to insert this $order_id = absint( get_query_var('view-order') ); Everything worked fine. Here is the solution for anyone in the same situation.

add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){

// Get Order ID
$order_id = absint( get_query_var('view-order') );

// Then you can get the order object
$order = new WC_Order( $order_id );

// What you want to see, in my case the order ID
return $order->get_id();

}

Here you can find everything you are interested in showing via shortcode: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Remember, I wrote the code in functions.php. This allows me to insert the [order_view_id] shortcode inside the woocommerce view-order.php template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文