Automattic\WooCommerce\Internal\DataStores\Fulfillments
FulfillmentsDataStore::create
Method to create a new fulfillment in the database.
Method of the class: FulfillmentsDataStore{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$FulfillmentsDataStore = new FulfillmentsDataStore(); $FulfillmentsDataStore->create( $data ): void;
- $data(Fulfillment) (required) (passed by reference — &)
- The fulfillment object to create.
FulfillmentsDataStore::create() FulfillmentsDataStore::create code WC 10.3.6
public function create( &$data ): void {
// Validate the fulfillment data.
if ( ! $data->get_entity_type() ) {
throw new \Exception( esc_html__( 'Invalid entity type.', 'woocommerce' ) );
}
if ( ! $data->get_entity_id() ) {
throw new \Exception( esc_html__( 'Invalid entity ID.', 'woocommerce' ) );
}
if ( ! FulfillmentUtils::is_valid_fulfillment_status( $data->get_status() ) ) {
throw new \Exception( esc_html__( 'Invalid fulfillment status.', 'woocommerce' ) );
}
$this->validate_items( $data );
// Set fulfillment properties.
$data->set_date_updated( current_time( 'mysql' ) );
/**
* Filter to modify the fulfillment data before it is created.
*
* @since 10.1.0
*/
$data = apply_filters( 'woocommerce_fulfillment_before_create', $data );
$is_fulfill_action = $data->get_is_fulfilled();
// If the fulfillment is fulfilled, set the fulfilled date.
if ( $is_fulfill_action ) {
$data->set_date_fulfilled( current_time( 'mysql' ) );
/**
* Filter to modify the fulfillment data before it is fulfilled.
*
* @since 10.1.0
*/
$data = apply_filters(
'woocommerce_fulfillment_before_fulfill',
$data
);
}
// Save the fulfillment to the database.
global $wpdb;
$rows_inserted = $wpdb->insert(
$wpdb->prefix . 'wc_order_fulfillments',
array(
'entity_type' => $data->get_entity_type(),
'entity_id' => $data->get_entity_id(),
'status' => $data->get_status() ?? 'unfulfilled',
'is_fulfilled' => $data->get_is_fulfilled() ? 1 : 0,
'date_updated' => $data->get_date_updated(),
'date_deleted' => $data->get_date_deleted(),
),
array( '%s', '%s', '%s', '%d', '%s', '%s' )
);
// Check for errors.
if ( false === $rows_inserted ) {
throw new \Exception( esc_html__( 'Failed to insert fulfillment.', 'woocommerce' ) );
}
// Set the ID of the fulfillment object.
$data_id = $wpdb->insert_id;
$data->set_id( $data_id );
// If the fulfillment is fulfilled, set the fulfilled date.
if ( $data->get_is_fulfilled() ) {
$data->set_date_fulfilled( current_time( 'mysql' ) );
}
// Save the metadata for the fulfillment to the database.
$data->save_meta_data();
// Apply changes let's the object know that the current object reflects the database and no "changes" exist between the two.
$data->apply_changes();
$data->set_object_read( true );
if ( ! doing_action( 'woocommerce_fulfillment_after_create' ) ) {
/**
* Action to perform after a fulfillment is created.
*
* @param Fulfillment $data The fulfillment object that was created.
*
* @since 10.1.0
*/
do_action( 'woocommerce_fulfillment_after_create', $data );
}
if ( $is_fulfill_action && ! doing_action( 'woocommerce_fulfillment_after_fulfill' ) ) {
/**
* Action to perform after a fulfillment is fulfilled.
*
* @since 10.1.0
*/
do_action( 'woocommerce_fulfillment_after_fulfill', $data );
}
}