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

WC_Cart::apply_coupon() public WC 1.0

Applies a coupon code passed to the method.

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

Возвращает

true/false. True if the coupon is applied, false if it does not exist or cannot be applied.

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

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

Код WC_Cart::apply_coupon() WC 4.5.2

<?php
public function apply_coupon( $coupon_code ) {
	// Coupons are globally disabled.
	if ( ! wc_coupons_enabled() ) {
		return false;
	}

	// Sanitize coupon code.
	$coupon_code = wc_format_coupon_code( $coupon_code );

	// Get the coupon.
	$the_coupon = new WC_Coupon( $coupon_code );

	// Prevent adding coupons by post ID.
	if ( $the_coupon->get_code() !== $coupon_code ) {
		$the_coupon->set_code( $coupon_code );
		$the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_EXIST );
		return false;
	}

	// Check it can be used with cart.
	if ( ! $the_coupon->is_valid() ) {
		wc_add_notice( $the_coupon->get_error_message(), 'error' );
		return false;
	}

	// Check if applied.
	if ( $this->has_discount( $coupon_code ) ) {
		$the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED );
		return false;
	}

	// If its individual use then remove other coupons.
	if ( $the_coupon->get_individual_use() ) {
		$coupons_to_keep = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons );

		foreach ( $this->applied_coupons as $applied_coupon ) {
			$keep_key = array_search( $applied_coupon, $coupons_to_keep, true );
			if ( false === $keep_key ) {
				$this->remove_coupon( $applied_coupon );
			} else {
				unset( $coupons_to_keep[ $keep_key ] );
			}
		}

		if ( ! empty( $coupons_to_keep ) ) {
			$this->applied_coupons += $coupons_to_keep;
		}
	}

	// Check to see if an individual use coupon is set.
	if ( $this->applied_coupons ) {
		foreach ( $this->applied_coupons as $code ) {
			$coupon = new WC_Coupon( $code );

			if ( $coupon->get_individual_use() && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons ) ) {

				// Reject new coupon.
				$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );

				return false;
			}
		}
	}

	$this->applied_coupons[] = $coupon_code;

	// Choose free shipping.
	if ( $the_coupon->get_free_shipping() ) {
		$packages                = WC()->shipping()->get_packages();
		$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

		foreach ( $packages as $i => $package ) {
			$chosen_shipping_methods[ $i ] = 'free_shipping';
		}

		WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
	}

	$the_coupon->add_coupon_message( WC_Coupon::WC_COUPON_SUCCESS );

	do_action( 'woocommerce_applied_coupon', $coupon_code );

	return true;
}