WC_Product_Variable_Data_Store_CPT::read_childrenpublicWC 1.0

Loads variation child IDs.

Method of the class: WC_Product_Variable_Data_Store_CPT{}

Hooks from the method

Returns

Array.

Usage

$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT();
$WC_Product_Variable_Data_Store_CPT->read_children( $product, $force_read );
$product(WC_Product) (required) (passed by reference — &)
Product object.
$force_read(true|false)
True to bypass the transient.
Default: false

WC_Product_Variable_Data_Store_CPT::read_children() code WC 10.5.0

public function read_children( &$product, $force_read = false ) {
	$children_transient_name = 'wc_product_children_' . $product->get_id();
	$children                = get_transient( $children_transient_name );
	if ( empty( $children ) || ! is_array( $children ) ) {
		$children = array();
	}

	$transient_version = WC_Cache_Helper::get_transient_version( 'product' );

	if ( ! $force_read && $children ) {
		// Validate the children data.
		if ( ! $this->validate_children_data( $children, $transient_version ) ) {
			$children   = array();
			$force_read = true;
		}
	}

	if ( ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) {
		$all_args = array(
			'post_parent' => $product->get_id(),
			'post_type'   => 'product_variation',
			'orderby'     => array(
				'menu_order' => 'ASC',
				'ID'         => 'ASC',
			),
			'fields'      => 'ids',
			'post_status' => array( ProductStatus::PUBLISH, ProductStatus::PRIVATE ),
			'numberposts' => -1, // phpcs:ignore WordPress.VIP.PostsPerPage.posts_per_page_numberposts
		);

		$visible_only_args                = $all_args;
		$visible_only_args['post_status'] = ProductStatus::PUBLISH;

		if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
			$visible_only_args['tax_query'][] = array(
				'taxonomy' => 'product_visibility',
				'field'    => 'name',
				'terms'    => ProductStockStatus::OUT_OF_STOCK,
				'operator' => 'NOT IN',
			);
		}

		/**
		 * Filters the query arguments used to retrieve variation children of a variable product.
		 *
		 * @since 3.0.0
		 *
		 * @param array      $args            Query arguments for get_posts().
		 * @param WC_Product $product         The variable product object.
		 * @param bool       $visible_only    True when retrieving only visible variations, false for all variations.
		 */
		$children['all'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $all_args, $product, false ) );

		// phpcs:disable WooCommerce.Commenting.CommentHooks
		$children['visible'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $visible_only_args, $product, true ) );

		// Validate the children data before storing it in the transient.
		if ( $this->validate_children_data( $children, $transient_version ) ) {
			set_transient( $children_transient_name, $children, DAY_IN_SECONDS * 30 );
		}
	}

	$children['all']     = wp_parse_id_list( (array) $children['all'] );
	$children['visible'] = wp_parse_id_list( (array) $children['visible'] );

	return $children;
}