Home › Forums › Stripe Payments Plugin › include product price in short code
- This topic has 3 replies, 3 voices, and was last updated 5 years, 6 months ago by alexanderfoxc.
-
AuthorPosts
-
May 3, 2019 at 8:11 pm #2064alecmyersParticipant
HI – basically I have one product (teaching) whose price varies by length of time. (I already have wordpress setup so each user has their own private protected page) and I give each user a button to pay for their last training session.
So I’d like to include the price in the short code on each user’s page, like this:
[asp_product id=”196″ price=”200″]
So I added into function shortcode_asp_product( $atts )
one line, at approx line 257:
after: $price = get_post_meta( $id, ‘asp_product_price’, true );
this: $price = $atts[‘price’]; //ARM20190502I notice there’s a filter that alters the price too, in about the same place, but I’m not using it right now. I just note that it’s there:
$price_arr = apply_filters( ‘asp_modify_price_currency_shipping’, $price_arr );Ok, so far so good. The right prices shows up in the button, and in the payment popup. It’s correct in the payment <form> code, it’s right everywhere. But when the transaction is completed the (existing, incorrect) product price from the database is what has been charged.
Normally I’m pretty good about working things through and debugging my changes, but I cannot work out *how* or *where* the stored product price is overwriting the price I set in the shortcode that’s being propagated all the way through and shown to the customer. What I see in the Stripe payment dashboard page is like this:
Amount:$1,1287 (succeeded) 1xCAD$226.00 – ch_1E……….
The product price was set in the db at $999 and the shortcode had price=200, so with tax, the amount actually charged wasn’t what I wanted but the description was.
And the ‘order’ in the WP database also shows $1,1287 not $226. (that is, $999+tax instead of $200+tax)
Can you help me by telling me where/how/why the $price in shortcode_asp_product is being overwritten?
May 4, 2019 at 2:30 am #2066alecmyersParticipantOK, I figured out what’s going on. You do all the arithmetic twice: once in class-shortcode-asp.php to create the button and form, and then do it all again (reading the price from the database a second time) in process_ipn.php.
I suppose that stops anyone spoofing their own price in the <form>.
So to get the option of [asp_product id=”196″ price=”200″] to work I:
a) send the custom price into get_button_code_new_method( $data ) by adding
685: if (isset($atts[‘price’])) { //ARM20190503
$data[‘customPrice’] = $atts[‘price’];
}and then
b) spoof the “user set amount” feature by adding a (hidden) field with name ‘stripeAmount’
802: if (isset($data[‘customPrice’])) { //ARM20190503
$data[‘amount’] = $data[‘customPrice’];
error_log($data[‘price’]);
$output .= “<input type=’hidden’ name=’stripeAmount’ value='” . $data[‘customPrice’] . “‘ >”;
}Now when process_ipn.php is run the field with that name triggers the //Custom amount conditional on 216, as if the customer had entered the amount themselves.
Can you think of a neater way to do this? It’s a bit ‘hacky’.
May 4, 2019 at 4:15 am #2068AdminKeymasterThere are various security checks in place for a product checkout. So manipulating price like that is not the best option in my opinion.
A better option is probably to create multiple different products in our plugin with the different price points. You can create as many products as you want inside the plugin (even though they may all refer to one same product in the real world). Then you can pull the product you need and allow the customer to do the transaction.
Alternatively, the following shortcode to dynamically create a Stripe button may work:
Creating a Payment Button by Dynamically Adding Item Details in the Shortcode
May 4, 2019 at 8:43 am #2069alexanderfoxcParticipantYou can also achieve that using variations.
E.g. you set some base price for your product (let’s say $100).
Then add variation for the duration:
1 hour + $50
2 hours + $100
10 hours + $800 (some kind of a discount)This way your customer can choose how many hours he\she wants to purchase.
-
AuthorPosts
- You must be logged in to reply to this topic.