Title: Custom pricing (from shortcode)
Last modified: September 11, 2020

---

# Custom pricing (from shortcode)

 *  Resolved [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/)
 * Hello,
 * Nice plugin!
 * How might I go about dynamically updating the pricing of a form from a variable
   on a page (i.e. in a shortcode or php)?
 * I see from this post ([https://wordpress.org/support/topic/dynamic-pricing-15/](https://wordpress.org/support/topic/dynamic-pricing-15/))
   there is a way to adjust pricing dynamically through filters, which works well,
   however, it seems to trigger before the page renders so I cannot pass any page
   variables to the plugin this way.
 * Any advice or recommendations much appreciated!
 * Cheers,
 * Wilson

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/custom-pricing-from-shortcode/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/custom-pricing-from-shortcode/page/2/?output_format=md)

 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13394520)
 * Hello [@wwwilson](https://wordpress.org/support/users/wwwilson/)
 * The filter will run as the shortcode is parsed, so anything defined up to that
   point should be able to be used in that filter.
 * Can you further explain the “page variable” you need to access?
 *  Thread Starter [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13396817)
 * Hey [@spencerfinnell](https://wordpress.org/support/users/spencerfinnell/),
 * Thanks for your reply.
 * My case is that I have multiple post types acting as product. Each product page
   with a number of variations of that product. Every product and variation requires
   a purchase button/form.
 * A variable on a product page is $price which would differ based on the product
   and variation.
 * Instead of creating a form for every product and variation in the simpay admin,
   I am wondering if there is a quick way to feed data directly to the form. In 
   effect, creating payment forms on the fly and bypassing the need to create a 
   unique form in the simpay admin for every instance.
 * For example’s sake I’ve used shortcodes with fake variables to try to illustrate
   the point: So on a product page I might include 3 variations of a given product.
 *     ```
       [simpay product_id="123" variation="x" price="10"]
   
       [simpay product_id="123" variation="y" price="20"]
   
       [simpay product_id="123" variation="z" price="30"]
       ```
   
 * Each shortcode would generate a payment form with corresponding variation/price.
 * Do you know if this be achieved somehow? Let me know if I can be more clear.
 * Cheers,
 * Wilson
 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13404193)
 * Hello [@wwwilson](https://wordpress.org/support/users/wwwilson/),
 * You should be able to use the following two code snippets:
 * [https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-amount.php](https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-amount.php)
   
   [https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-item-description.php](https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-item-description.php)
 * You could then modify them to something like:
 *     ```
       function simpay_custom_form_157_amount() {
         // Retrieve the amount from the current custom post type object.
         $amount = get_post_meta( get_the_ID(), '_amount', true );
         return $amount;
       }
       ```
   
 * Similarly with the “Item Description” snippet you could use that to hold the 
   Product ID + Variation in the Stripe record.
 * I hope that helps!
 *  Thread Starter [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13407288)
 * Hey [@spencerfinnell](https://wordpress.org/support/users/spencerfinnell/),
 * This works a dream, thanks!
 * My only bother was to get it to work for multiple shortcodes displayed on the
   same page (i.e. a payment button for each variation).
 * One workaround I found was to create a maximum number of forms to variations 
   expected on each page and use your stated method to apply a variation to each
   form available. It’s a little clunky but works.
 * I don’t suppose there is a more elegant solution? No worries if not.
 * Cheers,
 * Wilson
 *  Thread Starter [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13409477)
 * Follow up after testing with pulled post ids i.e. from get_the_ID() (apologies
   my previous reply was after manually coding the post ids)*
 * It appears like the simpay_form_#_amount filter can’t get the post ID of the 
   current post.
 * For example if I place this in my theme’s functions.php file:
 *     ```
       function simpay_price($amount){
           $id = get_the_ID();
           return $id;
       }
       add_filter( 'simpay_form_203_amount', 'simpay_price' );
       ```
   
 * And add the form to a post.
 * `echo do_shortcode('[simpay id="203"]')`
 * I would expect to see the price as the post_id however the result produced by
   the form is
 * > This value must be greater than or equal to 1.
 *  which suggests no value was returned by the filter (as the post ID was not found).
 * I am wondering if this is because the post needs to render in order to get the
   post id but in doing so the shortcode will also render (and therefore is not 
   able to be filtered by post id)?
 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13410561)
 * [@wwwilson](https://wordpress.org/support/users/wwwilson/) The return value of
   your price function needs to be an amount. Your code example is attempting to
   return a post ID.
 * You’ll need to attach the filter via something like:
 *     ```
       function simpay_custom_prices() {
       	// Ensure we have a singular post.
       	if ( ! get_post() ) {
       		return;
       	}
   
       	$id = get_the_ID();
   
       	add_filter( 'simpay_form_' . $id . '_amount', 'simpay_price' );
       }
       add_action( 'init', 'simpay_custom_prices' );
   
       function simpay_price( $amount ){
       	// Find the stored amount for the current post.
       	// @wwwilson Update this.
       	$id = get_the_ID();
       	$amount = get_post_meta( get_the_ID(), '_amount', true );
   
       	return $amount;
       }
       ```
   
 * That will look for a singular post on page load and dynamically create the filter
   based on the current ID.
 *  Thread Starter [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13412675)
 * Thanks [@spencerfinnell](https://wordpress.org/support/users/spencerfinnell/)
 * WP doesn’t get any post data during ‘init’ so I have adapted your script to get
   the id from ‘url_to_postid’ instead. I have hit a snag that the ‘simpay_form_#
   _amount’ filter doesn’t seem to accept post ids in any shape or form.
 * You can see from this code that simply using a post id prevents the filter from
   working correctly.
 *     ```
       add_filter( 'simpay_form_203_amount', function ( $amount ){
   
           // 1.a. Get post ID from URL
           $current_url = "//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
           $simpay_post_id = url_to_postid($current_url);
   
           // 1.b.
           $simpay_field = get_field('price',$simpay_post_id);
   
           // 2.a. Get post ID manually (for example's sake)
           $manual_id = 123; // The exact same id as $simpay_post_id
   
           // 2.b.
           $manual_field = get_field('price',$manual_id);
   
           // Success: Both $simpay_field and $manual_field var_dump the same (int) value here, however...
   
           return $simpay_field; // 1. returns nothing
           return $manual_field; // 2. returns field successfully
   
       } );
       ```
   
 * Any ideas why I can’t use a post ID in this filter?
 * Cheers,
 * Wilson
 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13420460)
 * [@wwwilson](https://wordpress.org/support/users/wwwilson/)
 *     ```
       $simpay_post_id = url_to_postid($current_url);
       ```
   
 * That will retrieve the current post/page ID, not the Simple Pay Form ID. If you
   are using the shortcode in page content you will have access to that information
   inside of the filter:
 *     ```
       add_action( 'simpay_form_203_amount', function( $amount ) {
       	var_dump( get_the_ID() ); // This is not 203. It is the ID of the current page.
       	return $amount;
       } );
       ```
   
 *  Thread Starter [wwwilson](https://wordpress.org/support/users/wwwilson/)
 * (@wwwilson)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13421519)
 * Hey [@spencerfinnell](https://wordpress.org/support/users/spencerfinnell/) ,
 * Ah, thanks for correcting. I didn’t clarify that I was trying to get the post/
   page id of the current page and not the simpay form.
 * Nevertheless I’m still stumped with the same problem for some reason. The var_dump
   turns up as the page id, however any post meta I then pull using the same page
   id returns null or simpay message
 * > This value must be greater than or equal to 1.
 * Can you confirm you can get a result from the filter when returning a custom 
   field value using the current page’s id and this filter?
 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13433866)
 * Hello [@wwwilson](https://wordpress.org/support/users/wwwilson/), sorry for the
   back and forth. You are right, things do act a little differently in our Lite
   version than what I was testing with on Pro. My mistake.
 * Here is what you should need to accomplish your goal:
 *     ```
       wp_posts
       +----+------------------+-------------------+
       | ID |   post_content   |    post_title     |
       +----+------------------+-------------------+
       | 50 | [simpay id="35"] | Post with form... |
       +----+------------------+-------------------+
       ```
   
 *     ```
       wp_postmeta
   
       +---------+---------+----------+------------+
       | meta_id | post_id | meta_key | meta_value |
       +---------+---------+----------+------------+
       |     123 |      50 | _amount  |       7.00 |
       +---------+---------+----------+------------+
       ```
   
 * And the following custom code snippet:
 *     ```
       add_filter( 'simpay_form_35_amount', function() {
       	$post_id = isset( $_POST['form_values']['post_id'] )
       		? sanitize_text_field( $_POST['form_values']['post_id'] )
       		: 0;
   
       	return get_post_meta( $post_id, '_amount', true );
       } );
   
       add_action( 'simpay_form_35_before_form_bottom', function() {
       	echo '<input type="hidden" name="post_id" value="' . esc_attr( get_the_ID() ) . '" />';
       } );
       ```
   
 *  [Adam Lea](https://wordpress.org/support/users/adamjlea/)
 * (@adamjlea)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13449796)
 * It’s been a number of days since we’ve back from you. If you need to further 
   help please feel free to get in touch again 🙂
 * Regards,
 *  [danielcambria](https://wordpress.org/support/users/danielcambria/)
 * (@danielcambria)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13500582)
 * Hi!
    Trying to use shortcode like [simpay id=”8735″ amount=”100″], usint [https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-amount.php](https://github.com/wpsimplepay/wp-simple-pay-snippet-library/blob/master/plugins/custom-amount.php)
   but getting
 * > The Checkout Session’s total amount due cannot be zero in `payment` mode. Please
   > see [https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts](https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts)
 * . Using Simple Pay Lite too.
    Thank you!
 *  [mbo123](https://wordpress.org/support/users/mbo123/)
 * (@mbo123)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13941517)
 * I am using WP Simple Pay plugin to handle payment in my wordpress website. According
   to this post I can dynamically change amount with this kind of code
 *     ```
       add_filter( 'simpay_form_4021_amount', 'simpay_custom_amount' );
       function simpay_custom_amount() {
           return 4000;
       }
       ```
   
 * And it work fine like this. But I want to change the amount according to some
   data that is in my database that come from a Ninja Form. Here is my code that
   doesn’t work, the amount value is always 1.
 *     ```
       add_filter( 'simpay_form_4021_amount', 'simpay_custom_amount' );
       function simpay_custom_amount() {
           $price = 1;
           $submissions = Ninja_Forms()->form( 10 )->get_subs();
           error_log("filter_simpay");
           if ( is_array( $submissions ) && count( $submissions ) > 0 ) {
               foreach($submissions as $submission) {
                   $sub_values = $submission->get_field_values();
                   if ($sub_values['_seq_num'] == 100) {
                       $price = $sub_values['price'];
                       error_log("-----------> price 1 is ".$price);
                   }
               }
           }
           error_log("-----------> price 2 is ". ((int) $price));
           return (int) $price;
       }
       ```
   
 * Can you help me fix this please? The logs display the good amount, but the amount
   in Stripe is always 1.
 *  [Spencer Finnell](https://wordpress.org/support/users/spencerfinnell/)
 * (@spencerfinnell)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13943038)
 * Hello [@mbo123](https://wordpress.org/support/users/mbo123/)
 * In the future, please create a separate forum topic to avoid previous participants
   from receiving unrelated notifications.
 * If you call your `simpay_custom_amount()` function directly, does it return the
   correct value? I am not familiar enough with Ninja Forms to determine if the 
   logic you have for finding the price is correct — you may need to follow up with
   the plugin author. WP Simple Pay will use the final return value to determine
   the amount used by Stripe Checkout.
 *  [mbo123](https://wordpress.org/support/users/mbo123/)
 * (@mbo123)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/#post-13943066)
 * Hi [@spencerfinnell](https://wordpress.org/support/users/spencerfinnell/),
 * Thank you for your answer and sorry for not opening a new topic for my issue.
   I didn’t want to open a new topic in order to avoid to overload topic list.
    
   Like I said “The logs display the good amount” so the value I get from Ninja 
   form is good I am 100% sure on this point. My issue is nearly exactly the same
   than the one of [@wwwilson](https://wordpress.org/support/users/wwwilson/) =>
   with “manual data” it works fine, but it doesn’t work well when I use data that
   come from the database (Ninja Form). I forgot to specify that I am using the 
   Lite version of the plugin.
    -  This reply was modified 5 years, 4 months ago by [mbo123](https://wordpress.org/support/users/mbo123/).
    -  This reply was modified 5 years, 4 months ago by [mbo123](https://wordpress.org/support/users/mbo123/).

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/custom-pricing-from-shortcode/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/custom-pricing-from-shortcode/page/2/?output_format=md)

The topic ‘Custom pricing (from shortcode)’ is closed to new replies.

 * ![](https://ps.w.org/stripe/assets/icon-256x256.png?rev=2784844)
 * [Stripe Payment Forms by WP Simple Pay - Accept Credit Card Payments + Subscriptions with Stripe](https://wordpress.org/plugins/stripe/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/stripe/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/stripe/)
 * [Active Topics](https://wordpress.org/support/plugin/stripe/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/stripe/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/stripe/reviews/)

 * 18 replies
 * 5 participants
 * Last reply from: [mbo123](https://wordpress.org/support/users/mbo123/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/custom-pricing-from-shortcode/page/2/#post-13943680)
 * Status: resolved