[英]How to run a shortcode for WooCommerce view-order template
I'm trying to insert a shortcode in the woocommerce view-order.php template but it doesn't work.我正在尝试在 woocommerce view-order.php 模板中插入一个短代码,但它不起作用。 This is the reference template: https://woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html
这是参考模板: https://woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html
In the functions.php file I wrote the following code:在 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();
}
The shortcode shows the number 0, so I'm not getting the order id which in my case is 40001.简码显示数字 0,所以我没有得到订单 ID,在我的例子中是 40001。
To structure the code I followed these references:为了构建代码,我遵循了这些参考:
How to create a shortcode for Woocommerce view-order template? 如何为 Woocommerce 查看订单模板创建简码?
Create Woocommerce shortcodes with order details 使用订单详细信息创建 Woocommerce 简码
Maybe I should change the line that affects the $order
part, but I'm not sure.也许我应该更改影响
$order
部分的行,但我不确定。 I don't understand where I'm wrong, does anyone kindly have a suggestion?我不明白我哪里错了,有没有好心人有建议?
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?经过一番研究后,我发现了这篇文章: 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') );
之后我尝试插入这个
$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/在这里,您可以通过短代码找到您感兴趣的所有内容: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Remember, I wrote the code in functions.php.请记住,我在 functions.php 中编写了代码。 This allows me to insert the [order_view_id] shortcode inside the woocommerce view-order.php template.
这允许我在 woocommerce view-order.php 模板中插入 [order_view_id] 简码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.