Shortcode: Show a Customer’s Purchase History as a Product Grid
Registers a `[my_products]` shortcode that looks up everything the logged-in customer has ever bought (completed or processing orders) and renders it as a normal WooCommerce product loop — useful for a "buy again" section on the My Account page.
add_shortcode( 'my_products', 'wc_user_products_bought' );
function wc_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
// Get user
$current_user = wp_get_current_user();
// Get user orders (COMPLETED + PROCESSING)
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
// Loop Through orders and get product IDs
if ( ! $customer_orders ) return;
$product_ids = array();
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product_ids[] = $product_id;
}
}
$product_ids = array_unique( $product_ids );
// Query products
$args = array(
'post_type' => 'product',
'post__in' => $product_ids,
);
$loop = new WP_Query( $args );
// Generate WC loop
ob_start();
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
// Return content
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
More WooCommerce snippets
Add a Custom Post Type Through WooCommerce
Lets a non-`product` post type behave like a purchasable WooCommerce item — pulling its price from a custom field and giving it a working "Add to Cart" button, without converting it into an actual Product post type.
Reorder (and Rename) the My Account Menu Tabs
Controls the exact order the tabs appear in on the WooCommerce My Account page, and lets you rename any of them at the same time — including slotting in a custom tab you've added elsewhere.
Add a Custom Tab to the WooCommerce My Account Page
Registers a brand new endpoint (in this example, "Premium Support") and adds it as its own tab in the My Account menu, complete with its own content area — the standard pattern for extending My Account with anything WooCommerce doesn't ship by default.