Automattic\WooCommerce\Internal\StockNotifications\Frontend

MyAccountEndpoint::cancel │ private │ WC 1.0

Flip a pending or active notification to cancelled on the customer's behalf.

Метод класса: MyAccountEndpoint{}

Хуков нет.

Возвращает

string|\WP_Error. Success notice text, or the error to show instead.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->cancel( $notification );
$notification(Notification) (обязательный)
The notification, already checked to belong to the current user.

Код MyAccountEndpoint::cancel() WC 11.2.2

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