Automattic\WooCommerce\Internal\DataStores\Fulfillments
FulfillmentsDataStore::update
Method to update an existing fulfillment in the database.
Method of the class: FulfillmentsDataStore{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$FulfillmentsDataStore = new FulfillmentsDataStore(); $FulfillmentsDataStore->update( $data ): void;
- $data(Fulfillment) (required) (passed by reference — &)
- The fulfillment object to update.
FulfillmentsDataStore::update() FulfillmentsDataStore::update code WC 10.3.6
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;
$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' => current_time( 'mysql' ),
'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' ) );
}
// If the fulfillment is fulfilled, set the fulfilled date.
if ( $data->get_is_fulfilled() && ! $data->meta_exists( '_fulfilled_date' ) ) {
$data->set_date_fulfilled( current_time( 'mysql' ) );
}
// 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.
*
* @since 10.1.0
*/
do_action( 'woocommerce_fulfillment_after_update', $data );
}
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 );
}
}