创建用于显示woocommerce用户订单历史记录的短代码

问题描述 投票:0回答:1

我正在尝试编写一个短代码来显示用户的woocommerce订单历史记录。我在这里找到了答案in woocommerce, is there a shortcode/page to view all orders?,但那不再起作用了。

如果我按照当前的答案,它会给我一个致命的错误。

Fatal error: Call to undefined function wc_get_account_orders_actions() in /wp-content/themes/wrapgate/woocommerce/myaccount/my-orders.php on line 72

任何人都知道更新的代码,以使我的短代码工作?这是我尝试过的短代码功能

add_shortcode( 'woocommerce_history', 'woo_order_history' );

function woo_order_history() {
    ob_start();
    wc_get_template( 'myaccount/my-orders.php', array(
        'current_user'  => get_user_by( 'id', get_current_user_id() ),
        'order_count'   => -1
    ));
    return ob_get_clean();
}

如果我尝试使用相同的错误

woocommerce_account_orders( -1 );

Woocommerce和wordpress都是最新版本。我试图从我的主题functions.php调用短代码函数

提前感谢您的帮助。

php wordpress templates woocommerce shortcode
1个回答
0
投票

在版本2.6.0之后不推荐使用my-orders.php。 Woocommerce my-account现在使用orders.php。创建短代码以显示订单历史记录

function woo_order_history( $atts ) {
extract( shortcode_atts( array(
    'order_count' => -1
), $atts ) );

ob_start();
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'customer' => get_current_user_id(),
    'page'     => $current_page,
    'paginate' => true,
) ) );
wc_get_template(
    'myaccount/orders.php',
    array(
        'current_page'    => absint( $current_page ),
        'customer_orders' => $customer_orders,
        'has_orders'      => 0 < $customer_orders->total,
    )
);
return ob_get_clean();
}
add_shortcode('woocommerce_history', 'woo_order_history');

将此代码添加到theme-> functions.php或child-theme-> functions.php(如果启用了子主题)。现在您要显示订单的位置只需添加短代码[woocommerce_history]

© www.soinside.com 2019 - 2024. All rights reserved.