Automattic\WooCommerce\Internal\DataStores\StockNotifications

StockNotificationsDataStore::updatepublicWC 1.0

Update a stock notification.

Method of the class: StockNotificationsDataStore{}

No Hooks.

Returns

Int|\WP_Error. The number of rows updated or WP_Error on failure.

Usage

$StockNotificationsDataStore = new StockNotificationsDataStore();
$StockNotificationsDataStore->update( $notification );
$notification(Notification) (required) (passed by reference — &)
The data object to update.

StockNotificationsDataStore::update() code WC 10.3.6

public function update( &$notification ) {
	global $wpdb;

	if ( 0 === $notification->get_id() ) {
		return new \WP_Error( 'invalid_stock_notification', 'Invalid notification ID.' );
	}

	$changes = $notification->get_changes();
	$result  = 0;

	if ( array_intersect( array( 'product_id', 'user_id', 'user_email', 'status', 'date_modified', 'date_confirmed', 'date_last_attempt', 'date_notified', 'date_cancelled', 'cancellation_source' ), array_keys( $changes ) ) ) {

		if ( ! array_key_exists( 'date_modified', $changes ) ) {
			$notification->set_date_modified( time() );
		}

		$result = $wpdb->update(
			$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'      => $notification->get_date_created( 'edit' ) ? gmdate( 'Y-m-d H:i:s', $notification->get_date_created( 'edit' )->getTimestamp() ) : null,
				'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( 'id' => $notification->get_id() ),
			array( '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ),
			array( '%d' )
		);

		if ( false === $result ) {
			return new \WP_Error( 'db_update_error', 'Could not update stock notification in the database.' );
		}

		if ( 0 === $result ) {
			return new \WP_Error( 'db_update_error', 'Invalid notification ID.' );
		}
	}

	$notification->save_meta_data();

	if ( $changes ) {
		$notification->apply_changes();
	}

	return $result;
}