Home > Net >  Make this Woocommerce-Shortcode show only certain categories
Make this Woocommerce-Shortcode show only certain categories

Time:01-25

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

Thanks a lot for your patience!

I tried to relace this

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

with

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
        }
    }
}

so I got this:

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();
    $terms = get_the_terms( $product_id, 'product_cat' );

    foreach ( $terms as $term ) {
        if($term->slug === 'the_category_slug') {
          products_ids[] = $product_id
        }
    }
}

}
$product_ids = array_unique( $product_ids );
$product_ids_str = implode( ",", $product_ids );

return do_shortcode("[products ids='$product_ids_str']");

}

But this code crashes my site, so I suppose I got I mistake in it, but I can't spot it... :-(

  •  Tags:  
  • Related