Home / Admin / WP Simple Pay: Set a Per-Form Custom Amount Programmatically
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Set a Per-Form Custom Amount Programmatically

Adjust a single payment form's custom amount value programatically.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/7oq8llow/
 * 
 * @param \SimplePay\Core\PaymentForm\PriceOption[] $price_options Payment form price options.
 * @param \SimplePay\Core\Abstracts\Form $form Payment form.
 * @return \SimplePay\Core\PaymentForm\PriceOption[]
 */
add_filter(
	'simpay_get_payment_form_price_options',
	function( $price_options, $form ) {
		// BEGIN UPDATES.
		$form_id     = 150; // Payment form ID.
		$currency    = 'usd'; // Currency.
		$unit_amount = 2000; // $20.00 in cents.
		// END UPDATES.
		// STOP EDITING.
		if ( $form_id !== $form->id ) {
			return $price_options;
		}
		$custom_amount = new \SimplePay\Core\PaymentForm\PriceOption(
			array(
				'id'              => 'simpay_custom_amount',
				'currency'        => $currency,
				'unit_amount'     => $unit_amount,
				'unit_amount_min' => $unit_amount,
				'default'         => true,
			),
			$form
		);
		return array( $custom_amount );
	},
	10,
	2
);

Comments

Add a Comment