Display tax amount based on specific tax class in WooCommerce variations - php

I'm currently using a custom function to target a specific product and change the output of the price with and without tax on the product page.
This currently works as intended for an individual product id, however trying to get this work for a specific tax_class instead with no avail
add_filter( 'woocommerce_available_variation', 'tax_variation', 10, 3);
function tax_variation( $data, $product, $variation ) {
$product = wc_get_product();
$id = $product->get_id();
$price_excl_tax = wc_get_price_excluding_tax( $variation ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $variation ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
if ( $id == 113576 ) {
$data['price_html'] = "<span class='ex-vat-price'>Price: <b>" . woocommerce_price($variation->get_price_excluding_tax()) . "</b></span><br>";
$data['price_html'] .= "<span class='tax_amount'>Sales Tax 13%: <b>" . woocommerce_price($tax_amount) . "</b></span><br>";
$data['price_html'] .= "<span class='inc-vat-price'>Total: <b>" . woocommerce_price($variation->get_price_including_tax()) . "</b></span>";
return $data;
} else {
$data['price_html'] .= "<span class='regular-price'> " . woocommerce_price($variation->get_price()) . "</span>";
return $data;
}
}
i want to change the if parameter to
$taxclass = $product->get_tax_class();
if ( $taxclass == 'costa-rate' ) {
but currently this does not function correctly and displays the regular price data twice

Since WooCommerce 3, your code is outdated and with some errors.
I guess that you are using woocommerce_available_variation action hook.
Try the following instead:
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3 );
function custom_variation_price( $data, $product, $variation ) {
$price_excl_tax = (float) wc_get_price_excluding_tax( $variation ); // price without VAT
$price_incl_tax = (float) wc_get_price_including_tax( $variation ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax;
if( $variation->get_tax_class() === 'costa-rate' ) {
$data['price_html'] = '<span class="ex-vat-price">' . __("Price:") . ' <strong>' . wc_price($price_excl_tax) . '</strong></span><br>
<span class="tax_amount">' . __("Sales Tax 13%:") . ' <strong>' . wc_price($tax_amount) . '</strong></span><br>
<span class="inc-vat-price">' . __("Total:") . ' <strong>' . wc_price($price_incl_tax) . '</strong></span>';
} else {
$data['price_html'] .= '<span class="regular-price"> ' . wc_price( wc_get_price_to_display( $variation ) ) . '</span>';
}
return $data;
}
Code goes in functions.php file of your active child theme (or active theme). It should better works.

Related

WooCommerce: Add CSS class to HTML elements returned by function in filter hook

I'm trying to change this function so the price ex. and inc. TAX get a class, so it is possible to style them:
https://www.businessbloomer.com/woocommerce-prices-inc-and-ex-tax/
add_filter( 'woocommerce_get_price_suffix','bbloomer_add_price_suffix_price_inc_tax', 99, 4 );
function bbloomer_add_price_suffix_price_inc_tax( $suffix, $product, $price, $qty )
{
$suffix = ' <small>ex. TAX</small> - '
. wc_price( wc_get_price_including_tax( $product ) )
. ' <small>inc. TAX</small>';
return $suffix;
}
You need to wrap the whole thing into another element
function bbloomer_add_price_suffix_price_inc_tax( $suffix, $product, $price, $qty )
{
$suffix = '<span class="price_including_tax">'
. '<small>ex. TAX</small> - '
. wc_price( wc_get_price_including_tax( $product ) )
. '<small>inc. TAX</small>'
. '</span>';
return $suffix;
}
And in your css:
.price_including_tax { color: green }

Displaying Additional Currencies on WooCommerce Product Page based on Product Price

I realized that using wc_price is not a good way to go since it affects everything else in the system. So, I was hoping to make this work only on the product page without affecting the cart, checkout, minicart, admin orders, and everything else...
So, based on Adding Additional Currencies to Product Price using wc_price Filter Hook - This is my attempt in making that happen. The problem is; nothing is shown.
add_filter( 'woocommerce_single_product_summary', 'manual_currency_converter', 5, 1);
function manual_currency_converter($price) {
$product = wc_get_product();
$product_price = $product->get_price();
// EUR
$conversion_rate_eur = (float) 1.25;
$symbol_eur = 'EUR';
$currency_symbol_eur = get_woocommerce_currency_symbol($symbol_eur);
$euro_price = (float) $product_price * $conversion_rate_eur;
// US dollar
$conversion_rate_us = (float) 0.85;
$symbol_us = 'USD';
$currency_symbol_us = get_woocommerce_currency_symbol($symbol_us);
$us_price = (float) $product_price * $conversion_rate_us;
// GP brittish pound
$conversion_rate_gbp = (float) 1.35;
$symbol_gbp = 'GBP';
$currency_symbol_gbp = get_woocommerce_currency_symbol($symbol_gbp);
$gbp_price = (float) $product_price * $conversion_rate_us;
$exchange_rate_section = '
<div class="exchange-rate-wrapper">
<div class="exchanged-price">
<h2>' . number_format( $euro_price, 2, '.', '' ) . ' ' . $currency_symbol_eur . '</h2>
</div>
<div class="exchanged-price">
<h2>'. number_format( $us_price, 2, '.', '' ) . ' ' . $currency_symbol_us . '</h2>
</div>
<div class="exchanged-price">
<h2>' . number_format( $gbp_price, 2, '.', '' ) . ' ' . $currency_symbol_gbp . '</h2>
</div>
</div>';
return $price . '<br>' . $exchange_rate_section;
}
You can still use the wc_price filter hook, if you want the code to run on the single product page only then use is_product() at the beginning of your function
So you get:
function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $return;
// else continue.. my other code..
return $return;
}
add_filter( 'wc_price', 'filter_wc_price', 10, 5 );

Manipulate woocommerc price in cart

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

Show custom fields on the order editing page in WooCommerce

Based on "Show woocommerce product custom fields under the cart and order item name" answer to one of my previous questions, The code shows custom fields on the product edit page. When these fields are filled in, data is displayed on the product page.
Here is the code:
// Backend: Display additional product fields
add_action('woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data');
function add_fields_to_options_general_product_data() {
// Custom Weight Field
woocommerce_wp_text_input(array(
'id' => '_custom_weight',
'label' => __('Weight dishes', 'woocommerce'),
));
// Calories Field
woocommerce_wp_text_input(array(
'id' => '_сalories',
'label' => __('Calories', 'woocommerce'),
));
// Ingredients Field
woocommerce_wp_textarea_input(array(
'id' => '_ingredients',
'label' => __('Ingredients', 'woocommerce'),
));
}
// Backend: Save the data value from the custom fields
add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values');
function save_admin_product_custom_fields_values($product) {
// Save Custom Weight Field
if (isset($_POST['_custom_weight'])) {
$product->update_meta_data('_custom_weight', sanitize_text_field($_POST['_custom_weight']));
}
// Save Calories Field
if (isset($_POST['_сalories'])) {
$product->update_meta_data('_сalories', sanitize_text_field($_POST['_сalories']));
}
// Save Ingredients Field
if (isset($_POST['_ingredients'])) {
$product->update_meta_data('_ingredients', sanitize_textarea_field($_POST['_ingredients']));
}
}
// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25);
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25);
function display_custom_meta_field_value() {
global $product;
if ($custom_weight = $product->get_meta('_custom_weight'))
echo '<p id="value-on-single-product">'.__("Weight:", "woocommerce").
' '.$custom_weight.
'g'.
'</p>';
if ($сalories = $product->get_meta('_сalories'))
echo '<p id="value-on-single-product">'.__("Calories:", "woocommerce").
' '.$сalories.
' kcal.'.
'</p>';
if ($ingredients = $product->get_meta('_ingredients'))
echo '<p id="value-on-single-product">'.__("Ingredients:", "woocommerce").
' '.$ingredients.
'</p>';
}
// Add custom fields values under cart item name in cart
add_filter('woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3);
function custom_cart_item_name($item_name, $cart_item, $cart_item_key) {
if (!is_cart())
return $item_name;
if ($value1 = $cart_item['data']->get_meta('_custom_weight')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Weight", "woocommerce").
':</strong> '.$value1.
'g</span>';
}
if ($value2 = $cart_item['data']->get_meta('_сalories')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Calories", "woocommerce").
':</strong> '.$value2.
'kcal</span>';
}
if ($value3 = $cart_item['data']->get_meta('_ingredients')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Ingredients", "woocommerce").
':</strong> <br>'.$value3.
'</span>';
}
return $item_name;
}
// Display custom fields values on orders and email notifications
add_filter('woocommerce_order_item_name', 'custom_order_item_name', 10, 2);
function custom_order_item_name($item_name, $item) {
$product = $item - > get_product();
if ($value1 = $product->get_meta('_custom_weight')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Weight", "woocommerce").
':</strong> '.$value1.
'g</span>';
}
if ($value2 = $product->get_meta('_сalories')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Calories", "woocommerce").
':</strong> '.$value2.
'kcal</span>';
}
if ($value3 = $product->get_meta('_ingredients')) {
$item_name. = '<br><span class="custom-field"><strong>'.__("Ingredients", "woocommerce").
':</strong> <br>'.$value3.
'</span>';
}
return $item_name;
}
How to display custom fields on the edit order page in the admin panel? It is supposed to show these fields after the product name.
UPDATE
As I understand it, woocommerce_checkout_create_order_line_item is responsible for this. But I can't add the correct code. I ask for your help!
// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product', 10, 4 );
function save_order_item_product( $item, $cart_item_key, $values, $order ) {
$product = $item->get_product();
if( $value1 = $product->get_meta('_custom_weight') ) {
$item_name .= '<br /><span class="custom-field"><strong>' . __("Weight dishes", "woocommerce") . ':</strong> ' . $value1 . ' g</span>';
}
return $item_name;
}
or
// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product', 10, 4 );
function save_order_item_product( $item, $cart_item_key, $values, $order ) {
if( isset($values['_custom_weight']) ) {
$key = __('Weight dishes', 'woocommerce');
$value = $values['_custom_weight'];
$item->update_meta_data( $key, $value );
}
}
Here is the way to display your product custom fields on admin order items:
add_action( 'woocommerce_before_order_itemmeta', 'add_admin_order_item_custom_fields', 10, 2 );
function add_admin_order_item_custom_fields( $item_id, $item ) {
// Targeting line items type only
if( $item->get_type() !== 'line_item' ) return;
$product = $item-> get_product();
$value1 = $product->get_meta('_custom_weight');
$value2 = $product->get_meta('_сalories');
$value3 = $product->get_meta('_ingredients');
if ( ! empty($value1) || ! empty($value2) || ! empty($value3) ) {
echo '<table cellspacing="0" class="display_meta">';
if ( ! empty($value1) ) {
echo '<tr><th>' . __("Weight", "woocommerce") . ':</th><td>' . $value1 . 'g</td></tr>';
}
if ( ! empty($value2) ) {
echo '<tr><th>' . __("Calories", "woocommerce") . ':</th><td>' . $value2 . 'kcal</td></tr>';
}
if ( ! empty($value3) ) {
echo '<tr><th>' . __("Ingredients", "woocommerce") . ':</th><td>' . $value3 . '</td></tr>';
}
echo '</table>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Related:
Display product custom fields on WooCommerce Admin Order Items also for variable products

Woocommerce Show only one price for variable product on discount

Here is my WooCommerce website: sweetworldcandy.com
The issue is, in variable product price the least and the max
value is showing what I want is if the product is not on sale show the least value if it is on sale show the least value and the least value of offer price by adding a slash as delete tag
I shared this three images below for reference:
2020 Update (for Woocommerce 3+)
Replaced deprecated function woocommerce_price() by wc_price() since Woocommerce 3+:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
}
return $price;
}
Original thread (before WooCommerce 3): The code below should do what you expect:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . woocommerce_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . woocommerce_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories