Automattic\WooCommerce\Internal\DataStores\StockNotifications
StockNotificationsDataStore::create
Create a new stock notification.
Method of the class: StockNotificationsDataStore{}
Hooks from the method
Returns
int|\WP_Error. The notification ID on success. WP_Error on failure.
Usage
$StockNotificationsDataStore = new StockNotificationsDataStore(); $StockNotificationsDataStore->create( $notification );
- $notification(Notification) (required) (passed by reference — &)
- The data object to create.
StockNotificationsDataStore::create() StockNotificationsDataStore::create code WC 11.3.0
public function create( &$notification ) {
global $wpdb;
// Fill in created and modified dates.
if ( ! $notification->get_date_created( 'edit' ) ) {
$notification->set_date_created( time() );
}
if ( ! $notification->get_date_modified( 'edit' ) ) {
$notification->set_date_modified( time() );
}
$insert = $wpdb->insert(
$this->get_table_name(),
array(
'product_id' => $notification->get_product_id( 'edit' ),
'user_id' => $notification->get_user_id( 'edit' ),
'user_email' => $notification->get_user_email( 'edit' ),
'status' => $notification->get_status( 'edit' ),
'date_created_gmt' => gmdate( 'Y-m-d H:i:s', $notification->get_date_created( 'edit' )->getTimestamp() ),
'date_modified_gmt' => gmdate( 'Y-m-d H:i:s', $notification->get_date_modified( 'edit' )->getTimestamp() ),
'date_confirmed_gmt' => $notification->get_date_confirmed( 'edit' ) ? gmdate( 'Y-m-d H:i:s', $notification->get_date_confirmed( 'edit' )->getTimestamp() ) : null,
'date_last_attempt_gmt' => $notification->get_date_last_attempt( 'edit' ) ? gmdate( 'Y-m-d H:i:s', $notification->get_date_last_attempt( 'edit' )->getTimestamp() ) : null,
'date_notified_gmt' => $notification->get_date_notified( 'edit' ) ? gmdate( 'Y-m-d H:i:s', $notification->get_date_notified( 'edit' )->getTimestamp() ) : null,
'date_cancelled_gmt' => $notification->get_date_cancelled( 'edit' ) ? gmdate( 'Y-m-d H:i:s', $notification->get_date_cancelled( 'edit' )->getTimestamp() ) : null,
'cancellation_source' => $notification->get_cancellation_source( 'edit' ),
),
array(
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
)
);
if ( false === $insert ) {
return new \WP_Error( 'db_insert_error', 'Could not insert stock notification into the database.' );
}
$notification_id = (int) $wpdb->insert_id;
$notification->set_id( $notification_id );
$notification->save_meta_data();
$notification->apply_changes();
/**
* Action: woocommerce_new_stock_notification
*
* Fires after a stock notification is created, from any code path.
* With double opt-in the new notification is `pending` until the shopper verifies it, so
* integrations that only want confirmed sign-ups can wait for
* `woocommerce_stock_notification_status_changed` to `active` instead.
*
* @since 11.3.0
*
* @param int $notification_id The notification ID.
* @param Notification $notification The notification.
*/
do_action( 'woocommerce_new_stock_notification', $notification_id, $notification );
return $notification_id;
}