Automattic\WooCommerce\Admin\API\Reports\Variations

DataStore::fill_deleted_product_name()protectedWC 1.0

Fill missing extended_info.name for the deleted products.

Method of the class: DataStore{}

No Hooks.

Return

null. Nothing.

Usage

// protected - for code of main (parent) or child class
$result = $this->fill_deleted_product_name( $products );
$products(array) (required)
Product data.

DataStore::fill_deleted_product_name() code WC 7.1.0

protected function fill_deleted_product_name( array &$products ) {
	global $wpdb;
	$product_variation_ids = [];
	// Find products with missing extended_info.name.
	foreach ( $products as $key => $product ) {
		if ( ! isset( $product['extended_info']['name'] ) ) {
			$product_variation_ids[ $key ] = [
				'product_id'   => $product['product_id'],
				'variation_id' => $product['variation_id'],
			];
		}
	}

	if ( ! count( $product_variation_ids ) ) {
		return;
	}

	$where_clauses = implode(
		' or ',
		array_map(
			function( $ids ) {
				return "(
					product_lookup.product_id = {$ids['product_id']}
					and
					product_lookup.variation_id = {$ids['variation_id']}
                    )";
			},
			$product_variation_ids
		)
	);

	$query = "
		select
			product_lookup.product_id,
			product_lookup.variation_id,
			order_items.order_item_name
		from
			{$wpdb->prefix}wc_order_product_lookup as product_lookup
			left join {$wpdb->prefix}woocommerce_order_items as order_items
			on product_lookup.order_item_id = order_items.order_item_id
		where
			{$where_clauses}
		group by
			product_lookup.product_id,
			product_lookup.variation_id,
			order_items.order_item_name
	";

	// phpcs:ignore
	$results = $wpdb->get_results( $query );
	$index   = [];
	foreach ( $results as $result ) {
		$index[ $result->product_id . '_' . $result->variation_id ] = $result->order_item_name;
	}

	foreach ( $product_variation_ids as $product_key => $ids ) {
		$product   = $products[ $product_key ];
		$index_key = $product['product_id'] . '_' . $product['variation_id'];
		if ( isset( $index[ $index_key ] ) ) {
			$products[ $product_key ]['extended_info']['name'] = $index[ $index_key ];
		}
	}
}