How to dynamically change the price of paypal smartbutton according to the PHP cart value?

AlbiKiappa
New Community Member
 

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?

Login to Me Too
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.