WC_Helper::activate_helper_subscriptionpublic staticWC 1.0

Activate helper subscription.

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

Возвращает

true|false. True if activated, false otherwise.

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

$result = WC_Helper::activate_helper_subscription( $product_key );
$product_key(строка) (обязательный)
Subscription product key.

Код WC_Helper::activate_helper_subscription() WC 10.5.0

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

	// Activate subscription.
	list( $activation_response, $activated, $body ) = self::wccom_activate( $product_key );

	if ( $activated ) {
		/**
		 * Fires when the Helper activates a product successfully.
		 *
		 * @param int    $product_id Product ID being activated.
		 * @param string $product_key Subscription product key.
		 * @param array  $activation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response );
	} else {
		/**
		 * Fires when the Helper fails to activate a product.
		 *
		 * @param int    $product_id Product ID being activated.
		 * @param string $product_key Subscription product key.
		 * @param array  $activation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response );

		// Include HTTP status code and any extra data from the API response in the exception so callers can surface it.
		$status_code = function_exists( 'wp_remote_retrieve_response_code' ) ? (int) wp_remote_retrieve_response_code( $activation_response ) : (int) ( $body['data']['status'] ?? 400 );
		$error_data  = isset( $body['data'] ) && is_array( $body['data'] ) ? $body['data'] : array();
		throw new WC_Data_Exception(
			esc_html( $body['code'] ?? 'unknown_error' ),
			isset( $body['message'] ) ? esc_html( $body['message'] ) : esc_html__( 'Unknown error', 'woocommerce' ),
			(int) $status_code,
			function_exists( 'map_deep' ) ? map_deep( $error_data, 'esc_html' ) : array_map( 'esc_html', $error_data ),
		);
	}

	self::_flush_subscriptions_cache();
	self::_flush_updates_cache();
	self::flush_product_usage_notice_rules_cache();

	return $activated;
}