Automattic\WooCommerce\Internal\StockNotifications\Frontend
MyAccountEndpoint::cancel
Flip a pending or active notification to cancelled on the customer's behalf.
Method of the class: MyAccountEndpoint{}
No Hooks.
Returns
string|\WP_Error. Success notice text, or the error to show instead.
Usage
// private - for code of main (parent) class only $result = $this->cancel( $notification );
- $notification(Notification) (required)
- The notification, already checked to belong to the current user.
MyAccountEndpoint::cancel() MyAccountEndpoint::cancel code WC 11.2.1
private function cancel( Notification $notification ) {
if ( ! self::is_cancellable( $notification ) ) {
return new \WP_Error( 'wc_bis_cancel_not_cancellable', __( 'That back in stock notification has already been cancelled.', 'woocommerce' ) );
}
$notification->set_status( NotificationStatus::CANCELLED );
$notification->set_cancellation_source( NotificationCancellationSource::USER );
$notification->set_date_cancelled( time() );
$notification->save();
// `WC_Data_Store::update()` drops the data store's return value, so a failed write
// reaches us as a successful save. Read the row back to confirm it really changed.
$saved = Factory::get_notification( $notification->get_id() );
if ( ! $saved instanceof Notification || NotificationStatus::CANCELLED !== $saved->get_status() ) {
return new \WP_Error( 'wc_bis_cancel_failed', __( 'We could not cancel that back in stock notification. Please try again.', 'woocommerce' ) );
}
$product_name = $notification->get_product_name();
if ( '' === $product_name ) {
return __( 'Back in stock notification cancelled.', 'woocommerce' );
}
/* translators: %s: product name */
return sprintf( __( 'Back in stock notification for "%s" cancelled.', 'woocommerce' ), $product_name );
}