I am planing to show a new price on my WooCommerce product page for all products. This is the installment price per month. I need to show this below the normal price (variable price and simple price) something like this:
"Monthly payment" total=regular price/months (I want to have 3, 6, 8, 12, etc) I understand I would have to add it per line I have tried with this code but it is not showing up anything - I am starting with only 3 months. zero interest so it is really price/3
add_action( 'woocommerce_single_product_summary', 'show_emi', 20 );
function show_emi() {
global $product;
$id = $product->get_id();
$product = wc_get_product( $id );
$a = $product->get_price();
$b = 3;
$total = $a/$b;
echo $total;
}
Can someone please help me with the code (I am really not good with it) to display what I want with the text?
Try the following example that will display a formatted installment price per month below the product price on single product pages for products and variations of variable products:
// Custom function that gets installment formatted price string *(the default divider value is 3)*
function get_installment_price_html( $active_price, $divider = 3 ) {
return sprintf( __("installment: %s per month", "woocommerce"), wc_price( get_installment_price( $active_price / $divider ) ) );
}
// Display installment formatted price on all product types except variable
add_action( 'woocommerce_single_product_summary', 'display_product_installment_price', 15 );
function display_product_installment_price() {
global $product;
// Not for variable products
if ( $product->is_type( 'variable' ) ) {
// get active price for display
$active_price = wc_get_price_to_display( $product );
if ( $active_price ) {
// Display formatted installment price below product price
echo get_installment_price_html( $active_price );
}
}
}
// Display installment formatted price on product variations
add_filter( 'woocommerce_available_variation', 'display_variation_installment_price', 10, 3) ;
function display_variation_installment_price( $variation_data, $product, $variation ) {
$active_price = $variation_data['display_price']; // Get active price for display
if ( $active_price ) {
// Display formatted installment price below product price
$variation_data['price_html'] .= '<br>' . get_installment_price_html( $active_price );
}
return $variation_data;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
Note: You will need to make some changes to have different installment rates prices as you describe on your question??�
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.