Automattic\WooCommerce\Blocks\BlockTypes

ClassicTemplate::filter_products_by_stock()publicWC 1.0

Filter products by stock status when as query param there is "filter_stock_status"

{} Это метод класса: ClassicTemplate{}

Хуков нет.

Возвращает

Массив.

Использование

$ClassicTemplate = new ClassicTemplate();
$ClassicTemplate->filter_products_by_stock( $meta_query );
$meta_query(массив) (обязательный)
Meta query.

Код ClassicTemplate::filter_products_by_stock() WC 6.7.0

public function filter_products_by_stock( $meta_query ) {
	if ( is_admin() ) {
		return $meta_query;
	}

	$stock_status = array_keys( wc_get_product_stock_status_options() );
	$values       = get_query_var( self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM );

	$values_to_array = explode( ',', $values );

	$filtered_values = array_filter(
		$values_to_array,
		function( $value ) use ( $stock_status ) {
			return in_array( $value, $stock_status, true );
		}
	);

	if ( ! empty( $filtered_values ) ) {

		$meta_query[] = array(
			'key'     => '_stock_status',
			'value'   => $filtered_values,
			'compare' => 'IN',
		);
	}
	return $meta_query;
}