I would need your help, if there is someone willing to help me, I would greatly appreciate it. In my website prices include a 10.0000% tax.
I want to calculate in my onsale badge my tax from woocommerce which is of 10.0000%. This is my code:
Badge: onsale badge from site
add_filter( 'woocommerce_sale_flash', 'add_amount_to_sale_badge', 20, 3 );
function add_amount_to_sale_badge( $html, $post, $product ) {
if( $product->is_type('variable')){
$amount_off = array();
$prices = $product->get_variation_prices();
foreach( $prices['price'] as $key => $price ){
if( $prices['regular_price'][$key] !== $price ){
$amount_off[] = $prices['regular_price'][$key] - $prices['sale_price'][$key];
}
}
$amount_off_s = "up to -$" . round(max($amount_off));
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$amount_off_s = "-$" . ($regular_price - $sale_price);
}
return '<span class="onsale">' . esc_html__( 'Save', 'woocommerce' ) . ' ' . $amount_off_s . '</span>';
}
my knowledge in php is limited and for this reason I am unable to solve this problem.
Related
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.
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;
I try to manipulate the calculated price in cart but with no luck...
I hope someone can help me here.
I have found this article and implemented the code as written on the Page which is to 98% exactly what i searched for. I need to add a cart calculation if the price type is per 100g.
So this is the actual working code:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html', 10, 2 );
// Adding a custom field to the price markup
function wb_change_product_html( $price, $product ) {
//$wb_price_type = get_field('product_price_type');
$wb_price_type = get_post_meta( $product->get_id(), 'product_price_type', true);
if($wb_price_type) {
$price_html = '<span class="amount">' . $price . ' ' . $wb_price_type . '</span>';
}
else {
$price_html = '<span class="amount">' . $price . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
$price = $price . ' ' . $wb_price_type;
}
else {
$price = $price;
}
return $price;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'wb_checkout_review', 10, 3 );
// Adding a custom field to the price in the checkout items
function wb_checkout_review ( $quantity, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true);
if ( $wb_price_type ) {
$cart_item = ' ' . sprintf( '× %s ', $cart_item['quantity'] ) . $wb_price_type . '';
}
else {
$cart_item = ' ' . sprintf( '× %s', $cart_item['quantity'] ) . '';
}
return $cart_item;
}
Well i thought its really easy to calculate so i do something like this:
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3 );
// Adding a custom field to the price in the cart
function wb_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
//$wb_price_type = get_field( 'product_price_type', $cart_item['product_id'] );
$wb_price_type = get_post_meta( $cart_item['product_id'], 'product_price_type', true );
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
$price = $price / 100 . ' ' . $wb_price_type;
}
else {
$price = $price . ' ' . $wb_price_type;
}
}
else {
$price = $price;
}
return $price;
}
But this didnt work... i too try to manipulate the price directly but i get anytime 0 in the value of the price...
so i google a little bit more and found this article, which makes in the end exactly what i want. in the last print screen we can see that the price will displayed per kg (which is completely fine) and in the calculation it uses an other price (which i try to do with the code above).
i think that the problem is that $price has for example the value "20$" and this is a string and i cannot calculate withe a string. And it makes no sense to split this string there should be an other way.
For better understanding here a picture. in the upper part we see the working calculation (which is good) but this is not exactly what i need. in the part down we see that it will only calculate on the calculated price (right side). the left side of the downer part remain with the correct price.
i think that the problem is that $price has for example the value "20$" and this is a string and i cannot calculate withe a string. And it makes no sense to split this string there should be an other way.
This is true, it's actually a full html string. Try changing the woocommerce_cart_item_price filter to get the price out of string:
add_filter('woocommerce_cart_item_price', 'wb_change_product_price_cart', 10, 3);
// Adding a custom field to the price in the cart
function wb_change_product_price_cart($price, $cart_item, $cart_item_key)
{
$wb_price_type = get_post_meta($cart_item['product_id'], 'product_price_type', true);
if ($wb_price_type) {
if ($wb_price_type == "per 100g") {
preg_match("/([0-9]+\.[0-9]+)/", $price, $matches);
$_price = $matches[0];
$per100 = $_price / 100;
$price = $per100 . ' ' . $wb_price_type;
} else {
$price = $price . ' ' . $wb_price_type;
}
} else {
$price = $price;
}
return $price;
}
Good afternoon friends, I will be brief; I am trying to customize the display of discounted product prices as shown in the following image:
Example
Now, I have already obtained several codes and I am modifying them to achieve that result. It works well in most products, but in variable products it is something else. Variable products that have a discount stop working, In products that have variations it gives error when a variation has a discount, the product stops working. Could you please help me? I would appreciate!
I have attached the code I have.
/* Text before and after the discounted price */
add_filter('woocommerce_format_sale_price', 'filter_function_name', 10, 3);
function filter_function_name($price, $regular_price, $sale_price) {
$product = new WC_Product(get_the_ID());
if (!is_admin() && $product->is_type('simple')) {
$regular_price = 'Before: ' . wc_price($regular_price);
$sale_price = 'After: ' . wc_price($sale_price);
$price = $regular_price . ' - ' . $sale_price;
}
return $price;
};
/*---------- Showing the saving message -----------*/
function ts_you_save() {
global $product;
if ($product->is_type('simple') || $product->is_type('external') || $product->is_type('grouped')) {
$regular_price = get_post_meta($product->get_id(), '_regular_price', true);
$sale_price = get_post_meta($product->get_id(), '_sale_price', true);
if (!empty($sale_price)) {
$amount_saved = $regular_price - $sale_price;
$currency_symbol = get_woocommerce_currency_symbol();
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
?>
<p style="font-size:24px;color:red;"><b>You Save: <b>$</b><?php echo number_format($amount_saved, 2, '.', '') . " (" . number_format($percentage, 0, '', '') . "%)"; ?></b></p>
<?php
}
}
}
add_action('woocommerce_single_product_summary', 'ts_you_save', 11);
Thank you very much for your time, I await your response :)
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.