I wrote the following code to change the position and appearance of the decimal places.
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
$decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
return $unit . '<sup>' . $decimal . '</sup>';
}
The code works so far that the decimal places are superscript, but now I'm missing the comma.
Related
So, I have a woocommerce shop where a 10% discount is applied to all products in the cart and at checkout if a certain payment gateway is used.
My shop has both simple and variable products, and they are all virtual products. I thought of showing on the product page the following information:
the lowest price (for variable products)
the 10% discounted price I could get at checkout.
To do this I used the following code for variable products:
add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
function wpglorify_variation_price_format( $price, $product ) {
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'A partire da: %1$s (' . $prices[0]*.9 . '€ se paghi online)' , 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
return $price;
}
For simple products I used this snippet instead:
add_filter( 'woocommerce_get_price_html', 'change_displayed_price_html', 10, 2 );
function change_displayed_price_html( $price, $product ) {
if( $product->is_type('simple') ){
$regular_price = (float) $product->get_regular_price();
$saving_price = $regular_price*.9;
$price .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
}
return $price;
}
Everything works fine, but on variable products I have a problem. When I select a variation, I would like to give the same information next to the price being shown and related to that variation. I tried with the following snippet:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $price, $product, $variation ) {
$variation_price = (float) $product->get_variation_price();
$saving_price = $variation_price*.9;
$price['availability_html'] .= sprintf( __(' (€%s se paghi online)', 'woocommerce' ), $saving_price );
return $price;
}
The problem is that the discounted price is calculated on the price of the first variation, regardless of which variation I select. For example, if variation A = $100, a discounted price of $90 is shown. If I select variation B = $120, a discounted price of $90 is always shown. Where am I doing wrong?
Thanks for your answers.
I like to display unit prices at variable products. I like it to do based on a product attribute whic contains the following: 5 kg, 2 l etc. So, I get the attribute, extract the numbers from it and do some math. The problem is, that all of the code displays the multipier number of 1. Here is my code:
function product_variation_unit_price( $data, $product, $variation ) {
$price = wc_get_price_to_display( $variation );
$unit = array_shift( wc_get_product_terms( $product->id, 'pa_unit', array( 'fields' => 'names' ) ) );
$unit_number_extracted = preg_replace('/[^0-9]/', '', $unit);
if ( (!empty($unit) ) AND $unit_number_extracted == 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price * $unit_number_extracted) );
} else if( (!empty($unit) ) AND $unit_number_extracted > 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price / $unit_number_extracted) );
}
$data['price_html'] .= '<span> ' . $unit_price . '</span>';
return $data;
}
add_filter( 'woocommerce_available_variation', 'product_variation_unit_price', 10, 3 );
I guess I should foreach the variations, but I don't know how to do that in this situation.
Thanks if you can help!
I can't display crossed price for 0 priced variable products.
That is my code. Where I'm going wrong ?
add_filter( 'woocommerce_get_price_including_tax_html', 'woocommerce_get_regular_price_html', 'woocommerce_get_price_html', 'price_free_zero_empty', 9998, 2 );
function price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price_including_tax() || 0 == $product->get_price_including_tax() ) {
$regular_price = $product->get_price_including_tax();
$price = '<del>' . wc_price( $regular_price ) . '</del> '. '<span class="woocommerce-Price-amount amount">'.__("Free", "woocommerce").'</span>';
}
return $price;
}
function wpglorify_price_free_zero_empty( $price, $product ) {
if ( $product->get_price() == 0 ) {
if ( $product->is_on_sale() && $product->get_regular_price() ) {
$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
$price = wc_format_price_range( '<del>' . wc_price($regular_price) . '</del>', __( 'Omaggio!', 'woocommerce' ) );
} else {
$price = '<span class="amount">' . __( 'Omaggio!', 'woocommerce' ) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wpglorify_price_free_zero_empty', 10, 2 );
I have this code to display the price.
function edit_selected_variation_price( $data, $product, $variation ) {
$price = $variation->price;
$price_incl_tax = $price + round($price * ( 23 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<div><span class="price">';
$display_price .= '<span>Cena netto</span><span class="amount">' . $price .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</div><div>';
$display_price .= '<span>Cena brutto</span><span class="amount">' . $price_incl_tax .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</span></div>';
$data['price_html'] = $display_price;
return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);
How to get the maximum price displayed without choosing any variants?
I do not fully understand your question, you want the max price from what prices (incl tax and ...)?
However, when you have different prices in an array (or as loose variables), consider using the PHP max() function:
https://www.php.net/manual/en/function.max.php
That will return the highest value (Hope that's what you're after)
I need some help.
On a Wordpress real estate website, I would like to display the price in XPF next to the price in €.
The price function is:
public static function format_price($price,$html = true){
$return = '';
$currency_code = self::get_general_option('currency');
$currency_symbol = self::get_currency_symbol($currency_code);
$currency_position = self::get_general_option('currency_position');
switch ( $currency_position ) {
case 'left' :
$format = '%1$s%2$s';
break;
case 'right' :
$format = '%2$s%1$s';
break;
case 'left_space' :
$format = '%1$s %2$s';
break;
case 'right_space' :
$format = '%2$s %1$s';
break;
default:
$format = '%1$s%2$s';
}
$thousands_sep = wp_specialchars_decode( stripslashes(self::get_general_option('price_thousand_sep')),ENT_QUOTES);
$decimal_sep = wp_specialchars_decode( stripslashes(self::get_general_option('price_decimal_sep')),ENT_QUOTES);
$num_decimals = self::get_general_option('price_num_decimals');
$price = floatval( $price );
if(!$html) {
return self::number_format( $price, $num_decimals, '.', '', $currency_code );
}
$price = self::number_format( $price, $num_decimals, $decimal_sep, $thousands_sep, $currency_code );
if('text' === $html) {
return sprintf( $format, $currency_symbol, $price );
}
//$price = preg_replace( '/' . preg_quote( self::get_general_option('price_decimal_sep'), '/' ) . '0++$/', '', $price );
$return = '<span class="amount">' . sprintf( $format, $currency_symbol, $price ) . '</span>';
return $return;
}
The conversion rate is : 1€ = 119.33XPF
How can I edit the code to display the price like this for instance :
11933000XPF (100000€)
Thanks.
You have the floatval of the price in euros. If your conversion rate is a fixed value just convert like this:
$price_xpf = $price * 119.33;
The formatting of the price is working in exact analogy as the formatting for the price in euros.
Just for display do the following:
$return = '<span class="amount">'. sprintf( $format,"XPF",$price_xpf ) ."(". sprintf( $format, $currency_symbol, $price ) . ')</span>';
For any other adaption most basic programming skills will be helpful.