【发布时间】:2021-02-01 23:24:57
【问题描述】:
我正在尝试获取订单详细信息以在我的感谢页面上显示摘要。我遇到的问题是我拥有的这段代码 sn-p 正在破坏我的 wordpress。我有堆栈跟踪,但我不确定如何修复它。对我来说,代码看起来是正确的,所以请确保它为什么不起作用。有谁知道这个堆栈跟踪的含义是什么以及如何修复它?
An error of type E_ERROR was caused in line 7 of the file /home4/xxx/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code. Error message: Uncaught Error: Call to a member function get_items() on boolean in /home4/xxx/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:7
Stack trace:
#0 /home4/xxx/public_html/staging/4326/wp-includes/shortcodes.php(343): order_table_summary('', '', 'order_table_sum...')
#1 [internal function]: do_shortcode_tag(Array)
#2 /home4/xxx/public_html/staging/4326/wp-includes/shortcodes.php(218): preg_replace_callback('/\\[(\\[?)(order_...', 'do_shortcode_ta...', '\n
我的代码:
function order_table_summary(){
$order = wc_get_order($order_id);
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
echo $item->get_name() . $item->get_quantity. $item->get_total();
}
}
add_shortcode('order_table_summary', 'order_table_summary');
更新 添加短代码
【问题讨论】:
-
$order_id是在哪里定义的?因此错误消息,$order对象不存在 -
变量
$order_id在你的函数中是未定义的。您可以阅读 PHP 手册中的 variable scope。 -
@7uc1f3r 好吧,我以为这是一个内置变量,但我错了。可悲的是,即使我在 $order 下设置了这个变量:$order_id = $order->get_id();,我的 wordpress 仍然出现错误
-
像
$order = wc_get_order( ADD AN EXISTING ORDER ID HERE );一样使用wc_get_order( 1 );然后使用if ( is_a( $order, 'WC_Order' ) ) { $order_status = $order->get_status();等等。
标签: php wordpress woocommerce shortcode orders