public function update( &$data ): void {
// If the fulfillment is deleted, do nothing.
if ( $data->get_date_deleted() ) {
return;
}
// Update the fulfillment in the database.
$data_id = $data->get_id();
if ( ! FulfillmentUtils::is_valid_fulfillment_status( $data->get_status() ) ) {
throw new \Exception( esc_html__( 'Invalid fulfillment status.', 'woocommerce' ) );
}
$this->validate_items( $data );
/**
* Filter to modify the fulfillment data before it is updated.
*
* @param Fulfillment $data The fulfillment object that is being updated.
*
* @since 10.1.0
*/
$data = apply_filters( 'woocommerce_fulfillment_before_update', $data );
// If the fulfillment is fulfilled, set the fulfilled date.
$is_fulfill_action = false;
if ( $data->get_is_fulfilled() && empty( $data->get_date_fulfilled() ) ) {
$is_fulfill_action = true;
$data->set_date_fulfilled( current_time( 'mysql' ) );
/**
* Filter to modify the fulfillment data before it is fulfilled.
*
* @param Fulfillment $data The fulfillment object that is being fulfilled.
*
* @since 10.1.0
*/
$data = apply_filters(
'woocommerce_fulfillment_before_fulfill',
$data
);
}
global $wpdb;
// Capture changes and previous status before set_date_updated (which always
// changes) and before apply_changes resets the tracking.
$changes = $data->get_changes();
$previous_status = $data->get_data()['status'] ?? 'unfulfilled';
$data->set_date_updated( current_time( 'mysql' ) );
$wpdb->update(
$wpdb->prefix . 'wc_order_fulfillments',
array(
'entity_type' => $data->get_entity_type(),
'entity_id' => $data->get_entity_id(),
'status' => $data->get_status(),
'is_fulfilled' => $data->get_is_fulfilled() ? 1 : 0,
'date_updated' => $data->get_date_updated(),
'date_deleted' => $data->get_date_deleted(),
),
array(
'fulfillment_id' => $data_id,
'date_deleted' => null,
),
array( '%s', '%s', '%s', '%d', '%s', '%s' ),
array( '%d' )
);
// Check for errors.
if ( $wpdb->last_error ) {
throw new \Exception( esc_html__( 'Failed to update fulfillment.', 'woocommerce' ) );
}
// Update the metadata for the fulfillment.
$data->save_meta_data();
$data->apply_changes();
$data->set_object_read( true );
if ( ! doing_action( 'woocommerce_fulfillment_after_update' ) ) {
/**
* Action to perform after a fulfillment is updated.
*
* @param Fulfillment $data The fulfillment object that was updated.
* @param array $changes The changes that were applied, as returned by
* Fulfillment::get_changes() before save. Core data
* props at top level, meta changes under 'meta_data'.
* @param string $previous_status The fulfillment status before the update.
*
* @since 10.1.0
* @since 10.7.0 Added $changes and $previous_status parameters.
*/
do_action( 'woocommerce_fulfillment_after_update', $data, $changes, $previous_status );
}
if ( $is_fulfill_action && ! doing_action( 'woocommerce_fulfillment_after_fulfill' ) ) {
/**
* Action to perform after a fulfillment is fulfilled.
*
* @param Fulfillment $data The fulfillment object that was fulfilled.
*
* @since 10.1.0
*/
do_action( 'woocommerce_fulfillment_after_fulfill', $data );
}
}