Home / Admin / WP Simple Pay: Per-Form Fixed Tax Rate Percentages
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Per-Form Fixed Tax Rate Percentages

Override the returned tax rate for a given Payment Form.

Important: Tax rates with the percentage to use must first be created in WP Simple Pay > Settings > General > Taxes.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/924rp65g/
 * 
 * @param int $percentage Tax rate percentage.
 * @param \SimplePay\Pro\Taxes\TaxRate[] $tax_rates Tax Rates
 */
function simpay_find_tax_rate( $percentage, $tax_rates ) {
	return array_values(
		array_filter(
			$tax_rates,
			function ( $tax_rate ) use ( $percentage ) {
				return $tax_rate->percentage == $percentage;
			}
		)
	);
}
add_filter(
	'simpay_get_payment_form_tax_rates',
	/**
	 * Filters the tax rates for a given form.
	 * 
	 * @param \SimplePay\Pro\Taxes\TaxRate[] $tax_rates Tax Rates
	 * @param \SimplePay\Core\Abstracts\Form $form Payment Form.
	 */
	function( $tax_rates, $form ) {
		switch ( $form->id ) {
			// Return a tax rate of 10% for Payment Form 13.
			case 13:
				return simpay_find_tax_rate( 10, $tax_rates );
				break;
				
			// Return a tax rate of 12% for Payment Form 57
			case 57:
				return simpay_find_tax_rate( 12, $tax_rates );
				break;
				
			// Return no rates for all other Payment Forms.
			default:
				return array();
		}
	},
	10,
	2
);

Comments

Add a Comment