Automattic\WooCommerce\Internal\DataStores\StockNotifications

StockNotificationsDataStore::createpublicWC 1.0

Create a new stock notification.

Method of the class: StockNotificationsDataStore{}

No Hooks.

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() code WC 10.3.6

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();

	return $notification->get_id();
}