Show max variable price in shop page - Woocommerce - 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)

Related

Sup in Woocommerce with comma for decimal

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.

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!

Price conversion

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.

Woocommerce Calculate amount by customer input in product page

In product page to get input from customer as number of words. To calculate the amount from products add-ons fixed amount to additional 10$ for each above 500 words.
ex: 1. Number of Words: 530
Normal Price + additional price: 180$ + 10$
Total: 190$
Number of Words: 1040
Normal Price + additional price: 180$ + 20$
Total: 200$
This process as to create dynamic input form customer to calculate amount the price and total amount.
`$extra_amount = (int)'10';
$amount = (int)'180'; // how to get amount from woocommerce data
if(isset($_POST['wordnumber'])){ // how to get this paramater form woocommerce post values
$test =$_POST['wordnumber'];
$original_amount = '';
if($test <= (int)'500'){
$original_amount = $amount;
}
elseif($test > (int)'500'){
$div_amount = $test/(int)'500';
$round = floor($div_amount);
//echo '<br/>';
$total_extra = $round*$extra_amount;
$original_amount = $amount+$total_extra;
}
echo $original_amount;
}`
I installed WC Fields Factory plugin then i got value from one field using key. in that i written a function and i overrided the price value.
function calculate_gift_wrap_fee( $cart_object ) {
/* Gift wrap price */
$additionalPrice = 10;
foreach ( $cart_object->cart_contents as $key => $value ) {
$test = $value['custom field'];
if( $test <= 100 ) {
$quantity = floatval( $value['quantity'] );
$orgPrice = floatval( $value['data']->price );
$value['data']->price = ( $orgPrice );
}
elseif( $test > 100 ) {
$div_amount = floatval($test/500);
$round = floor($div_amount);
$total_extra = floatval($round * $additionalPrice);
$quantity = floatval( $value['quantity'] );
$orgPrice = floatval( $value['data']->price );
$pp = ($value['price']);
$total = floatval($pp + $total_extra);
$value['data']->price = ( $orgPrice + $total );
}
}}
add_action( 'woocommerce_before_calculate_totals', calculate_gift_wrap_fee', 1, 1 );

convert price on woocommerce

My products price is based on USD , but i want change USD to IRR with a Conversion Rate (1 USD = 3500 IRR) in shop and checkout . I found this code but it works only for product veiw and when i click on cart or checkout price change to USD .
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$toman = 3500;
$total = $price * $toman . ' ' . 'تومان';
return $total;
}

Resources