Woocommerce price after discount - php

I am not expert on WooCommerce, i cant find how to display price that calculate on it discount from coupon.
This code, display price after coupon, but also with shipping price - what i dont want, i want to display price after discount, before shipping and tax:
$woocommerce->cart->get_cart_total();
This code display the finnal price without discount and without shipping:
$woocommerce->cart->get_cart_subtotal();
So i tried something like that to get price after discount:
<?php
$totalp = $woocommerce->cart->get_cart_subtotal();
$totaldisc = $woocommerce->cart->get_total_discount();
$resultp = $totalp - $totaldisc;
echo ($resultp);
?>
But it show "0" number.
So, i guess there is simple function for what i want (i want to get total cart price after discount, before shipping), but i cant find this function.

Answer: i was need to use int's or floats without symbols, and the best way was to check WooCommerce docs here:
Woo - Docs , and to search for this variable and use it without () (not function). now it work :)
<?php
$first_number = $woocommerce->cart->subtotal;
$second_number = $woocommerce->cart->discount_total;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>

Since you have the currency in the prices, you'll need to remove them.
I couldn't find WooCommerce code to just get the subtotal & discount without currency symbol, so let's just remove them manually:
function remove_currency($price) {
return (double) preg_replace('/[^0-9\.]+/', '', $price);
}
$totalp = remove_currency($woocommerce->cart->get_cart_subtotal());
$totaldisc = remove_currency($woocommerce->cart->get_total_discount());
$resultp = $totalp - $totaldisc;
echo $resultp;

This post is a bit old but I would like to inform anyone viewing that Woocommerce 3.x now uses:
WC()->cart
or you can use just
$cart
as long as you have the name_of_my_function( $cart ){} parameter callable.
Everything above is fine... I'm sure yet in hopes someone is not aware that $woocommerce global is no long in use.
There is also a way to loop foreach through the cart items (objects) to pick your price sub-totals using the cart_object.
foreach( $cart_obj->get_cart() as $key=>$value )
{
$priced = $value['subtotal'];
$new_price = $priced - ($predefined_referral_discount * 1);
$value['data']->set_price( $new_price );
}
Remember: name_of_my_function( $cart_obj ){} for this one.
Happy Coding

Related

Recalculate Taxes in Woocommerce

I got a Problem. In Woocommerce I added a custom product type (abo), where the user can choose a area size (in square meters) and there is a price per square meter and a dosage information where it says e.g. "You need 0.165kg per squaremeter" (it´s a fertilizer).
So for example if the user chooses 100 square meters and the price per square meter is 2.30€ and the dosage is 0.165kg/sqm the total price of the abo is 37.95€ and at a tax rate of 10.7% the tax amount should be 4.06€.
My Problem now is, I have normal products in the shop as well, where the standard calculation of the price and tax etc. works perfectly fine and you can buy the abo and a normal product.
So my question is, how can I recalculate the tax amount if I got an abo in cart?
I tried a few things for testing so far:
Adding a custom fee (that isn´t what I´m looking for, cause the tax has to be the same on cart, checkout and receipt):
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if(current_user_can('administrator')) :
$percent = 10.7;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
endif;
}
calc_tax function (I think this one is the one I need) :
/ define the woocommerce_calc_tax callback
function filter_woocommerce_calc_tax( $taxes, $price, $rates, $price_includes_tax, $suppress_rounding ) {
if(current_user_can('administrator')) :
foreach(WC()->cart->cart_contents as $item) :
if($item['data']->get_type() == 'abo') :
$qty = $item['quantity'];
$ppqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_price_per_kg')[0]);
$kgpqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_kg_qm')[0]);
$realQty = floatval($kgpqm) * floatval($qty);
$priceCustom = $priceCustom + $realQty * floatval($ppqm);
endif;
endforeach;
return $taxes;
};
// add the filter
add_filter( 'woocommerce_calc_tax', 'filter_woocommerce_calc_tax', 10, 5 );
With the above function I got the problem, that I don´t have the cart contents directly so it could be, that the order of the items isn´t the same as the Tax order which I get from the function.
I would also note, that the woocommerce_calc_tax fires twice. I had for debug reasons an output in the console and if I have 2 items in cart, I got 4 outputs.
Hope my Problem is clear and that anyone could help me with that problem.
Thanks in advance.
I would look into the tax class for WooCommerce. You can specific target a item with a different tax rate. Unless I'm missing something this should actually work how you need it to out of the box.
When checking out it will also separate the taxes.

How to get sum of shipping and payment method fees in WooCommerce

I'm using WooCommerce for my e-commerce store and I'd like to know if there is some option, how to get a total price (fee) of shipping and total price (fee) of payment methods and their sum on Checkout page. (shipping fee + payment fee = result). I found out how to get a shipping fee without currency symbol ($), which is what I was looking for. But I can't find out the solution how to get payment fee (total), because sometimes when you use Cash-on-delivery tax, you need to get sum of all payment method fees. And so on I need to get sum of shipping fees and payment method fees.
For now I got something like the code, which is mentioned below, but it's not as good as I need.
<?php echo $shipping_price = WC()->session->get('cart_totals') €['shipping_total'];?>
<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>,
<?php $checkout_fee = wc_cart_totals_fee_html( $fee );
echo $checkout_fee;
?>
<?php endforeach; ?>
With the code above I get both values, but one is without currency symbol and the second one is with currency symbol and also placed in foreach loop. So I can't get a sum of them.
If anybody could help, I'd be very grateful.
Thanks.
Instead you could use directly some related WC_Cart methods, to get what you expect, like:
<?php
$shipping_total = WC()->cart->get_shipping_total() + WC()->cart->get_shipping_tax();
$fees_total = WC()->cart->get_fee_total() + WC()->cart->get_fee_tax();
// Output formatted total
echo '<p>Total shipping and fees: ' . wc_price( $fees_total + $shipping_total ) . '</p>';
?>

Can I change the order WooCommerce Coupons are applied programmatically?

I am developing a Woocommerce shop and want to offer all members (users logged in) 20% off their Total.
I have created a coupon called "twentypercent_off" and I am programmatically applying this using a custom function and a hook in my functions.php.
This works well.
In WC settings I have checked "Calculate coupon discounts sequentially" and this works almost as I would expect.
However what I would like is that my "twentypercent_off" is always applied last in cases where there are multiple coupons.
No matter what I have attempted to do (loop through all coupons on current order, remove all coupons, add them back in the order I desire) I find that Woo-commerce seems to always apply them in the order of monetary savings.
For example if the customer orders $100 worth of products and they have a coupon for $25 and also qualified to receive 20% off total.
I would like the total to calculate as (100 -25) * 0.8 = $60
but WC is calculating as such (100 * 0.8) - 25 = $55
I tried the following but it made no difference:
if ( is_user_logged_in() ) {
$Coupon_code = 'twentypercent_off';
$Current_Coupons = WC()->cart->get_coupons();
$Non_Prog_Coupons=array();
// loop through all current coupons
foreach( $Current_Coupons as $code => $coupon ){
if($code==$coupon_code){
WC()->cart->remove_coupon( $code );
} else {
WC()->cart->remove_coupon( $code );
$Non_Prog_Coupons[]=$code; // array of non-programmatically added coupons
}
}
// loop through non-programmatically added coupons
foreach( $Non_Prog_Coupons as $x => $code ){
WC()->cart->add_discount( $code ); // add them again
}
// add programmatically added coupon last !
WC()->cart->add_discount($Coupon_code);
}
return;
The only possible solution I have come across is to apply the 20% discount as a negative fee, which is not suitable for our purposes and would be an absolute last resort
Any ideas on how to achieve this would be greatly appreciated

Custom price per month on product page in Woocommerce

Hello dear Stackoverflow. Long time reader, first time writer.
We are running a webstore (with Woocommerce on Wordpress) with high-end prices and of course then we have installment plans. I like to show this on the product page itself (namely before "add to cart form"). And I made this simple custom code which should calculate month-to-month price, however what I receive on the page is a stop in the code before "add to cart form" so there is an error here, but I cannot see it.
Is there anybody out there who can help me or point me in a direction? Really appreciate it!
By the way, no interest rate when the installment plan is 12 months or less. That's why I did not include interest rate.
(Code has been edited to reflect solution to the problem)
// Price per month
add_action( 'woocommerce_before_add_to_cart_form', 'price_per_month', 5 );
function price_per_month() {
global $product
$price = $product->get_price();
$price = $price + 295;
$months = 12;
$fee = 39;
//$register_fee = 295;
$price_per_month = ($price / $months);
$total_per_month = $price_per_month + $fee;
echo "
<div id='ppm-container'>
<div id='ppm-text'>
Pay from only ". $total_per_month ." NOK per month.
</div>
</div>
";
}
Few things
First you need to have
global $product
Second,
Use WP_DEBUG in order to see errors
Why are you writing add_action before defining the function price_per_month?

How to add conditional price mark up in woocommerce?

I have been looking for this for a long time and can't seem to find an answer
I want to add mark up to my products based on their current price
e.g Products b/w £0-49 I want to £5
49-100 £10 and so on
But i want the markup to be part of the price displayed on shop and product page. (As In I don't want customer to see that I have added a markup)
any help will be appreciated.
Cheers
You can filter all the prices via woocommerce_get_price. Assuming that you want to add £5 for every £50 you could do something like:
function so_32352745_filter_price( $price ){
$factor = floor( $price / 50 );
$price = $price + $factor * 5;
return $price;
}
add_filter( 'woocommerce_get_price', 'so_32352745_filter_price' );

Categories