Automattic\WooCommerce\Internal\Fulfillments

FulfillmentsRenderer::handle_fulfillment_bulk_actionspublicWC 1.0

Handle bulk actions for fulfillments.

Method of the class: FulfillmentsRenderer{}

No Hooks.

Returns

String.

Usage

$FulfillmentsRenderer = new FulfillmentsRenderer();
$FulfillmentsRenderer->handle_fulfillment_bulk_actions( $redirect_to, $action, $post_ids );
$redirect_to(string) (required)
The redirect URL.
$action(string) (required)
The action being performed.
$post_ids(array) (required)
The post IDs being acted upon.

FulfillmentsRenderer::handle_fulfillment_bulk_actions() code WC 10.3.3

public function handle_fulfillment_bulk_actions( $redirect_to, $action, $post_ids ) {
	if ( 'fulfill' === $action ) {
		foreach ( $post_ids as $post_id ) {
			$order = wc_get_order( $post_id );
			if ( ! $order ) {
				continue;
			}

			$fulfillments = $this->maybe_read_fulfillments( $order );

			// Fulfill all existing fulfillments.
			foreach ( $fulfillments as $fulfillment ) {
				$fulfillment->set_status( 'fulfilled' );
				$fulfillment->save();
			}

			// Create a fulfillment for the order, containing all remaining items in the order.
			$remaining_items = array_map(
				function ( $item ) {
					return array(
						'item_id' => $item['item_id'],
						'qty'     => $item['qty'],
					);
				},
				FulfillmentUtils::get_pending_items( $order, $fulfillments )
			);

			if ( 0 < count( $remaining_items ) ) {
				$fulfillment = new Fulfillment();
				$fulfillment->set_entity_type( WC_Order::class );
				$fulfillment->set_entity_id( (string) $order->get_id() );
				$fulfillment->set_status( 'fulfilled' );
				$fulfillment->set_items( $remaining_items );
				$fulfillment->save();
			}
		}
		$redirect_to = add_query_arg( array( 'bulk_action' => $action ), $redirect_to );
	}
	return $redirect_to;
}