Get and multiply WooCommerce product formatted price to insert in Javascript

Viewed 207

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);
1 Answers

Updated - You can use the following to get the raw product price for display, to multiply it by 4 and format it in the right way:

add_action( 'woocommerce_after_add_to_cart_form', 'wc_pb_modal', 1 );
function wc_pb_modal() {
    global $product;
    
    if ( ! is_a($product, 'WC_Product') ) {
        $product = wc_get_product(get_the_ID());
    }

    if ( $product->is_type('variable') ) {
        return; // Exit if a variable product
    }

    $formatted_price = strip_tags( wc_price( wc_get_price_to_display( $product ) * 4 ) );
    $my_api_key      = '[MY API KEY]'; // Here set your api key
    
    echo "<script id='pb_prequalify' type='text/javascript' src='https://app.paybright.com/dist/sdk.js?public_key=$my_api_key&financedamount=$formatted_price'></script>
    <script>
    pb_prequalify_init({
        triggerElement: 'link',  
        triggerText: 'Credit available with <br> Prequalify now',
        lang: 'en',
        hideTriggerLeadText: true,
        noFiguresInModal: true,
    });
    </script>
    <div id='paybright-widget-container'></div>";
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

Related