• Resolved albertvanharten

    (@albertvanharten)


    I am creating a new plugin to WordPress, that uses data from WooCommerce.

    I am having the following code:

    $args = array(
        'limit' => -1,  // Attempt to fetch all products
        'return' => 'ids' // Fetch only IDs to optimize performance; remove this to get full product objects
    );
    
    $product_ids = wc_get_products($args);
    echo "<pre>";
    print_r($product_ids);
    echo "</pre>";

    This should display all the products I am having in my WooCommerce.
    However, it only returns four of them.

    I am also having the following code:

    function dispProduct($id)
    {
        $productArray = array(
            'product_id'       => wc_get_product($id)->get_id(),
            'name'             => wc_get_product($id)->get_name(),
            'sku'              => wc_get_product($id)->get_sku(),
            'price'            => wc_get_product($id)->get_price(),
            'stock_status'     => wc_get_product($id)->get_stock_status(),
            'total_sales'      => wc_get_product($id)->get_total_sales(),
            'regular_price'    => wc_get_product($id)->get_regular_price(),
            'sale_price'       => wc_get_product($id)->get_sale_price(),
            // Add more attributes as needed
        );
        return $productArray;
    }
    
    
    echo "<pre>";
    print_r(dispProduct(620));
    echo "</pre>";

    This displays some data from product 620. However, 620 is not in the list, that displays my products, even though the product clearly exists.

    When looking in WooCommerce, I can see I am having 9 products.
    If I quick-edit one of those, that are not in the $product_ids array, and just hit the “Update” button, without editing something, the product will show up in the array, after a refresh.

    Anyone having an idea?

    The page I need help with: [log in to see the link]

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

    (@shameemreza)

    Hi @albertvanharten

    For reference, these particular forums are meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or 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 some further insight/information.

Viewing 1 replies (of 1 total)
  • The topic ‘Cannot get all products in PHP using wc_get_products’ is closed to new replies.