WC_Helper::activate_helper_subscription()public 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 9.4.2

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.
	$activation_response = WC_Helper_API::post(
		'activate',
		array(
			'authenticated' => true,
			'body'          => wp_json_encode(
				array(
					'product_key' => $product_key,
				)
			),
		)
	);

	$activated = wp_remote_retrieve_response_code( $activation_response ) === 200;
	$body      = json_decode( wp_remote_retrieve_body( $activation_response ), true );

	if ( ! $activated && ! empty( $body['code'] ) && 'already_connected' === $body['code'] ) {
		$activated = true;
	}

	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 );
		throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) );
	}

	// Attempt to activate this plugin.
	$local = self::_get_local_from_product_id( $product_id );
	if ( $local && 'plugin' == $local['_type'] && current_user_can( 'activate_plugins' ) && ! is_plugin_active( $local['_filename'] ) ) {
		activate_plugin( $local['_filename'] );
	}

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

	return $activated;
}