首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用布尔值上的成员函数get_items() --谢谢

调用布尔值上的成员函数get_items() --谢谢
EN

Stack Overflow用户
提问于 2020-10-19 07:41:01
回答 1查看 2.6K关注 0票数 1

我正在尝试获取订单细节,以便在我的“谢谢”页面上显示一个摘要。我遇到的问题是,我拥有的这个代码片段正在破坏我的wordpress。我有堆积如山的痕迹,但我不知道如何解决它。在我看来,代码是正确的,所以要确保它不起作用。有没有人知道这个堆栈的意思是什么,以及我如何修复它?

代码语言:javascript
复制
   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

我的代码:

代码语言:javascript
复制
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');

添加短代码的更新

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-19 08:07:33

变量$order_id没有定义,$item->get_quantity需要是$item->get_quantity(),并且输出不应该与一个始终返回的短代码相呼应。

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

代码语言:javascript
复制
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]
  • or还在PHP代码中:echo do_shortcode('[order_table_summary]');

只是为了测试:

代码语言:javascript
复制
add_action( 'woocommerce_thankyou', 'testing_shorcode' );
function testing_shorcode() {
    echo do_shortcode('[order_table_summary]');
}

相关信息:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64423037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档