【问题标题】:Call to a member function get_items() on boolean in WooCommerce thankyou在 WooCommerce 中调用布尔值的成员函数 get_items() 谢谢
【发布时间】: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();等等。
  • 这能回答你的问题吗? Reference - What does this error mean in PHP?

标签: php wordpress woocommerce shortcode orders


【解决方案1】:

变量$order_id 未定义,$item->get_quantity 需要为$item->get_quantity(),并且输出永远不应使用简码回显,始终返回。

此短代码将用于收到订单页面

add_shortcode('order_table_summary', 'display_order_table_summary_thankyou');

function display_order_table_summary_thankyou(){
    global $wp;

    // If order_id is defined on Order reveived / thankyou page
    if ( is_wc_endpoint_url('order-received')
    && isset($wp->query_vars['order-received'])
    && absint($wp->query_vars['order-received']) > 0 ) {

        // Get the WC_Order Object
        $order = wc_get_order( absint($wp->query_vars['order-received']) );

        ob_start(); // Start buffering

        echo '<table><tr>
            <th>' . __("Product name", "woocommerce") . '</th>
            <th>' . __("Quantity", "woocommerce") . '</th>
            <th>' . __("Line total", "woocommerce") . '</th>
        </tr>';

        // Loop Over Order Items
        foreach ( $order->get_items() as $item ) {
            echo '<tr>
            <td>' . $item->get_name() . '</td>
            <td>' . $item->get_quantity() . '</td>
            <td>' . $item->get_total() . '</td>
            </tr>';
        }

        echo '</table>';

        return ob_get_clean(); // Return the buffered content
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

用法:

  • [order_table_summary]
  • 或者在 PHP 代码中:echo do_shortcode('[order_table_summary]');

只是为了测试:

add_action( 'woocommerce_thankyou', 'testing_shorcode' );
function testing_shorcode() {
    echo do_shortcode('[order_table_summary]');
}

相关:

【讨论】:

  • @BruceyBandit 我在我的答案中为简码添加了一个测试功能。显示将在感谢页面的末尾。
  • 它没有显示短代码。这是一个创建的页面,我将其命名为“谢谢”,所以不知道这是否会有所不同。
  • 顺便说一句另一个问题。当我尝试它时它确实有效,但是当我刷新页面时,它说 wordpress 再次遇到错误。我想看看谁在 wordpress 中调试
  • 调试:stackoverflow.com/questions/61740111/…(在 woocommerce 和 wordpress 中)……我这里的答案代码没有出错,所以是别的。
  • 谢谢,我正在浏览一个显示相同内容的 youtube 视频。希望通过调试可以看到发生了什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多