WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Платформа для конвертации и монетизации трафика
функция не описана

Automattic\WooCommerce\Blocks\StoreApi\Utilities

CartController::apply_coupon() public WC 1.0

Based on the core cart class but returns errors rather than rendering notices directly.

{} Это метод класса: CartController{}

Возвращает

Null. Ничего.

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

$CartController = new CartController();
$CartController->apply_coupon( $coupon_code );
$coupon_code(строка) (обязательный)
Coupon code.

Код CartController::apply_coupon() WC 4.7.1

<?php
public function apply_coupon( $coupon_code ) {
	$cart            = $this->get_cart_instance();
	$applied_coupons = $this->get_cart_coupons();
	$coupon          = new \WC_Coupon( $coupon_code );

	if ( $coupon->get_code() !== $coupon_code ) {
		throw new RouteException(
			'woocommerce_rest_cart_coupon_error',
			sprintf(
				/* Translators: %s coupon code */
				__( '"%s" is an invalid coupon code.', 'woocommerce' ),
				esc_html( $coupon_code )
			),
			400
		);
	}

	if ( $this->has_coupon( $coupon_code ) ) {
		throw new RouteException(
			'woocommerce_rest_cart_coupon_error',
			sprintf(
				/* Translators: %s coupon code */
				__( 'Coupon code "%s" has already been applied.', 'woocommerce' ),
				esc_html( $coupon_code )
			),
			400
		);
	}

	if ( ! $coupon->is_valid() ) {
		throw new RouteException(
			'woocommerce_rest_cart_coupon_error',
			wp_strip_all_tags( $coupon->get_error_message() ),
			400
		);
	}

	// Prevents new coupons being added if individual use coupons are already in the cart.
	$individual_use_coupons = $this->get_cart_coupons(
		function( $code ) {
			$coupon = new \WC_Coupon( $code );
			return $coupon->get_individual_use();
		}
	);

	foreach ( $individual_use_coupons as $code ) {
		$individual_use_coupon = new \WC_Coupon( $code );

		if ( false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $coupon, $individual_use_coupon, $applied_coupons ) ) {
			throw new RouteException(
				'woocommerce_rest_cart_coupon_error',
				sprintf(
					/* translators: %s: coupon code */
					__( '"%s" has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ),
					$code
				),
				400
			);
		}
	}

	if ( $coupon->get_individual_use() ) {
		$coupons_to_remove = array_diff( $applied_coupons, apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $coupon, $applied_coupons ) );

		foreach ( $coupons_to_remove as $code ) {
			$cart->remove_coupon( $code );
		}

		$applied_coupons = array_diff( $applied_coupons, $coupons_to_remove );
	}

	$applied_coupons[] = $coupon_code;
	$cart->set_applied_coupons( $applied_coupons );

	do_action( 'woocommerce_applied_coupon', $coupon_code );
}