WC_Coupon::__construct()publicWC 1.0

Coupon constructor. Loads coupon data.

Method of the class: WC_Coupon{}

Hooks from the method

Return

null. Nothing (null).

Usage

$WC_Coupon = new WC_Coupon();
$WC_Coupon->__construct( $data );
$data(mixed)
Coupon data, object, ID or code.
Default: ''

WC_Coupon::__construct() code WC 8.6.1

public function __construct( $data = '' ) {
	parent::__construct( $data );

	// If we already have a coupon object, read it again.
	if ( $data instanceof WC_Coupon ) {
		$this->set_id( absint( $data->get_id() ) );
		$this->read_object_from_database();
		return;
	}

	// This filter allows custom coupon objects to be created on the fly.
	$coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $data, $this );

	if ( $coupon ) {
		$this->read_manual_coupon( $data, $coupon );
		return;
	}

	// Try to load coupon using ID or code.
	if ( is_int( $data ) && 'shop_coupon' === get_post_type( $data ) ) {
		$this->set_id( $data );
	} elseif ( is_string( $data ) && ! StringUtil::is_null_or_whitespace( $data ) ) {
		$id = wc_get_coupon_id_by_code( $data );
		// Need to support numeric strings for backwards compatibility.
		if ( ! $id && 'shop_coupon' === get_post_type( $data ) ) {
			$this->set_id( $data );
		} else {
			$this->set_id( $id );
			$this->set_code( $data );
		}
	} else {
		$this->set_object_read( true );
	}

	$this->read_object_from_database();
}