WC_Cart_Fees::add_fee()publicWC 3.2.0

Add a fee. Fee IDs must be unique.

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

Хуков нет.

Возвращает

Объект. Either a fee object if added, or a WP_Error if it failed.

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

$WC_Cart_Fees = new WC_Cart_Fees();
$WC_Cart_Fees->add_fee( $args );
$args(массив)
Array of fee properties.
По умолчанию: array()

Список изменений

С версии 3.2.0 Введена.

Код WC_Cart_Fees::add_fee() WC 9.4.2

public function add_fee( $args = array() ) {
	$fee_props            = (object) wp_parse_args( $args, $this->default_fee_props );
	$fee_props->name      = $fee_props->name ? $fee_props->name : __( 'Fee', 'woocommerce' );
	$fee_props->tax_class = in_array( $fee_props->tax_class, array_merge( WC_Tax::get_tax_classes(), WC_Tax::get_tax_class_slugs() ), true ) ? $fee_props->tax_class : '';
	$fee_props->taxable   = wc_string_to_bool( $fee_props->taxable );
	$fee_props->amount    = wc_format_decimal( $fee_props->amount );

	if ( empty( $fee_props->id ) ) {
		$fee_props->id = $this->generate_id( $fee_props );
	}

	if ( array_key_exists( $fee_props->id, $this->fees ) ) {
		return new WP_Error( 'fee_exists', __( 'Fee has already been added.', 'woocommerce' ) );
	}

	$this->fees[ $fee_props->id ] = $fee_props;

	return $this->fees[ $fee_props->id ];
}