Hello to all!
I'm new here and already have my first question.
i have a woocommerce project where i need the single product prices with 3 decimals - and all other prices (subtotal, total, tax) should be with 2 decimals (rounded) - also in the e-mails this prices should have 2 decimals.is there a simple function for this or a work around? Thanks for the help!
Stefan
) Just found a way and wanted to share with you:
Select in the woocommerce settings the 2 decimals for the shop.
then add this to the function.php of your theme:
add_filter( 'wc_get_price_decimals' , 'custom_price_decimals', 20, 1 );
function custom_price_decimals( $decimals ) {
if( is_product() || is_shop() || is_product_category() || is_product_tag() )
$decimals = 3;
return $decimals;
}
Related
This question already has an answer here:
Display total customers reviews and ratings average in WooCommerce
(1 answer)
Closed 4 years ago.
I have this code in a plugin (custom dashboard widget plugin) and the output displays in a Dashboard widget. If someone submits a product review, the error goes away. But I would like know how to make the error go away when there are no product reviews.
I am using the code from:
Display total customers reviews and ratings average in WooCommerce
The error is:
Warning: Division by zero
and it refers to this line:
return '<p class="rating-average"><b>'.round($average / $total_count, 1).' / 5 </b>'. __('Rating Stars Average').'</p>';
The output displays as: NAN / 5 Rating Stars Average
Any ideas or suggestions on how to fix this?
Use an if and check to see if $total_count === 0:
function products_rating_average_html(){
$stars = 1;
$average = 0;
$total_count = 0;
foreach( get_products_ratings() as $values ){
$average += $stars * $values->count;
$total_count += $values->count;
$stars++;
}
if ($total_count === 0) {
return 'No Reviews';
}
return '<p class="rating-average"><b>'.round($average / $total_count, 1).' / 5 </b>'. __('Rating Stars Average').'</p>';
}
I'm trying to add to an existing fee, but when I use:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$cartfee = $woocommerce->cart->get_fees();
$stripefee = 0.015;
$perorder = 0.20;
$surcharge1 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $stripefee;
$bookingfee = $surcharge1 + $perorder + $cartfee;
$woocommerce->cart->add_fee( 'Booking Fee', $bookingfee, true, '' );
}
There is already a Booking Fee of 0.30 that displays fine on the cart, when I add the above it does the $surcharge1 + $perorder but the $cartfee is empty. It doesn't seem to read the existing fee. The Booking fee is from a plugin that I can set a % per product.
Any help to point me in the right direction would be great.
What's kind of value $woocommerce->cart->get_fees() this one returns (your $cartfee)? Seems like it should be an array. Did you check for an empty array?
I have created a link in the header of my site to the cart with the current totals.
It is supposed to show the current total of the cart and the number of items contained in the cart, in this manner :
[icon] 1.20 € - 1 article
However, the value is only correctly calculated when in the cart page. On the homepage, for example, it will only display like this :
[icon] 0.00 € - 1 article
This is how I am accessing the values :
$count = WC()->cart->cart_contents_count;
WC()->cart->calculate_totals();
if($count > 0)
{
print "<a class='cart-contents' href='" . WC()->cart->get_cart_url();
print "' title='Voir votre panier'>";
print WC()->cart->get_total();
print " - " . sprintf(_n('%d article', '%d articles', $count, 'woothemes'), $count);
print "</a>";
}
If I call WC()->cart->get_cart_total();, this will show the price exclusive of tax, even if I force prices include tax.
How can I get the correct value consistently across the site ?
global $woocommerce;
$amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;
You can also convert $amount in float value as per your requirement.
I had the same issue so I went through the WooCommerce code. In the function WC()->cart->calculate_totals() in the class WC_Cart since some time ago there is a following condition
// Only calculate the grand total + shipping if on the cart/checkout
if ( is_checkout() || is_cart() ||
defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') )
This condition applies to calculating specifically the total, I'm not sure why. But this gives you the proper workaround:
On the page where you need to show the total include either one of the following lines of code:
define( 'WOOCOMMERCE_CART', true );
or
define( 'WOOCOMMERCE_CHECKOUT', true );
And then perform the function WC()->cart->calculate_totals();
Then to get the cart total use either WC()->cart->total or wc_cart_totals_order_total_html() (for currency symbol)
Is there a way to change variable product default stock message?
My goal is to check how many items are in stock, if there is more than 20 items it should display "More than 20 available" and if it is under 20 items, it says exactly how much items are left (15 items left)
Is it doable?
I have googled the hell out of it but no luck.
add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $_product ) {
if ( $_product->product_type == 'variation' && $_product->manage_stock == "yes" && $_product->stock > 20 ) $availability['availability'] = 'more than 20';
return $availability;
}
Dear I've a Pizza site and I'm using Woocommerce plugin. I want to apply some price check on attributes.
My Question is
I've a Pizza product which attributes are Size and Meat. Size(Small,large) Meat(Chicken, donair meat etc).
I've applied a check on attributes that when a customer select a product size, small and one piece of meat, the price will be $10 on default rate. But I want to apply this logic here that, when a customer select a more than one piece of meat (Extra topping), Then I want to add $2 on total price like ($10 + $2 = 12).
please help me??
Please use this action for adding the extra price on woocommerce cart.Now you need to work on the commented loop code and apply the simple login.Now I have added a summary for how you will work.
I have use it and now it is working on my local system.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$productid = $value['product_id'];
$variationArr =$value['variation']; //it is an array have the value : meat and size i.e chicken and small
//In every loop you can count the number of products .
//Note: In array($variationArr),It have all the active attribute of this product which is created by admin in woocommece.
//so you can search it by loop;
/*
* Like
* foreach($variationArr as $k=>$v)
* {
* //here is the variation product size
* }
*
*/
//Now increase the price.
$price = $value['data']->price;
$extraPrice = 2;
$newPrice = $price + $extraPrice;
$value['data']->price = $newPrice;
}
}