I tried to change the price a single way, like WAS, NOW
it's working, But on the single product page, it's not working showing all variation prices line by line and when I change the variation price does not change(showing static).
snapshot here- https://prnt.sc/Tdg19atUacwq
code here below
add_filter( 'woocommerce_get_price_html', 'revent_custom_variable_product_price_customs' );
function revent_custom_variable_product_price_customs() {
global $product;
$id = $product->get_id();
echo '<p class="price">';
if( $product->is_type( 'variable' )){
// Regular Price
$v_prices = array($product->get_variation_regular_price( 'min', true ),$product->get_variation_regular_price( 'max', true ) );
sort( $v_prices );
$v_price = sprintf(__(' %1$s', 'woocommerce'), wc_price( $v_prices[1] ) ) ;
$v_saleprice = $v_prices[0]===$v_prices[1] ? sprintf(__('<span class="revent_custom_sale_price"> %1$s </span>','woocommerce') , wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );
if ($product->is_on_sale() ) {
$v_price = ' <del><span class="revent_custom_regular_price">Was: ' .$v_price.'</span>' . $product->get_price_suffix() . '</del> <ins><span class="revent_custom_sale_price">Now: '.$v_saleprice.'</span>'.$product->get_price_suffix() . '</ins></p>';
}else{
$v_price = sprintf(__('<span class="revent_custom_regular_price">Now: %1$s</span></p>', 'woocommerce'), wc_price( $v_prices[0] ) ) ;
}
echo $v_price;
}else{
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
// Regular Price
if ($product->is_on_sale() ) {
$v_price = ' <del><span class="revent_custom_regular_price"><span class="woocommerce-Price-amount amount">Was: ' .$regular_price.'<span class="woocommerce-Price-currencySymbol">د.إ</span></span></span> </del> <ins><span class="revent_custom_sale_price">Now: '.$sale_price.'</span><span class="woocommerce-Price-currencySymbol">د.إ</span></ins></p>';
}else{
$v_price= sprintf(__('<span class="revent_custom_regular_price">Now: %1$s</span></p>','woocommerce'), wc_price($regular_price) );
}
echo $v_price;
}
}
Please help me to fix this.
As per my understanding you are using sales option in woo commerce, for that you need to use
$product->get_variation_sale_price( 'max', true )
It will get the sale price and you can display it as you want.
Related
I have a question about styling a php script and change the way it shows but i cant get it work well
I use this script to display prices including and excluding TAX in Woocommerce.
I would like to display the price excluding TAX first, it needs to be styled with the class amountex (the $price value also)
After the price excluding Tax i want to show the price including TAX on a new row, this must be styled with the class amount
I made a lot of changes but the price is shown without styling and i can't get the price without tax showing first.
Advice is very welcome
This is the part i had done some changes
if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
$price_html = '<span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><br>';
$price_html .= '<span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><br>';
$price_html .= $product->get_price_suffix();
}
}
return $price_html;
This is the full code
add_filter('woocommerce_get_price_html', 'display_prices_incl_and_excl_taxes', 100, 2 );
function display_prices_incl_and_excl_taxes( $price_html, $product ) {
global $woocommerce_loop; {
// For simple products and products variations
if( $product->is_type('simple') || $product->is_type('variation') ) {
// On sale products
if( $product->is_on_sale() ) {
$regular_price_incl_tax = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) );
$price_incl_tax_html = wc_format_sale_price( $regular_price_incl_tax, wc_get_price_including_tax( $product ) );
$regular_price_excl_tax = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) );
$price_excl_tax_html = wc_format_sale_price( $regular_price_excl_tax, wc_get_price_excluding_tax( $product ) );
}
// Not on sale
else {
$price_incl_tax_html = wc_price( wc_get_price_including_tax( $product ) );
$price_excl_tax_html = wc_price( wc_get_price_excluding_tax( $product ) );
}
}
// variable pproducts
elseif( $product->is_type('variable') ) {
$prices = $product->get_variation_prices( true );
if ( ! empty( $prices['price'] ) ) {
$act_keys = array_keys($prices['price']);
$reg_keys = array_keys($prices['regular_price']);
$min_price_incl_tax = wc_get_price_including_tax( wc_get_product(reset($act_keys)));
$max_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($act_keys)));
$min_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($act_keys)));
$max_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($act_keys)));
$min_reg_price_jncl_tax = wc_get_price_including_tax( wc_get_product(reset($reg_keys)));
$max_reg_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($reg_keys)));
$min_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($reg_keys)));
$max_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($reg_keys)));
if ( $min_price_excl_tax !== $max_price_excl_tax ) {
$price_incl_tax_html = wc_format_price_range( $min_price_incl_tax, $max_reg_price_incl_tax );
$price_excl_tax_html = wc_format_price_range( $min_price_excl_tax, $max_reg_price_excl_tax );
}
elseif ( $product->is_on_sale() && $min_reg_price_excl_tax === $max_reg_price_excl_tax ) {
$price_incl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_incl_tax ), wc_price( $min_price_incl_tax ) );
$price_excl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_excl_tax ), wc_price( $min_price_excl_tax ) );
}
else {
$price_incl_tax_html = wc_price( $min_price_incl_tax );
$price_excl_tax_html = wc_price( $min_price_excl_tax );
}
}
}
if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
$price_html = '<span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><br>';
$price_html .= '<span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><br>';
$price_html .= $product->get_price_suffix();
}
}
return $price_html;
}
edit. The order of displaying the tax is resolved by swapping these 2 rules :)
The only thing i can't get working for now is the styling of $price_excl_tax_html in the first rule. My class seemed to be overruled by the woocommerce class: woocommerce-Price-amount
if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
$price_html = '<bdi><span class="amountex">' . $price_excl_tax_html . ' Excl. BTW </span><bdi><br>';
$price_html .= '<bdi><span class="amount">' . $price_incl_tax_html . ' Incl. BTW </span><bdi><br>';
$price_html .= $product->get_price_suffix();
}
}
return $price_html;
Looking for a bit of help here, we have simple product in woocommerce and we want to display a you save % and amount on the product page for any discount we offer.
We have it set up for a variable product already but not for a simple product can anyone help with this.
Here is the code we have for the variable product.
add_filter( 'woocommerce_variable_price_html', 'imti_variation_price_format_310', 10, 2 );
function imti_variation_price_format_310( $price, $product ) {
// Getting the clean numeric prices (without html and currency)
$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );
$price_diff = $min_var_reg_price - $min_var_sale_price ;
$currency_symbol = get_woocommerce_currency_symbol();
// Percentage calculation and text
$percentage = round( ( $min_var_reg_price - $min_var_sale_price ) / $min_var_reg_price * 100 ).'%';
$percentage_txt = __('<br/><p class="you-save"><small style="color:#d1461c; font-size:12.8px; font-weight:bold;"> You Save '.$percentage. ' (- '.$currency_symbol. ''.$price_diff. ') </small></p>' , 'woocommerce' );
return '<del>' . wc_price( $min_var_reg_price ) . '</del> <ins>' . wc_price( $min_var_sale_price ) . $percentage_txt . '</ins>';
}
Thanks
Jamie
Okay. The easy way it changes your hook to woocommerce_get_price_html. But I want to explain the problem with your solution. I suggest to move in another way. Firstly create a function which works with product independent this is variation product or simple:
function discount_message( $product ) {
$sale_price = 'variable' === $product->get_type()
? $product->get_variation_sale_price( 'min' )
: $product->get_sale_price();
$regular_price = 'variable' === $product->get_type()
? $product->get_variation_regular_price( 'min' )
: $product->get_regular_price();
$discount = round( $sale_price / $regular_price * 100 );
echo '<p class="you-save"><small>';
echo sprintf(
__( 'You Save %1$s (-%2$s)', 'your-domain' ),
absint( $discount ) . '%',
wc_price( absint( $regular_price - $sale_price ) )
);
echo '</small></p>';
}
Then create a function that will separately from product price (or you can also use filter woocommerce_get_price_html but I don't like this idea):
function single_product_discount_message() {
global $product;
discount_message( $product );
}
add_action( 'woocommerce_single_product_summary', 'single_product_discount_message', 9 );
When you will add the single_product_discount_message to hook woocommerce_single_product_summary you can change priority number(9) to another to move this label on your product cart.
I'm setting up a function for Woocommerce that display discount price and the regular price for variable product, this function add a " from to " text before the price range.
The "WooCommerce variable products: keep only "min" price with a custom label" answer thread matches the best with what I am looking for and work like a charm!
But when all variations in a variable product have the same prices, the " start from " should not be displayed.
So I've made a simple try and add below the "if" condition
else {
$price = sprintf( __( '%1$s', 'woocommerce' ), $min_price_html );
return $price;
}
and integrate in the if condition:
$price = sprintf( __( 'À partir de %1$s', 'woocommerce' ), $min_price_html );
return $price;
But it doesn't really work. Some help is appreciated and welcome.
To handle when all variations of a variable product are the same, you need to use array_unique() php function and count() to look if all variation prices are the same.
So the code will be slightly different:
add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
$count = (int) count( array_unique( $prices['price'] ));
// When all variations prices are the same
if( $count === 1 )
return $price;
$min_price = current( $prices['price'] );
$min_keys = current(array_keys( $prices['price'] ));
$min_reg_price = $prices['regular_price'][$min_keys];
$min_price_html = wc_price( $min_price ) . $product->get_price_suffix();
// When min price is on sale (Can be removed)
if( $min_reg_price != $min_price ) {
$min_price_reg_html = '<del>' . wc_price( $min_reg_price ) . $product->get_price_suffix() . '</del>';
$min_price_html = $min_price_reg_html .'<ins>' . $min_price_html . '</ins>';
}
$price = sprintf( __( 'À partir de %s', 'woocommerce' ), $min_price_html );
return $price;
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
I do have a rather complex problem with price calculations in WooCommerce using the plugin GravityForms.
I do sell mostly customized products offering engraving options. Thats why I need GravityForms for.
I wanted to replace the standard woocommerce price by the chosen variable price below the heading. For that I used the very good solution made by
#Loictheaztec
Replace the variable price range by the chosen variation price
Here is his code I'am using in my functions.php
// Utility function to get the default variation (if it exist)
function get_default_variation( $product ){
$attributes_count = count($product->get_variation_attributes());
$default_attributes = $product->get_default_attributes();
// If no default variation exist we exit
if( $attributes_count != count($default_attributes) )
return false;
// Loop through available variations
foreach( $product->get_available_variations() as $variation ){
$found = true;
// Loop through variation attributes
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
// Searching for a matching variation as default
if( isset($default_attributes[$taxonomy]) &&
$default_attributes[$taxonomy] != $value ){
$found = false;
break;
}
}
// If we get the default variation
if( $found ) {
$default_variaton = $variation;
break;
}
// If not we continue
else {
continue;
}
}
return isset($default_variaton) ? $default_variaton : false;
}
add_action( 'woocommerce_before_single_product',
'move_variations_single_price', 1 );
function move_variations_single_price(){
global $product, $post;
if ( $product->is_type( 'variable' ) ) {
// removing the variations price for variable products
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
// Change location and inserting back the variations price
add_action( 'woocommerce_single_product_summary',
'replace_variation_single_price', 10 );
}
}
function replace_variation_single_price(){
global $product;
// Main Price
$prices = array( $product->get_variation_price( 'min', true ),
$product->get_variation_price( 'max', true ) );
$active_price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s',
'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ),
$product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$regular_price = $prices[0] !== $prices[1] ? sprintf( __( 'From:
%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price(
$prices[0] );
if ( $active_price !== $regular_price && $product->is_on_sale() ) {
$price = '<del>' . $regular_price . $product->get_price_suffix() .
'</del> <ins>' . $active_price . $product->get_price_suffix() .
'</ins>';
} else {
$price = $regular_price;
}
// When a default variation is set for the variable product
if( get_default_variation( $product ) ) {
$default_variaton = get_default_variation( $product );
if( ! empty($default_variaton['price_html']) ){
$price_html = $default_variaton['price_html'];
} else {
if ( ! $product->is_on_sale() )
$price_html = $price =
wc_price($default_variaton['display_price']);
else
$price_html = $price;
}
$availiability = $default_variaton['availability_html'];
} else {
$price_html = $price;
$availiability = '';
}
// Styles ?>
<style>
div.woocommerce-variation-price,
div.woocommerce-variation-availability,
div.hidden-variable-price {
height: 0px !important;
overflow:hidden;
position:relative;
line-height: 0px !important;
font-size: 0% !important;
}
</style>
<?php // Jquery ?>
<script>
jQuery(document).ready(function($) {
var a = 'div.wc-availability', p = 'p.price';
$('select').blur( function(){
if( '' != $('input.variation_id').val() ){
if($(a).html() != '' ) $(a).html('');
$(p).html($('div.woocommerce-variation-price >
span.price').html());
$(a).html($('div.woocommerce-variation-
availability').html());
} else {
if($(a).html() != '' ) $(a).html('');
$(p).html($('div.hidden-variable-price').html());
}
});
});
</script>
<?php
echo '<p class="price">'.$price_html.'</p>
<div class="wc-availability">'.$availiability.'</div>
<div class="hidden-variable-price" >'.$price.'</div>';
}
Lets clarify a bit the things. Please see the screenshot to know where the woocommerce variations and the GravityForm fields are located.
The css class of the woocommerce price is:
`<p class="price">
<span class="woocommerce-Price-amount amount">45 </span>
<span class="woocommerce-Price-currencySymbol">€</span>
<span class="mwst"> inkl. MwSt.</span>
</p>`
And the css class of the GravityForms price is:
<li class="gfield">
<label class="gfield_label">Total</label>
<div class="ginput_container">
<span class="formattedTotalPrice ginput_total">45,00  €</span>
</div>
</li>
Now lets select a price variation from the woocommerce fields.
This outputs very nicely the selected variation price of woocommerce at top as well as on the bottom.
So now lets start the problem and select a variation price out of a GravityForms field.
As you can see the price on top won't recognize the price change. But the price displayed at the lower end shows it correctly.
The question is now, is it possible to modify the code which replaces the variable prices by chosen variation price in a way that it will recognize price changes made via GravityForms too?
Update
I've searched through the documentation of gravity forms to find a filter hook for the price label. And I have found this one:
GravityForms Documentation - gform_product_price Filter
Then I checked what happens if I use this function in the functions.php file of my child theme.
add_filter( 'gform_product_price', 'set_price_label', 10, 2 );
function set_price_label( ) {
echo 'Some output somewhere?';
}
or this function
add_filter( 'gform_product_price', 'set_price_label_test', 10, 2 );
function set_price_label_test( $sublabel, $form_id ) {
return 'Cost';
}
unfortunately nothing happened.
Here is an image of the result.
How to display woocommerce variable price for current active variation on single product page?
I use the code:
<?php
global $product;
if ($product->is_type( 'simple' )) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php
if($product->product_type=='variable') {
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
echo $regular_price+$sales_price;
}
?>
But it shows only lowest variable price instead of currently selected variation's price.
How can I display the current price for the active variation?
So, you can just modify \your-theme\woocommerce\single-product\sale-flash.php file
Or, your can also use filter.
By the way there is more simple solution:
if ($product->is_type( 'simple' )) {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
}
elseif($product->is_type('variable')){
$sale_price = $product->get_variation_sale_price( 'min', true );
$regular_price = $product->get_variation_regular_price( 'max', true );
}
$discount = round (($sale_price / $regular_price -1 ) * 100);
}
Or you can copy this gist https://gist.github.com/sholex/4064fc2b656c0857a78228cf5324b370
<?php
global $product;
if ($product->is_type( 'simple' )) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php
if($product->product_type=='variable') {
$available_variations = $product->get_available_variations();
$count = count($available_variations)-1;
$variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
echo $regular_price+$sales_price;
}
?>
try this. This may help you.
<?php
global $product;
if ($product->is_type( 'simple' )) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php
if($product->product_type=='variable') {
$available_variations = $product->get_available_variations();
foreach($available_variations as $key=>$val){
if(trim($val['variation_id'])==**"your selected variant id"**){
$variation_id=$available_variations[$key]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
}
}
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
echo $regular_price+$sales_price;
}
?>
add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );
function check_if_variable_first(){
if ( is_product() ) {
global $post;
$product = wc_get_product( $post->ID );
if ( $product->is_type( 'variable' ) ) {
// removing the price of variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Change location of
add_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );
function custom_wc_template_single_price(){
global $product;
// Variable product only
if($product->is_type('variable')):
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'От: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'От: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice && $product->is_on_sale() ) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
?>
<style>
div.woocommerce-variation-price,
div.woocommerce-variation-availability,
div.hidden-variable-price {
height: 0px !important;
overflow:hidden;
position:relative;
line-height: 0px !important;
font-size: 0% !important;
}
</style>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() ){
$('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
console.log($('input.variation_id').val());
} else {
$('p.price').html($('div.hidden-variable-price').html());
if($('p.availability'))
$('p.availability').remove();
console.log('NULL');
}
});
});
</script>
<?php
echo '<p class="price">'.$price.'</p>
<div class="hidden-variable-price" >'.$price.'</div>';
endif;
}
}
}
}