WC_Helper::deactivate_helper_subscription()public staticWC 1.0

Deactivate a product subscription.

Method of the class: WC_Helper{}

Return

true|false. True if deactivated, false otherwise.

Usage

$result = WC_Helper::deactivate_helper_subscription( $product_key );
$product_key(string) (required)
Subscription product key.

WC_Helper::deactivate_helper_subscription() code WC 9.0.1

public static function deactivate_helper_subscription( $product_key ) {
	$subscription = self::get_subscription( $product_key );
	if ( ! $subscription ) {
		throw new Exception( __( 'Subscription not found', 'woocommerce' ) );
	}
	$product_id = $subscription['product_id'];

	$deactivation_response = WC_Helper_API::post(
		'deactivate',
		array(
			'authenticated' => true,
			'body'          => wp_json_encode(
				array(
					'product_key' => $product_key,
				)
			),
		)
	);

	$code        = wp_remote_retrieve_response_code( $deactivation_response );
	$deactivated = 200 === $code;

	if ( $deactivated ) {
		/**
		 * Fires when the Helper activates a product successfully.
		 *
		 * @param int    $product_id Product ID being deactivated.
		 * @param string $product_key Subscription product key.
		 * @param array  $deactivation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_deactivate_success', $product_id, $product_key, $deactivation_response );
	} else {
		self::log( sprintf( 'Deactivate API call returned a non-200 response code (%d)', $code ) );

		/**
		 * Fires when the Helper fails to activate a product.
		 *
		 * @param int    $product_id Product ID being deactivated.
		 * @param string $product_key Subscription product key.
		 * @param array  $deactivation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_deactivate_error', $product_id, $product_key, $deactivation_response );

		$body = json_decode( wp_remote_retrieve_body( $deactivation_response ), true );
		throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) );
	}

	self::_flush_subscriptions_cache();

	return $deactivated;
}