在 WooCommerce 中调用 boolean 上的成员函数 get_items() 谢谢 [英] Call to a member function get_items() on boolean in WooCommerce thankyou

查看:52
本文介绍了在 WooCommerce 中调用 boolean 上的成员函数 get_items() 谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取订单详细信息以在我的感谢页面上显示摘要.我遇到的问题是我拥有的这段代码正在破坏我的 wordpress.我有堆栈跟踪,但我不确定如何修复它.对我来说,代码看起来是正确的,所以请确保它不工作的原因.有没有人知道这个堆栈跟踪的含义是什么以及我如何解决它?

I am trying to get order details to display a summary on my thank you page. The issue I am having is that this code snippet I have is breaking my wordpress. I have the stacktrace but I am unsure on how to fix it. The code to me looks correct so ensure why it's not working. Does anybody have any idea what is the meaning of this stacktrace and how I can fix it?

   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


我的代码:


My code:

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未定义,$item->get_quantity需要为$item->get_quantity() 并且输出不应与短代码一起回显,始终返回.

The variable $order_id is not defined, $item->get_quantity need to be $item->get_quantity() and the output should never be echoed with a shortcode, always returned.

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

This shortcode is to be used on Order received page

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 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

用法:

  • [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]');
}

相关:

这篇关于在 WooCommerce 中调用 boolean 上的成员函数 get_items() 谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆