Automattic\WooCommerce\Blocks\BlockTypes

RelatedProducts::get_related_products_ids()privateWC 1.0

Get related products ids. The logic is copied from the core function woocommerce_related_products. https://github.com/woocommerce/woocommerce/blob/ca49caabcba84ce9f60a03c6d3534ec14b350b80/plugins/woocommerce/includes/wc-template-functions.php/#L2039-L2074

Method of the class: RelatedProducts{}

No Hooks.

Return

Array. Products ids.

Usage

// private - for code of main (parent) class only
$result = $this->get_related_products_ids( $product_per_page );
$product_per_page(number)
Products per page.
Default: 5

RelatedProducts::get_related_products_ids() code WC 8.9.0

private function get_related_products_ids( $product_per_page = 5 ) {
	global $post;

	$product = wc_get_product( $post->ID );

	$related_products = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $product_per_page, $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
	$related_products = wc_products_array_orderby( $related_products, 'rand', 'desc' );

	$related_product_ids = array_map(
		function( $product ) {
			return $product->get_id();
		},
		$related_products
	);

	return $related_product_ids;
}