L
L
Leonid2020-08-14 09:31:34
WordPress
Leonid, 2020-08-14 09:31:34

First product from the WooCommerce category?

It is necessary to determine whether the product is the first in the list of products from the category - how to do this?
Ie, for example, the category Cars and products in it:
Skoda
Hyndai
Ford
...
On the product page, you need to understand that Skoda is the first in order (sorting: default) product from the category Cars.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2020-08-17
@easycode

$_first_product = get_posts( array(
    'numberposts' => 1,
    'post_type'   => 'product',
    'category' => $_categories,
    'post_status' => 'publish',
    //'orderby'=>'menu_order',
    'order'=>'ASC',
) );

if ( is_array($_first_product) and count($_first_product) and $_first_product[0]->ID == $_product_id ) 
    $_is_first_product = true;

if $_is_first_product = true, then this is the first product in order in the category
in $_categories - the category ID of the current product

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question