Automattic\WooCommerce\Internal\DataStores\StockNotifications
StockNotificationsDataStore::update
Update a stock notification.
Method of the class: StockNotificationsDataStore{}
Hooks from the method
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() StockNotificationsDataStore::update code WC 11.3.0
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;
$status_from = $notification->get_data()['status'];
$has_prop_changes = (bool) 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 ( $has_prop_changes || $notification->has_meta_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.' );
}
}
$notification->save_meta_data();
$notification->apply_changes();
$status_to = $notification->get_status( 'edit' );
/**
* Action: woocommerce_update_stock_notification
*
* Fires after a stock notification is updated, from any code path, whether or not anything changed.
* Also fires inside the back-in-stock send batch, so defer remote calls to Action Scheduler.
*
* @since 11.3.0
*
* @param int $notification_id The notification ID.
* @param Notification $notification The notification.
*/
do_action( 'woocommerce_update_stock_notification', $notification->get_id(), $notification );
if ( array_key_exists( 'status', $changes ) && $status_from !== $status_to ) {
/**
* Action: woocommerce_stock_notification_status_changed
*
* Fires after a stock notification's status is persisted with a different value, from any code path.
*
* @since 11.3.0
*
* @param int $notification_id The notification ID.
* @param string $status_from The previous status.
* @param string $status_to The new status.
* @param Notification $notification The notification.
*/
do_action( 'woocommerce_stock_notification_status_changed', $notification->get_id(), $status_from, $status_to, $notification );
}
return $result;
}