Woocommerce List of all previously ordered products.

I am trying to customize a page that the user can continue, and they can see all the products they bought in the standard Woocommerce format so that they can add them to the cart.

I tried this, but only 1 appears at every moment annoyingly:

<ul class="products">
[insert_php]
        $user_id        = get_current_user_id();
        $current_user   = wp_get_current_user();
        $customer_email = $current_user->email;
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : 
                $loop->the_post(); 
                $_product = get_product( $loop->post->ID );
                if (wc_customer_bought_product($customer_email, $user_id,$_product->id)){
                     woocommerce_get_template_part( 'content', 'product' );
                }
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
[/insert_php]
</ul><!--/.products-->
+4
source share
2 answers

, 12 , , . , , . , , . :

<ul class="products">
[insert_php]
        global $wpdb;

        $current_user   = wp_get_current_user();
        $customer_email = $current_user->user_email;
        $customer_data = array( $customer_email );
        $statuses      = array_map( 'esc_sql', wc_get_is_paid_statuses() );

        $result = $wpdb->get_col( "
                SELECT im.meta_value FROM {$wpdb->posts} AS p
                INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
                INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
                INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
                WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
                AND pm.meta_key IN ( '_billing_email', '_customer_user' )
                AND im.meta_key IN ( '_product_id', '_variation_id' )
                AND im.meta_value != 0
                AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
            " );
        $product_ids = array_map( 'absint', $result );

        if ( !empty( $product_ids ) ) {
            $args = array(
                'post_type'         => 'product',
                'post__in'          => $product_ids,
                'posts_per_page'    => 12
            );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : 
                    $loop->the_post(); 
                     woocommerce_get_template_part( 'content', 'product' );
                endwhile;
            } else {
                echo __( 'No products found' );
            }
            wp_reset_postdata();
        } else {
            echo __( 'No products found' );
        }
[/insert_php]
</ul><!--/.products-->

sql WooCommerce wc_customer_bought_product. , , - , , . , , . , , , .

+1

(WP 4.8, WooCommerce 3.1.0, Insert PHP 1.3).

:

, .

, . , .

0

Source: https://habr.com/ru/post/1678801/


All Articles