如何运行 WooCommerce 查看订单模板的短代码
我试图在 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。
为了构建代码,我遵循了以下参考文献:
也许我应该更改影响 < 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了解决方案。
经过一番研究,我遇到了这篇文章:我如何从WooCommerce中的订单密钥中获取订单ID?
之后,我尝试插入此
$ order_id = absInt = absint = absint(get_query_var('view-order'view-order''));
一切都很好。这是在同一情况下的任何人的解决方案。在这里,您可以找到您有兴趣通过短代码显示的所有内容: 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.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.