I'm using this code:
WC()->cart->subtotal;
to check how much money the customer has to spend until she gets free shipping. The free shipping amount considers coupon codes though so I need to subtract any discount from WC()->cart->subtotal; before displaying the total amount. Any idea?
I've tried to do something like this:
$current = WC()->cart->subtotal - WC()->cart->discounts;
But it isn't doing anything.
Appreciate your help!
If you want display the total amount (subtotal - discount) you can write this :
WC()->cart->total;
So, you can do this for example :
$amount_for_free_shipping= 49;
$cart = round(WC()->cart->total,2);
$remaining = $amount_for_free_shipping - $cart;
Related
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.
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?
i want put an pixel for tracking my orders for affiliate.
I must get my total order after discount, so without Tax and Shipping cost.
I've make something like this but it's display 0 .
<?php echo $woocommerce->cart->get_total_ex_tax(); ?>
It's maybe because it's display currency symbol.
This it the cart total without tax and shipping.
$cart_value = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
Vdadmax's answer is almost correct. If tax is applied on shipping, then it's deducted twice in his case (the total shipping cost including tax is deducted and after that, shipping sales tax is deducted again), leaving you with a final total that is too low.
This gives you the correct total with all sales tax and shipping deducted:
$cart_value = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping(), wc_get_price_decimals(), '.', '' );
I can't comment yet, hence the reason why I am adding this as an answer instead.
This helps me to get the total amount without tax.
WC()->cart->subtotal_ex_tax
Have you tried?
$cart_value = $order->get_total_tax() - $order->get_total();
get_woocommerce_totals()['cart_subtotal']['value']
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
I am trying to apply Shipping Amount after Discounts in Magento checkout
I'm using UPS free shipping by price, so if you total after discount is under the minimum amount for free shipping it will not grant you free shipping.
Magento calculate free shipping before discount so I calculated on my local ups.php file the correct amount after discount and applied it to the code:
$coupon_code=Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode();
Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
Mage::getSingleton('checkout/session')->getQuote()->setCouponCode($coupon_code)->setTotalsCollectedFlag(false)->collectTotals();
//Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->collectTotals()->save();
$totals =Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
$subtotal = $totals["subtotal"]->getValue(); //Subtotal value
if(isset($totals['discount']) && $totals['discount']->getValue()) {
$discount = $totals['discount']->getValue(); //Discount value if applied
} else {
$discount = '';
}
$r->setValue($request->getPackageValueWithDiscount( ) - $giftCardPrice);
if($discount!='') $r->setValueWithDiscount($subtotal-abs($discount));
else $r->setValueWithDiscount($request->getPackageValueWithDiscount( )- $giftCardPrice);
This works my problem now that in every Ajax call for shipping Magento calculates the grandtotal of the cart twice
What can I do?