I am using a plugin to display a widget that calculates the monthly installments of a product when buying on credit. The plugin uses $product->get_price()
and $variation->get_price()
to fill the data-amount=
parameter to auto calculate the installment based on the product price. However it only seems to work correctly on simple products with a static price, on variable products it only calculates the first variation and doesn’t change dynamically when selecting different variables. Is there a way to update this code so the plugin would calculate variable products correctly for each variation?
Here is the code I think is responsible for executing this calculation:
function mobicred_widget_display() {
global $post;
if( function_exists("wc_get_product") ) {
$product = wc_get_product($post->ID);
}
else {
$product = new WC_Product($post->ID);
}
$price = $product->get_price();
$thePlugin = new WC_Mobicred_Instalment();
$widget_code = '<script src="https://mobicred.co.za/plugins/instalment.js"></script>';
$widget_code .= '<div id="instalmentCalc"';
$widget_code .= 'data-amount="';
$widget_code .= $price;
$widget_code .= '"></div>';
echo $widget_code;
}
add_action('woocommerce_single_product_summary','mobicred_widget_display',15);
function mobicred_variation_widget_display( $price, $variation ) {
$widget_code = $price;
$amount = $variation->get_price();
$thePlugin = new WC_Mobicred_Instalment();
$widget_code .= '<script src="includes/instalment.js"></script>';
$widget_code .= '<div id="instalmentCalc"';
$widget_code .= 'data-amount="';
$widget_code .= $amount;
$widget_code .= '"></div>';
echo $widget_code;
}
add_filter( 'woocommerce_variation_price_html', 'mobicred_variation_widget_display', 10, 2);
add_filter( 'woocommerce_variation_sale_price_html', 'mobicred_variation_widget_display', 10, 2);