Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::upshift_or_delete_child_ordersprivateWC 1.0

Set the parent id of child orders to the parent order's parent if the post type for the order is hierarchical, just delete the child orders otherwise.

Method of the class: OrdersTableDataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->upshift_or_delete_child_orders( $order ): void;
$order(WC_Abstract_Order) (required)
Order object.

OrdersTableDataStore::upshift_or_delete_child_orders() code WC 10.7.0

private function upshift_or_delete_child_orders( $order ): void {
	global $wpdb;

	$order_table     = self::get_orders_table_name();
	$order_parent_id = $order->get_parent_id();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$child_order_ids = $wpdb->get_col(
		$wpdb->prepare(
			"SELECT id FROM $order_table WHERE parent_order_id=%d",
			$order->get_id()
		)
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

	if ( empty( $child_order_ids ) ) {
		return;
	}

	if ( $this->legacy_proxy->call_function( 'is_post_type_hierarchical', $order->get_type() ) ) {
		$wpdb->update(
			$order_table,
			array( 'parent_order_id' => $order_parent_id ),
			array( 'parent_order_id' => $order->get_id() ),
			array( '%d' ),
			array( '%d' )
		);

		$this->clear_cached_data( $child_order_ids );
	} else {
		foreach ( $child_order_ids as $child_order_id ) {
			$child_order = wc_get_order( $child_order_id );
			if ( $child_order ) {
				$child_order->delete( true );
			}
		}
	}
}