John Davidson

php - Get and multiply WooCommerce product formatted price to insert in Javascript

0 comments
Message:


I am Using this custom Function to show a modal pop up that tell client the monthly price based on product price. Currently it works well, the issue is that we need the amount to be multiplied by 4. This way when it calculates it can display the correct price per month.
--Ex: if price is $500.00 for set of 4 (tires) --> then monthly payments are based on 500 not 125 which is confusing customers.


Here is my code..need '_price'to be multiplied by *4


Can someone please help with solution.


function pb_modal() {
$pb_product_price = get_post_meta( get_the_ID(), '_price', true);

$pb_product_format = number_format((float)$pb_product_price, 2, '.', '');
echo "<script id='pb_prequalify' type='text/javascript' src='https://app.paybright.com/dist/sdk.js?public_key=[MY API KEY]&financedamount=$$pb_product_format'></script>";
echo "<script>
pb_prequalify_init({
triggerElement: 'link',
triggerText: 'Credit available with <br> Prequalify now',
lang: 'en',
hideTriggerLeadText: true,
noFiguresInModal: true,
});
</script>
";
echo "<div id='paybright-widget-container'></div>";
}
add_action( 'woocommerce_after_add_to_cart_form', 'pb_modal', 1);


My Answer:
Here is a custom function that you can use to get the WooCommerce product price, multiply it by 4, and insert it into your JavaScript code:

php
function get_product_price() {
global $product;

// Get the product price
$price = $product->get_price();

// Multiply the price by 4
$monthly_price = $price * 4;

// Format the price
$formatted_price = wc_price($monthly_price);

// Return the formatted price
return $formatted_price;
}


You can then call this function in your JavaScript code like this:

javascript
var monthlyPrice = '';

// Use the monthlyPrice variable in your modal pop up code


This will display the correct price per month based on the product price multiplied by 4.

Rate this post

4 of 5 based on 1630 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us