【问题标题】:Create shortcode for displaying woocommers users order history创建用于显示 woocommerce 用户订单历史记录的简码
【发布时间】:2018-03-17 13:34:25
【问题描述】:

我正在尝试编写一个简码来显示用户的 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中调用简码函数

提前感谢您的每一个帮助。

【问题讨论】:

  • 此代码仅适用于模板'myaccount/my-orders.php',但不适用于'myaccount/orders.php'...
  • 感谢您的评论。我已经编辑了问题,错误的文件只是一个错字
  • 我没有收到这个错误……对我来说它有效

标签: php wordpress templates woocommerce shortcode


【解决方案1】:

my-orders.php 在 2.6.0 版本之后被弃用。 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');

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多