【问题标题】:Display only completed orders on my Woocommerce accounts page在我的 Woocommerce 帐户页面上仅显示已完成的订单
【发布时间】:2017-08-03 14:45:03
【问题描述】:

在我的 woocommerce 购物网站的帐户区域中,您可以点击“订单”并查看您的订单,即已完成、已取消、暂停等。

但是,当您单击“收藏”(我在帐户区域中创建的页面)时,我希望它只显示“已完成”的项目。

我尝试将以下代码放入functions.php,但没有成功:

(参考 - https://businessbloomer.com/woocommerce-display-products-purchased-user/

add_shortcode( 'my_products', 'bbloomer_user_products_bought' );

function bbloomer_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
$current_user = wp_get_current_user();
$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'meta_query'            => array(
        array(
            'key'           => '_visibility',
            'value'         => array('catalog', 'visible'),
            'compare'       => 'IN'
        )
    )
);
$loop = new WP_Query($args);

ob_start();

woocommerce_product_loop_start();

循环部分

while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
wc_get_template_part( 'content', 'product' ); 
} 
endwhile; 

woocommerce_product_loop_end();

woocommerce_reset_loop();
wp_reset_postdata();

return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}

然后添加短代码:

[my_products]

当您单击集合链接时,它不显示任何内容 - 生成一个没有任何内容的 div!

有没有更好的方法或替代方法?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    用下面的代码替换你的 $args

    $args = array(
        'post_type'             => 'shop_order',
        'post_status'           => 'wc-completed'
    );
    

    然后告诉我结果。谢谢

    【讨论】:

    • 感谢您的回复 - 我首先用您提供的内容替换了整个 '$args = array' 部分,但没有结果,然后将 'product' 和 'publish' 替换为 'shop_order' 和'wc-completed' 但它再次生成相同的 '
        ',其中没有任何内容。
      • 抱歉,我猜还有第二部分需要更新?我已将其添加到名为“循环部分”的问题中
      猜你喜欢
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多