Automattic\WooCommerce\Internal\DataStores\Fulfillments
FulfillmentsDataStore::add_meta
Method to add metadata for a fulfillment.
Method of the class: FulfillmentsDataStore{}
No Hooks.
Returns
Int. meta ID or WP_Error on failure.
Usage
$FulfillmentsDataStore = new FulfillmentsDataStore(); $FulfillmentsDataStore->add_meta( $data, $meta ): int;
- $data(Fulfillment) (required) (passed by reference — &)
- The fulfillment object to save.
- $meta(WC_Meta_Data) (required)
- Meta object (containing at least ->id).
FulfillmentsDataStore::add_meta() FulfillmentsDataStore::add meta code WC 10.3.6
public function add_meta( &$data, $meta ): int {
// Add the metadata for the fulfillment.
global $wpdb;
// Prevent adding metadata to a deleted fulfillment.
if ( $data->get_date_deleted() ) {
throw new \Exception( esc_html__( 'Cannot add meta to a deleted fulfillment.', 'woocommerce' ) );
}
// Data ID can't be something wrong as this function is called after the meta is read.
// See WC_Data::save_meta_data().
$data_id = $data->get_id();
$wpdb->insert(
$wpdb->prefix . 'wc_order_fulfillment_meta',
array(
'fulfillment_id' => $data_id,
'meta_key' => $meta->key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => wp_json_encode( $meta->value ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
),
array(
'%d',
'%s',
'%s',
)
);
// Note: There is no error check on WC_Data::save_meta_data(), and it expects us to return an ID in all cases.
// If there's an error, we should return null to indicate we didn't save it.
if ( $wpdb->last_error ) {
throw new \Exception( esc_html__( 'Failed to insert fulfillment meta.', 'woocommerce' ) );
}
return $wpdb->insert_id;
}