Change number of decimals in Woocommerce checkout and payment page - php

My Woocommerce website doesn't display decimals as I use a currency converter plugin and don't want cents to show.
I wanted to have the possibility to create custom orders (in order to send payment links to customers) with cents. I tried to create an exception for my custom product category.
Here is what I tried:
add_filter( 'wc_get_price_decimals', 'custom_price_decimals', 10, 1 );
function custom_price_decimals($decimals) {
global $product;
if (is_a($product, 'WC_Product') && has_term('voyage-sur-mesure', 'product_cat', $product->get_id()) && is_cart() || is_checkout() || is_account_page()) {
$decimals = 2;
}
return $decimals;
}
The result is that my cents are showing on the payment page but it is rounded on the total line. Yet, I need the cents to be paid.
Product × 1 50,23€
Subtotal : 50,23€
Total : 50,00€
Could someone please help?

Related

WooCommerce Offset Stock levels Based On Custom Field Value Threshold

I need to set a threshold on a per product basis for stock inventory.
Example : If the product has a threshold of 10 and there's 10 or less in stock, the product shows the out of stock text. If the threshold is set at 10 but there's 15 in stock, 5 is shown in stock.
So what i need to do is offset the real inventory levels on a per product basis using a value.
This is the code i'm trying to get working
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = get_post_meta( $product->get_id(), '_stock_threshold', true );
if ( $product->managing_stock() && ! empty( $stock_threshold ) ) {
$availability['availability'] = $product->get_stock_quantity() - $stock_threshold;
return $availability;
}
}
This code doesn't show the Out of Stock text on the single product page if the inventory is calculated at 0 or less than zero.
Example : When the stock level is set at 2 and the offset is set at 2, it = 0 so should show Out of Stock but doesn't.
I think this gets you some of the way there;
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = $product->get_low_stock_amount();
$stock_quantity = $product->get_stock_quantity();
if ( $product->managing_stock() && !empty($stock_threshold) ) {
$avail_stock = ($stock_quantity - $stock_threshold);
if ($avail_stock <= 0){
$availability['availability'] = __('Out of Stock', 'woocommerce');
}
return $availability;
}
}
Basically, we're getting the low stock amount (threshold) and subtracting the stock quantity. If it's 0 or less, then set the availability to Out of Stock.
This works without a problem.
However if the product has 15 in stock and the threshold at 10, it still shows 15 as the out of stock is NOT 0.
I think the best approach is a combination of the above, and then shortcode to display the quantity minus the threshold rather than using the code inside WooCommerce.
Maybe someone else can build off what I have posted above as I couldn't get the quantity number to reduce on products NOT out of stock.

Woocommerce hide out of stock variation price from variable price range

I want to hide out of stock variation price from woocommerce variable products price-range
for example i have product that has 2 variation, one of them sold out but in price range it's still showing old price for sold out variation
i want to show price range only for available (in stock) variations
I searched a lot and did't find any solution
Found this code
add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock() && (is_product() || is_product_category() || is_shop() )) {$price = '';}
return $price;
}
but it's only hide price from product itself, but in price range it's still showing out of stock variation price
Thanks all in advance for help!

WooCommerce doesn't show actual price on cart page without tax

I have build webstore on WooCommerce. I have added standard tax rate when the customer fills their billing address and select country tax will be applied on the checkout page that is working fine I want to show actual price of products on cart page without multiply my tax ratio with my product price
This is checkout page show tax when a country is selected
this is cart page showing tax calculated value and grand total show without tax value
I have added this function in my function.php file but no success
function wc_remove_cart_tax_totals( $tax_totals, $instance ) {
if( is_cart() ) {
$tax_totals = array();
}
return $tax_totals;
}
add_filter( 'woocommerce_cart_tax_totals', 'wc_remove_cart_tax_totals', 10, 2 );
// Show the cart total excluding tax.
function wc_exclude_tax_cart_total( $total, $instance ) {
// If it is the cart subtract the tax
if( is_cart() ) {
$total = round( WC()->cart->cart_contents_total + WC()->cart->shipping_total + WC()->cart->fee_total, WC()->cart->dp );
}
return $total;
}
add_filter( 'woocommerce_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );
add_filter( 'woocommerce_subscriptions_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );

Woocommerce set minimum order for a specific user role

I have a php code that sets a 100 minimum order site wide, that works good.
I want to set a different minimum order of 250 for a specific role 'company' using the same code or by cloning it and wrapping it in an if() statement, only I have no idea how to write it. Would appreciate any kind of help.
This it the currant code (don't mind the Hebrew text, its just the error messages, when the condition is not met):
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 100;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart's total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 10 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>לקוח יקר, יש צורך במינימום הזמנה
של %s %s₪ על מנת לבצע רכישה באתר.</strong>'
.'<br />סכום הביניים בעגלה הינו: %s %s₪',
$minimum_cart_total,
get_option( 'woocommerce_currency_symbol'),
$total,
get_option( 'woocommerce_currency_symbol') ),
'error' );
}
}
}
Thanks!
Try the following using current_user_can(), so in your code:
// Set a minimum amount per order (and user role)
add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' );
function set_min_total_per_user_role() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set minimum cart total
$minimum_cart_total = current_user_can('company') ? 250 : 100;
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// Add an error notice is cart total is less than the minimum required
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Dear customer, minimum order of %s is required to make a purchase on your site.</strong> <br>
Your actual cart amount is: %s',
wc_price($minimum_cart_total),
wc_price($total)
), 'error' );
}
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.
To format prices for display you can use the dedicated wc_price() formatting function.
Related: Apply a discount for a specific user role in Woocommerce
First you have to retrieve the roles of the current user.
$roles = is_user_logged_in() ? (array) wp_get_current_user()->roles : [];
Now you want to check if the role company is in the user's roles to determine the minimum.
$minimum_cart_total = in_array('company', $roles) ? 250 : 100;

Progressive fixed Coupon Discount based on specific product quantity in Woocommerce

I have one little problem that dont know how to fix myself. I want to use this logic into my Woocommerce store just for one product.
I have use link like this to autmatically apply coupon code and add to cart:
https://testsite.com/checkout/?add-to-cart=Product_ID&quantity=1&coupon=Coupon_Code
and this seems to work, when quantity is 1. But i want discount (30%) that is automatically placed when click on direct link from before, to make dynamic, for ex:
Increase quantity to 2 in cart page, and coupon automatically to calculate 2 x 30$ = 60$ discount,
buy 3 from same product, and calculate 3 X 30$ = 90$ coupon discount
and so on..
I searched, and found this usefull thread, but there situation is little different then mine.
How can I make to have a specific coupon? Some advice or start point. Thanks
This is possible with that 2 steps:
1) Add a unique coupon with:
In General settings > Type = Fixed Product discount
In General settings > Amount = 30
In Usage restrictions > Products ==> set your desired product(s)
2) Add this code (where you will set your coupon code in the function (in lowercase)):
add_filter( 'woocommerce_coupon_get_discount_amount', 'custom_coupon_get_discount_amount', 10, 5 );
function custom_coupon_get_discount_amount( $rounded_discount, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('30perqty');
## ------ The code ------- ##
if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) && $cart_item['quantity'] > 1 ) {
if( in_array( $cart_item['product_id'], $coupon->get_product_ids() ) ){
$discount = (float) $coupon->get_amount() * (int) $cart_item['quantity'];
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $rounded_discount;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Categories