WC_Order_Data_Store_CPT::prime_refund_caches_for_order()privateWC 1.0

Prime refund cache for orders.

Method of the class: WC_Order_Data_Store_CPT{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->prime_refund_caches_for_order( $order_ids, $query_vars );
$order_ids(array) (required)
Order Ids to prime cache for.
$query_vars(array) (required)
Query vars for the query.

WC_Order_Data_Store_CPT::prime_refund_caches_for_order() code WC 9.4.2

private function prime_refund_caches_for_order( $order_ids, $query_vars ) {
	if ( ! isset( $query_vars['type'] ) || ! ( 'shop_order' === $query_vars['type'] ) ) {
		return;
	}
	if ( isset( $query_vars['fields'] ) && 'all' !== $query_vars['fields'] ) {
		if ( is_array( $query_vars['fields'] ) && ! in_array( 'refunds', $query_vars['fields'], true ) ) {
			return;
		}
	}
	$cache_keys_mapping = array();
	foreach ( $order_ids as $order_id ) {
		$cache_keys_mapping[ $order_id ] = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refunds' . $order_id;
	}
	$non_cached_ids = array();
	$cache_values   = wc_cache_get_multiple( array_values( $cache_keys_mapping ), 'orders' );
	foreach ( $order_ids as $order_id ) {
		if ( false === $cache_values[ $cache_keys_mapping[ $order_id ] ] ) {
			$non_cached_ids[] = $order_id;
		}
	}
	if ( empty( $non_cached_ids ) ) {
		return;
	}

	$refunds       = wc_get_orders(
		array(
			'type'            => 'shop_order_refund',
			'post_parent__in' => $non_cached_ids,
			'limit'           => - 1,
		)
	);
	$order_refunds = array_reduce(
		$refunds,
		function ( $order_refunds_array, WC_Order_Refund $refund ) {
			if ( ! isset( $order_refunds_array[ $refund->get_parent_id() ] ) ) {
				$order_refunds_array[ $refund->get_parent_id() ] = array();
			}
			$order_refunds_array[ $refund->get_parent_id() ][] = $refund;
			return $order_refunds_array;
		},
		array()
	);
	foreach ( $non_cached_ids as $order_id ) {
		$refunds = array();
		if ( isset( $order_refunds[ $order_id ] ) ) {
			$refunds = $order_refunds[ $order_id ];
		}
		wp_cache_set( $cache_keys_mapping[ $order_id ], $refunds, 'orders' );
	}
}