WC_Product_Variable::get_available_variations( string $return = 'array' )

Get an array of available variations for the current product.


Description Description


Parameters Parameters

$return

(Optional) The format to return the results in. Can be 'array' to return an array of variation data or 'objects' for the product objects. Default 'array'.

Default value: 'array'


Top ↑

Return Return

(array[]|WC_Product_Variation[])


Top ↑

Source Source

File: includes/class-wc-product-variable.php

	public function get_available_variations() {

		$variation_ids        = $this->get_children();
		$available_variations = array();

		if ( is_callable( '_prime_post_caches' ) ) {
			_prime_post_caches( $variation_ids );
		}

		foreach ( $variation_ids as $variation_id ) {

			$variation = wc_get_product( $variation_id );

			// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
			if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
				continue;
			}

			// Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price).
			if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $this->get_id(), $variation ) && ! $variation->variation_is_visible() ) {
				continue;
			}

			$available_variations[] = $this->get_available_variation( $variation );
		}

		$available_variations = array_values( array_filter( $available_variations ) );

		return $available_variations;
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.