wc_get_products is not returning related product id's

Viewed 278

I try to use wc_get_products in functions.php in WordPress. However, I have 2 problems. I only can see the data when using var_dump and the response is missing related_ids (relatable product id's).

The original WooCommerce api call /wp-json/wc/v3/products does return the related_ids (when calling from e.g. postman). BUT the wc_get_products()in PHP does not.

Code in functions.php

$productArgs = array(
     'numberposts' => -1,
     'post_status' => 'published',
     'include' => array(108903),
    );
$products = wc_get_products( $productArgs );

var_dump($products);
return $products;

I use the follow documentation: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query, but I don't see any information about related product id's.

1 Answers

By default, only the API returns the related_ids. To get the related ids for a product you may need to use the function wc_get_related_products.

$productArgs = array(
    'numberposts' => -1,
    'post_status' => 'published',
    'include' => array(108903),
);
$products = wc_get_products($productArgs);
$related_ids = array_map('absint', array_values(wc_get_related_products(108903)));
Related