I am Using "Change number of decimals in Woocommerce cart totals" answer code to display the total with 2 decimals. The code works fine for the displayed total in cart and checkout pages.
I would like if possible to do the same thing for the subtotal too. Any help is appreciated.
To format the displayed subtotal amount with 2 decimals in cart and checkout pages use the following:
// Displayed formatted cart subtotal in cart and checkout pages
add_filter( 'woocommerce_cart_subtotal', 'filter_cart_subtotal_html', 10, 3 );
function filter_cart_subtotal_html( $cart_subtotal, $compound, $cart ) {
$decimals = 2; // <== <== Set the number of decimals to be displayed
$args = ['decimals' => strval($decimals)]; // Initializing
if ( $compound ) {
$cart_subtotal = wc_price( $cart->get_cart_contents_total() + $cart->get_shipping_total() + $cart->get_taxes_total( false, false ), $args );
} elseif ( $cart->display_prices_including_tax() ) {
$cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax(), $args );
if ( $cart->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$cart_subtotal = wc_price( $cart->get_subtotal(), $args );
if ( $cart->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
return $cart_subtotal;
}
Code goes in functions.php file of the active child theme (or active theme), or a plugin file. Tested and works.
Related: Change number of decimals in Woocommerce cart totals
Related
I'm using this code snippet to display the total order savings at WooCommerce checkout:
add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 9999 );
function show_total_discount_cart_checkout() {
$discount_total = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( $product->is_on_sale() ) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$discount = ( $regular_price - $sale_price ) * $values['quantity'];
$discount_total += $discount;
}
}
if ( $discount_total > 0 ) {
echo '<tr class="total-saved"><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
}
}
It should display the total amount of money a customer saved (sale prices plus coupon discounts). Screenshot: https://ibb.co/KXg2bDj
However, if there are no discounted products in the cart, the total order savings do not show up, even if there is a coupon code applied to the order. The total order savings show up only if there are discounted products in the cart. Screenshot: https://ibb.co/PCQPGZx
I would like the total order savings to show up if there is a coupon code applied to the order, if there are discounted products in the cart or if there's both. If there's neither 1 of those 2, the total order savings do not need to show up.
Could someone please help me achieve this?
Try the following instead, that will solve your issue and handle displaying tax settings:
add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 1000 );
function show_total_discount_cart_checkout() {
$discount_total = 0; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
$product = $item['data'];
if ( $product->is_on_sale() ) {
$regular_args = array( 'price' => $product->get_regular_price() );
if ( WC()->cart->display_prices_including_tax() ) {
$active_price = wc_get_price_including_tax( $product );
$regular_price = wc_get_price_including_tax( $product, $regular_args );
} else {
$active_price = wc_get_price_excluding_tax( $product );
$regular_price = wc_get_price_excluding_tax( $product, $regular_args );
}
$discount_total += ( $regular_price - $active_price ) * $item['quantity'];
}
}
if ( WC()->cart->display_prices_including_tax() ) {
$discount_total += WC()->cart->get_discount_tax();
}
$discount_total += WC()->cart->get_discount_total();
if ( $discount_total > 0 ) {
$text = __("You Saved", "woocommerce");
printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">%s</td></tr>', $text, $text, wc_price($discount_total) );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I've been trying to modify Display price on add to cart button from the functions.php file in WooCommerce so it displays a price on the "Add to cart" button (in Wordpress) but only if the price is higher than zero.
Unfortunately I'm stuck. When the price is zero or not set at all the button still displays "Add to cart - 0.00 USD". I just need "Add to cart".
Note the extra if conditions who check the price. Explanation via comment tags added in the code
function custom_add_to_cart_price( $button_text, $product ) {
// Product type = variable
if ( $product->is_type('variable') ) {
// NOT return true when viewing a single product.
if( ! is_product() ) {
// Get price
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) );
// Greater than
if ( $price > 0 ) {
$product_price = wc_price( $price );
$button_text .= ' - From ' . strip_tags( $product_price );
}
}
} else {
// Get price
$price = wc_get_price_to_display( $product );
// Greater than
if ( $price > 0 ) {
$product_price = wc_price( $price );
$button_text .= ' - Just ' . strip_tags( $product_price );
}
}
return $button_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 10, 2 );
I understand that the title of my question might sound confusing and therefore, I ask you to look at the attached image for clarification.
I've tried myself to make this work without success. Testing my code changes nothing on the checkout page. No change, no error, no notices - nothing.
My code so far:
add_filter( 'woocommerce_cart_item_price', 'item_price_and_total_price', 10, 3 );
function item_price_and_total_price( $price, $cart_item, $cart_item_key ) {
foreach( WC()->cart->get_cart() as $cart_item ){
$item = $cart_item['data']->get_id();
}
// item price title
$price_title = 'Item price';
// item price (this should not change as the qty changes)
$price_per_item = wc_price( $item ) . ' per item';
// total item price (cost) as the qty changes
$total_item_price = // don't know how to get it
// return everything
$price = $price_title . '<br />' . $price_per_item . '<br />' . $total_item_price;
return $price;
}
Someone who can tell me where it goes wrong?
You could use the woocommerce_cart_item_subtotal hook instead.
The use of a foreach loop is not necessary
Notice the use of WooCommerce Conditional tags: is_checkout() - Returns true on the checkout page
Optional: $cart_item['quantity']; can be used if necessary to provide additional conditions based on the product quantity
function filter_woocommerce_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
// Returns true on the checkout page
if ( is_checkout() ) {
// Item price title
$price_title = 'Item price';
// Item price
$item_price = $cart_item['data']->get_price();
// Line subtotal
$line_subtotal = $cart_item['line_subtotal'];
// Item title + item price
$subtotal = '<span>' . $price_title . '</span>' . wc_price( $item_price );
// Append to
$subtotal .= wc_price( $line_subtotal );
}
return $subtotal;
}
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_subtotal', 10, 3 );
I use some code that shows the discounted price on Woocommerce archive pages for on sale products:
add_filter( 'woocommerce_get_price_html', 'display_savings_as_price_and_percentage', 10, 2 );
//add_filter( 'woocommerce_variable_price_html','display_savings_as_price_and_percentage', 10, 2 );
function display_savings_as_price_and_percentage( $price, $product ) {
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
$product_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_price();
$save_price = wc_price( $product_price - $sale_price );
$save_percentage = round( 100 - ( $sale_price / $product_price * 100 ), 1 ) . '%';
$price .= sprintf( __('<p class="saved-on-sale">Save: %s (%s)</p>', 'woocommerce' ), $save_price, $save_percentage );
}
return $price;
}
Discount on archives pages:
But it doesn't work for variable products and I've tried with every hook I've found but I cannot figure out how to make it work variable products.
(I commented out the hook for the variable in my code).
This is what I would like to have:
If the variable has a price span of 10-20 with a discount making it 10-15 I would like for that to be displayed like this with the default price striked through:
$40 - $50
$20 - $30
Save: $20 (40-50%)
How can I replace the on sale product price for variable products with saving amount and percentages?
The following code will handle both simple and variable on sale products:
add_filter( 'woocommerce_get_price_html', 'display_savings_price_and_percentages', 20, 2 );
function display_savings_price_and_percentages( $price_html, $product ) {
// Only on frontend and for on sale products
if( is_admin() || ! $product->is_on_sale() )
return $price_html;
// Only on archives pages
if( ! ( is_shop() || is_product_category() || is_product_tag() ) )
return $price_html;
// Variable product type
if( $product->is_type('variable')){
$percentages = $savings = array(); // Initializing
// Get all variation prices
$prices = $product->get_variation_prices();
// Loop through variation prices
foreach( $prices['price'] as $key => $price ){
// Only on sale variations
if( $prices['regular_price'][$key] !== $price ){
// Calculate and set in the array the percentage for each variation on sale
$percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100), 1 );
// Calculate and set in the array the savings for each variation on sale
$savings[] = $prices['regular_price'][$key] - $prices['sale_price'][$key];
}
}
$save_price = wc_price( max($savings) );
if( min($percentages) !== max($percentages) ){
$save_percentage = min($percentages) . '-' . max($percentages) . '%';
$save_text = __( 'Save up to:', 'woocommerce' );
} else {
$save_percentage = max($percentages) . '%';
$save_text = __( 'Save:', 'woocommerce' );
}
}
// All other product types
else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$save_price = wc_price( $regular_price - $sale_price );
$save_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
$save_text = __( 'Save:', 'woocommerce' );
}
return '<p class="saved-on-sale">' . sprintf( '%s %s (%s)', $save_text, $save_price, $save_percentage ) . '</p>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I'm using the following script:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) {
$price .= '<span style="display:none;" class="not-from">v.a. ' . _x('', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
}
return $price;
}
It does show the min variation price at this moment. I want to create the following:
If the min price is 0,00, then show the 2nd product variation price.
E.g.: Product X has 3 different variations: 1 (€0,00), 2 (€5,00), 3 (€15,00). If the variation price is 0,00, then show the second price variation, €5,00.
Here is the way to display the 2nd min variable price whenthe min price is equal to 0:
add_filter('woocommerce_variable_price_html', 'custom_variation_price_html', 10, 2);
function custom_variation_price_html( $price, $product ) {
// Get all variations prices
$prices = $product->get_variation_prices( true );
// Get the prices (min and max)
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
// Get 2nd min price
$min_price2_arr = array_slice($prices['price'], 1, 1);
$min_price2 = $min_price2_arr[0];
if ( $min_price > 0) {
$price = wc_price( $min_price );
} elseif ( $min_price == 0 ) {
$price = wc_price( $min_price2 );
}
return $price;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This is code tested and works only in WooCommerce 3+.