• Resolved ptom98

    (@ptom98)


    I’ve tried editing a piece of code posted on your github account, but I get an error message Invalid integer: false.
    The original code is here https://github.com/awesomemotive/wp-simple-pay-snippet-library/blob/master/plugins/custom-amount.php which works fine. It’s when I amend this code to get the value from ACF fields that the issue occurs, the edited code is below;

    add_filter(
    	'simpay_get_payment_form_price_options',
    	function( $price_options, $form ) {
    		global $post;
    		
    		$amount = 0.00;
    		
    		if (have_rows('items', $post->ID)) {
    			while (have_rows('items', $post->ID)) {
    				the_row();
    				$qty+=get_sub_field('quantity');
    				$amount+=get_sub_field('price')*get_sub_field('quantity');
    			}
    		}
    		
    		$amount = $amount * 100;
    
    		$form_id     = 1111;
    		$unit_amount = intval($amount);
    		$currency    = 'gbp';
    
    		$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
    );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Spencer Finnell

    (@spencerfinnell)

    Hello @ptom98,

    Using global $post won’t work in this situation because the filter is not always run in an environment where the post is accessible. You should be able to use the second argument of the callback function, $form, to reference the ID of the custom post type though: $form->ID.

    Hopefully that helps!

    Thread Starter ptom98

    (@ptom98)

    The $post is reference to page that i copy the shortcode on rather the id of the payment form.

    This bit seems to work fine, if i output the value of $amount in the code, the expected value is correct and appears as an int, but when using as $unit_amount, i get the error specified.

    Plugin Author Spencer Finnell

    (@spencerfinnell)

    Hello @ptom98,

    When the payment form is submitted through the WordPress REST API the current page context is no longer available — the next time that function is called it won’t be able to retrieve the value from ACF. You need to store a reference to the page ID elsewhere (like in wp_options), or put the IDs in directly:

    
    switch ( $form->ID ) {
      // Payment form 123 is embedded on page ID 456.
      case 123:
        $post_id = 456;
        break;
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Unable to change price via PHP’ is closed to new replies.