Price conversion - wordpress

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.

Related

Display WooCommerce Unit Prices at Product variations

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!

Edit Attribute Name in Additional Information tab on WooCommerce Product Page [duplicate]

Using "Reorder and customize product dimensions formatted output in WooCommerce" answer code il would like to make the output display as:
Size: D40 x W45 x H60 (cm)
Any help is appreciated.
Just add the follows code snippet in your active theme's functions.php to achieve the above -
function woocommerce_display_product_attributes( $product_attributes, $product ){
if( !isset( $product_attributes['dimensions'] ) ) return $product_attributes;
$modified_dimensions = array();
foreach ( $product->get_dimensions( false ) as $key => $value ) {
if( $key == 'length' )
$modified_dimensions[$key] = 'D'.$value;
if( $key == 'width' )
$modified_dimensions[$key] = 'W'.$value;
if( $key == 'height' )
$modified_dimensions[$key] = 'H'.$value;
}
$dimension_string = implode( ' × ', array_filter( array_map( 'wc_format_localized_decimal', $modified_dimensions ) ) );
if ( ! empty( $dimension_string ) ) {
$dimension_string .= ' (' . get_option( 'woocommerce_dimension_unit' ) . ')';
} else {
$dimension_string = __( 'N/A', 'woocommerce' );
}
// change dimensions label & value.
$product_attributes['dimensions']['label'] = __( 'Size', 'text-domain' );
$product_attributes['dimensions']['value'] = $dimension_string;
return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'woocommerce_display_product_attributes', 99, 2 );
Welcome to StackOverflow!
Try a simpler code (tested):
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' ('.get_option( 'woocommerce_dimension_unit' ).')';
}

Woocommerce: display crossed price for free products

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

Show max variable price in shop page - Woocommerce

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)

Break Up WordPress Archive List into Multiple Columns

I want to break up this archive list into multiple columns (three columns)...I don't want to use CSS3 for this. I can easily do this with wp_list_categories, but that method doesn't work with wp_get_archives.
Ideally, I'd like to implement a counter to add in tags at an approximate 1/3 count...then I can use CSS for the rest.
CUSTOM ARCHIVE FUNCTION:
function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_custom_archive' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_custom_archive', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
}
}
$output .= '</ul>';
if ( $echo )
echo $output;
else
return $output;
}
CURRENT HTML OUTPUT (shortened):
<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>
If I understand correctly, you would have to do something like this
$output = '<ul class="col1">';
if ( $arcresults ) {
$numarticles = count($arcresults);
$column1 = round($numarticles/3);
$column2 = round($column1*2);
$counter = 0;
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
if($counter == $column1) {
$output .= "</ul><ul class='col2'>";
} elseif($counter == $column2) {
$output .= "</ul><ul class='col3'>";
}
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
$counter ++;
}
$output .= '</ul>';
}
This way, when the counter gets to a third of the way, it will add another list, so at the end you will have 3 lists.
Hope this helps.

Resources