How to dynamically change the price of paypal smartbutton according to the PHP cart value?
AlbiKiappa
New Community Member
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
Oct-30-2020
03:11 AM
I am not able to assign the value of my shopping cart to the Paypal button in order for the user to be able to pay the correct amount.
The total price of the cart is stored as a variable $total_price:
<span class="metadata" id="totalprice" value="<?php echo $total_price ?>" > </span>
This is done to change it to something callable in Javascript.
Paypal's code from their developer website is as such:
<script> var num = document.getElementById("totalprice").value; // Render the PayPal button into #paypal-button-container paypal.Buttons({ // Set up the transaction createOrder: function (data, actions) { return actions.order.create({ purchase_units: [{ amount: { value: num } }] }); }, // Finalize the transaction onApprove: function (data, actions) { return actions.order.capture().then(function (details) { // Show a success message to the buyer alert('Transaction completed by ' + details.payer.name.given_name + '!'); }); } }).render('#paypal-button-container'); </script>
However by adding my variable as the value (as shown in the code above) every time I run the payment, the Paypal pop up briefly opens and then crashes.
I have also tried to create a Javascript variable containing the PHP variable and then setting value to that, but it had no effect.
Is there any way to solve this?
Labels:
Login to Me Too
- Labels:
-
Carts
-
PayPal HTML Buttons
0 REPLIES 0
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
Related Content
- How do you dynamically pass an item name through an 'add to cart' button? in Sandbox Environment
- BAD_INPUT_ERROR on _cart upload in PayPal Payments Standard
- Monthly payment from the Pay Later button in PayPal Payments Standard
- click funnels + paypal pay later messaging disappeared in Sandbox Environment
- How to make billing address optional in PayPal SmartButton credit/Debit card dialog in SDKs