skip to Main Content

I found this function-snippet and inserted it to my site. Works great, so far, but I’d need to make a minor change: how could I make the function display just a certain category…?

Here is the code:

add_shortcode( 'my_purchased_products', 'products_bought_by_curr_user' );
   
function products_bought_by_curr_user() {
   
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) return;
   
    $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() ),
    ) );
   
    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 );
    $product_ids_str = implode( ",", $product_ids );
   
    return do_shortcode("[products ids='$product_ids_str']");
   
}

Could someone please push me in the right direction? 😉

Best regards
Andi

3

Answers


  1. You can get product category by Product ID.
    and now , you can just use if condition to check.

    if category id is X , so do job.

    Login or Signup to reply.
  2. I misread your question so it’s edit time. You won’t be able to filter the category on get_posts() because you’re gettings Orders and not Products (this is why my solution doesn’t work).

    To achieve your filtering, you will need to work on this part :

        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            $product_ids[] = $product_id;
        }
    

    by checking that the product contains the category you wish to get products from. Something like this should work:

            foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            $terms = get_the_terms( $product_id, 'product_cat' );
        
            foreach ( $terms as $term ) {
                if($term->slug === 'the_category_slug') {
                  $products_ids[] = $product_id;
                }
            }
        }
    

    I used the code from this post How to get categories from an order at checkout in WooCommerce?

    So what is happening here is that after getting the product_id, we get the product categories using the get_terms (the ‘product_cat’ param is to get only product categories and not product tags with it, as both categories and and tags are terms).

    After getting the term, we loop into it and check for each one if it is the category we want to get). If it is, we push the product id to the results array.

    We’re using the category slug here but we can use the ID by replacing

    if($term->slug === 'the_category_slug')
    

    by

    if($term->term_id === 149)
    

    Where 149 is the ID of the category you want

    Let me know if you need more help and good luck again

    Login or Signup to reply.
  3. To respond to your new crashing problem :

    I recommend that you activate the WordPress Debug (which shows the PHP errors on your screen if the site crashes).
    You can do that by editing the wp-config.php file at the project root (the top-level folder of your project) and put the follow line in it :

    define( 'WP_DEBUG', true ); 
    

    (you can put it anywhere). You should then see why your site is crashing.

    I didn’t have time yet to test the code but something I see is that you forgot the $ and the ; character on the line

    products_ids[] = $product_id
    

    which should become

    $products_ids[] = $product_id;
    

    If you get an error on screen come back and post it here 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search