• Resolved levke21

    (@levke21)


    Hello,

    I would like to extract the product identifiers from the product list. But I would not need the product identifiers of the entire product list, only a subset that currently appears.

    This code snippet correctly counts the product IDs, but always only takes the current product page into account and puts the identifiers of those products into the array:

    $all_products = $wp_query->posts;
    $product_ids = [];
    $product_ids = wp_list_pluck($all_products, 'ID');

    I would need PHP code that puts the product identifiers into an array in a similar way to the above method, but collects product identifiers from other pages as well if the current product set has multiple product pages (pagination).

    The following code is a PHP code snippet belonging to the WP Template that counts the number of products. However, I cannot extract the product identifiers from this. But the logic is appropriate:

    if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) { return; } $total = wc_get_loop_prop( 'total' );

    I would like to request assistance with this matter.

    Thank you!

Viewing 1 replies (of 1 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @levke21,

    If you want to extract product identifiers from multiple pages, you’ll need to modify your query to include pagination.

    Although we don’t provide custom code as per our support policy, however, here’s an example of how you could adjust your code:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
    'post_type' => 'product',
    'posts_per_page' => 10,
    'paged' => $paged,
    );
    
    $loop = new WP_Query($args);
    
    $product_ids = array();
    
    while ($loop->have_posts()) : $loop->the_post();
    $product_ids[] = get_the_ID();
    endwhile;
    
    wp_reset_query();

    This code will only get the product IDs from the current page. If you want to get the product IDs from all pages, you would need to run this loop for each page.

    Also, I would like to recommend our WooCommerce Code Reference, a detailed guide to WooCommerce PHP functions, classes, methods, and hooks. This could be an excellent resource for you in the future.

    Furthermore, if you still need help, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

Viewing 1 replies (of 1 total)
  • The topic ‘Current product list with product pages (pagination)’ is closed to new replies.