Woocommerce product detail page remove price decimals - php

I'm customizing Woocommerce plugin, trying to remove decimals from product variation prices in product detail page.
Ex- if someone select option i want to display the regular and sales prices without decimals and the other locations of site like shop, cart, checkout no need to change.
I found this filter but it change prices of whole site.
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
https://snipboard.io/GXgtUi.jpg
anyone have idea or solution about archive this, Thanks a lot

You can try below code snippet into your theme function.php file
add_filter( 'formatted_woocommerce_price', 'ums_remove_zero_decimals', 10, 5 );
function ums_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
if( is_single()) {
if($product->is_type( 'variable' )) {
return (int) round( $price );
}
else {
return ( $formatted_price);
}
}
}
Let me know if this works for you or not.

Related

How I can hide bulk products or Price in WooCommerce?

I am using WooCommerce in WordPress. I have a lot of products with different prices. I just want to hide products which have $35 price bracket. I don't want to do it one by one using editor. I want a solution with code because 5000+ products have $35 price.
You can try with the woocommerce_product_is_visible hook
add_filter( 'woocommerce_product_is_visible', 'hide_35', 10, 2 );
function hide_35( $visible, $id ){
$product = wc_get_product( $id );
if( $product->get_regular_price() == 35 ){
return false;
}
return $visible;
}

Adjust WooCommerce price if product is picked from specific archive page

I'm not sure if this is possible, but it feels like it should be.
I have a list of products that I wish to display via two different archives (set by another plugin) where I can control the products displayed by listing categories with the shortcode. The different archives need to have different prices showing for the same products and sometimes to not list specific products within a category.
So what I've done is setup categories (which are filterable on the front end and as such display their name) with the same name but different slugs for each archive page and then added each product to both categories with the same name so they show up in each archive.
The problem is I need to increase the base price of almost all of the products in one archive by 0.50 and I can't figure out a way of doing it. I can achieve the correct result visually like this:
function bbloomer_alter_price_display( $price_html, $product ) {
global $wp_query;
$slug = basename(get_permalink($wp_query->post->ID));
// Only if not null
if ( '' === $product->get_price() ) return $price_html;
// If on specifc archive page
if ( $slug == "eathere" ) {
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 0.50 );
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
But obviously, this only updates the visual price, and as soon as it's added to the cart and checkout it just shows the base price, unaltered.
I know that you're supposed to update the cart separately with a hook like woocommerce_before_calculate_totals but I can't figure out how to apply the logic that it has to have come from this archive to have the modified price.
This code works for the category they're in:
add_filter( 'woocommerce_product_get_price', 'custom_sale_price_for_category', 10, 2 );
function custom_sale_price_for_category( $price, $product ) {
//Get all product categories for the current product
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
$categories[] = $term->slug;
}
if ( ! empty( $categories ) && in_array( 'eathere-burgers', $categories, true ) ) {
$price *= ( 1 + 0.50 );
}
return $price;
}
But this will then affect the prices across the site regardless of which archive page they're on. If I add that it should only affect the archive page via the slug as in the first example then they appear correctly in the archive but again don't change in the cart of checkout.
I'm really stuck on how to get this working without duplicating the entire product list and adjusting the price. It feels like this should be possible with a simple bit of code but WooCommerce's hooks and filters are so dense in their functionality I'm finding it hard to figure out.
I don't know if the logic is correct or not but you can try with Session Variable in 'woocommerce_product_get_price' filter to store value of archive
session_start();
$_SESSION["archive"] = "eathere";
Now get the same session variable value on cart/checkout Hook 'woocommerce_before_calculate_totals' & don't forgot to
session_start(); & get value of $_SESSION["archive"] in this hook to check archive slug & update the price according to that.
In your code assign slug value to session variable in if & elseif statement based on which archive page you are on
function bbloomer_alter_price_display( $price_html, $product ) {
global $wp_query;
$slug = basename(get_permalink($wp_query->post->ID));
// Only if not null
if ( '' === $product->get_price() ) return $price_html;
// If on specifc archive page
if ( $slug == "eathere" ) {
$_SESSION["archive"] = "eathere";
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 0.50 );
}
elseif ( $slug == some_other_archive_slug ) {
$_SESSION["archive"] = some_other_archive_slug;
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 2 );
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
on Cart/Checkout page compare the value of archive page slug with Session variable slug value & based on that update the price of the product
add_action( 'woocommerce_before_calculate_totals', 'update_price_cart', 9999 );
function update_price_cart( $cart ) {
// LOOP THROUGH CART ITEMS & ADD Price
if ($_SESSION["archive"] == "eathere"){
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price + 0.50 );
}
}
elseif ($_SESSION["archive"] == some_other_archive_slug) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price + 2 );
}
}
Sometimes it may give error in that case you need to start_session(); before assigning value to session

Checkout price issue using woocommerce_product_get_price hook

I need to double the price for every product in Woocommerce frontend. For this I used the following code:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
But there is an error using this code. checkout page price is not correct.
for example the product orginal price is 10. We double the price by this code . so the product price is 20 now. When i added this product to the cart then cart and checkout page price is 40. That means this multiplication is take place in two times.
Please help to solve this.
Updated To double the price:
1) First you will restrict your code to single product and archives pages only:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) Then for cart and checkout pages, you will change the cart item price this way:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and working. It will change all prices as you expect in cart, checkout and order pages…

Role based taxes in woocommerce / Cart page

I've set up a woocommerce store with multiple users (B2C & B2B). Some of them will automatically be exempt from tax and just have tax disappear from the cart/checkout. I've used a dynamic pricing plugin to provide different prices to different roles but there is no options for tax variations.
I found this answer and tried to put it in place Role based taxes in woocommerce but as #Jplus2 is telling, #dryan144 solution is not good because it is only applied during the checkout and not on the cart. I tried to figure out the way to do it but I still do have to refresh my 'cart' page to display taxes to 0 (as they are included in the price for "guest" or "customer", any help to launch the action when my cart page is called?
I did the following:
add_filter( 'woocommerce_before_cart_contents', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_shipping_calculator', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_checkout_billing_form', 'prevent_wholesaler_taxes' );
function prevent_wholesaler_taxes() {
global $woocommerce;
if ( is_user_logged_in() && !(current_user_can('customer'))){
$woocommerce->customer->set_is_vat_exempt(false);
} else {
$woocommerce->customer->set_is_vat_exempt(true);
}
} //end prevent_wholesaler_taxes
It's working straight away sometimes but most of the time it's only after working after a refresh of my cart which is not good.
Try to add https://eshoes.com.au/product/test-shoes08/ to the cart then -> View your basket
Any help would be greately appreciated ;)
Cheers
This solution works perfectly, instead of using set_is_vat_exempt() I simply used $tax)class = 'Zero Rate':
add_filter( 'woocommerce_before_cart_contents', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_before_shipping_calculator', 'wc_diff_rate_for_user', 1, 2);
add_filter( 'woocommerce_before_checkout_billing_form', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
function wc_diff_rate_for_user( $tax_class ) {
if ( !is_user_logged_in() || current_user_can( 'customer' ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}

How to dynamically change the price of a product in Woocommerce - WordPress?

I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:
add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
$product = new WC_Product( get_the_ID() );
$price = $product->price;
echo $price * 0.9;
}
Ok, it works! But when in the checkout the price are normals without the 10% discount!
Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?
New in Woocommerce too.
Your question looks really similiar to this one.
Adding Custom price with woocomerce product price in Cart & Checkout
I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)
I made a slightly modification to the code (changed price formula). I think that may help you.
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price = $price*.09;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}

Categories