Woocommerce Include Tax on Cart but not on checkout - wordpress

Iv setup my Woocommerce Site such that the prices are entered excluding tax.
However id like to show Prices Inclusive of tax on the cart and Product Pages but excluding on the Checkout Page.
The Issue is, Woocommerce by default allows you to select Includes/Excludes tax For Cart & Checkout Together. but I need them to display individually.
Any ideas?

Use the filter woocommerce_get_price_including_tax on the single product pages. I copied the code out of the function get_price_including_tax. I haven't tested this code but this is the basic idea of what you need to do.
function modify_woocommerce_get_price_including_tax( $price, $qty, $product ) {
if( ! is_checkout() ) {
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
$base_tax_rates = WC_Tax::get_base_tax_rates( $product->tax_class );
if ( ! empty( WC()->customer ) && WC()->customer->is_vat_exempt() ) {
$base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true );
$base_tax_amount = array_sum( $base_taxes );
$price = round( $price * $qty - $base_tax_amount, wc_get_price_decimals() );
/**
* The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations.
* e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes.
* product feature is experimental #since 2.4.7 and may change in the future. Use at your risk.
*/
} elseif ( $tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
$base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true );
$modded_taxes = WC_Tax::calc_tax( ( $price * $qty ) - array_sum( $base_taxes ), $tax_rates, false );
$price = round( ( $price * $qty ) - array_sum( $base_taxes ) + array_sum( $modded_taxes ), wc_get_price_decimals() );
} else {
$price = $price * $qty;
}
}
return $price;
}
add_filter( 'woocommerce_get_price_including_tax', 'modify_woocommerce_get_price_including_tax', 10, 3 );
If you want to modify the prices on the cart page then you'll need to override the template cart-totals.php in your child theme.

Related

Issue with changing WooCommerce cart item prices based on product meta

When the product subtotal is more than 3000, it has to be another price.
I added it as a meta field to products. On Stack Overflow, a long time ago someone advised me the filter for a special price called woocommerce_cart_item_name
I modified it a little for my purpose:
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$threshold = 3000; // Change price if > 3000
$_product = $cart_item['data'];
$meta = $_product->get_meta( '_opt_price_var');
$price = $_product->get_regular_price();
$subtotal = $cart_item['quantity'] * $price;
if ( $subtotal > $threshold && ( $meta )) {
$_product->set_price( $meta );
}
echo $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
The special price is showing only in the cart form, but not in cart totals and not in the checkout, in totals and in checkout the price is still regular, the main calculator doesn't take it from this filter.
How can I fix it? How to include it in the main calculator too?
I tried different hooks that can be responsive for the calculator - both add_action and add_filter, with and without 10, 2 - it isn't it.
add_action('woocommerce_before_shipping_calculator', 'kia_add_subtitle_to_cart_product',10,2);
add_action('woocommerce_before_cart_totals', 'kia_add_subtitle_to_cart_product', 10, 2 );
The woocommerce_cart_item_name hook is being misused in your code attempt. A filter hook should always return something compared to the use of echo.
You should use the woocommerce_before_calculate_totals hook instead
So you get:
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Change price if > 3000
$threshold = 3000;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get regular price
$price = $cart_item['data']->get_regular_price();
// Subtotal = Get quantity * price
$subtotal = $cart_item['quantity'] * $price;
// Greater than
if ( $subtotal > $threshold ) {
// Get meta
$meta = $cart_item['data']->get_meta( '_opt_price_var', true );
// NOT empty
if ( ! empty ( $meta ) ) {
// Set new price
$cart_item['data']->set_price( $meta );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
add_filter('woocommerce_calculated_total', 'custom_calculated_total', 10, 2);
function custom_calculated_total($total, $cart_object) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if (!WC()->cart->is_empty()):
foreach ($cart_object->cart_contents as $key => $cart_item) {
$meta = $cart_item['data']->get_meta('_opt_price_var');
if ($total >= 3000 && ( $meta )) {
$total = $meta;
}
}
endif;
return $total;
}

WooCommerce change price when adding product to order in admin

We have customers who have different discount percentages. This is all programmed and working on the front end when adding products to the cart, but if using the back end order admin to add a new product I can't seem to find a way to calculate the new product price based on the user's discount. Is there a way to change the price in the woocommerce_new_order_item hook when adding a product to an existing order in the order admin?
Here's what I have so far:
function action_woocommerce_new_order_item( $item_id, $item, $order_id ) {
// only run this from the WP admin section
if ( !is_admin() )
return;
$item_type = $item->get_type();
// return if this is not a product (i.e. fee, tax, etc.)
if ( $item_type != 'line_item' )
return;
$product = wc_get_product( $item->get_product_id() );
if ( !$product )
return;
$current_price = $product->get_price();
$quantity = $item->get_quantity();
// here I get the order's user's discount percentage and calculate the new discounted price
// custom function
$discounted_price = get_discounted_price( $current_price, $users_discount );
$new_price = ( !empty($discounted_price) ) ? $discounted_price : $current_price;
// this doesn't work
$item->set_price( $new_price );
// and this doesn't work
$product->set_price( $new_price );
// this appears to work but I'm not sure if this the best way to accomplish this
$item->set_total( $price * $quantity );
}
add_action( 'woocommerce_new_order_item', 'action_woocommerce_new_order_item', 10, 3 );
Any help would be greatly appreciated!
Thanks
This works for adding item meta and changing the item price in the order admin screen:
function action_woocommerce_new_order_item( $item_id, $item, $order_id ) {
// only run this from the WP admin section
if ( !is_admin() )
return;
$item_type = $item->get_type();
// return if this is not a product (i.e. fee, tax, etc.)
if ( $item_type != 'line_item' )
return;
$product = wc_get_product( $item->get_product_id() );
if ( !$product )
return;
$current_price = $product->get_price();
$size = $product->get_attribute('size');
// add custom item meta to order
if ( $size ) {
wc_add_order_item_meta( $item_id, 'Size', $size , false );
}
$order = wc_get_order( $order_id );
$order_user = $order->get_user();
$order_user_id = $order->get_user_id();
// get this order's user's discount %
$users_discount = get_user_meta( $order_user_id, 'discount', true );
$users_discount = ( $users_discount > 0 ) ? $users_discount : 0;
// calculate the discounted price based on the user's discount (custom function)
$discounted_price = get_discounted_price( $current_price, $users_discount );
$quantity = $item->get_quantity();
$new_price = ( !empty($discounted_price) ) ? $discounted_price : $current_price;
$item->set_total( $new_price * $quantity );
};
add_action( 'woocommerce_new_order_item', 'action_woocommerce_new_order_item', 10, 3 );

Update Order Programmatically Doesn't Update Totals

I'm trying to update the quantities of an ordered product and when doing so I would like the order to reflect the actual cost. What I find happens is that the Product Cost is reduced to match the total and the order total is never actually updated. I've supplied a simple sample below:
function prefix_update_woo_order() {
$order_id = 123; // This needs to be a real order or there will be errors
$order_item_id = 5; // This needs to be a real order item ID or there will be errors.
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
$order_items[ $order_item_id ]->set_quantity( 2 );
$order->calculate_taxes();
$order->calculate_totals();
$order->save();
}
add_action( 'admin_init', 'prefix_update_woo_order' );
For example, a "Beanie with Logo" product is on sale for $18.00 and I originally buy 1. I want to programmitically update the order item to a quantity of 2 instead of 1 after the order has been placed. I would expect the total to be $36.00 but what I'm finding is that the product cost changes to match the total price. Instead of a cost of $18.00 for a "Beanie with Logo" the quantity is updated to 2 and the cost is reduced to $9.00ea.
In short, what I want to do is update an existing order items quantity and have the totals updated to reflect the new quantity cost, discount, taxes. What methods do I need to use to achieve this?
Hello I think this code will change your problem
add_action( 'admin_init', 'test_order_update_order' );
function test_order_update_order() {
$order_id = 80; // This needs to be a real order or there will be errors
$order_item_id = 11; // This needs to be a real order item ID or there will be errors.
$quantity = 2; //quantity which you want to set.
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
foreach ( $order_items as $key => $value ) {
if ( $order_item_id == $key ) {
$product_value = $value->get_data();
$product_id = $product_value['product_id'];
}
}
$product = wc_get_product( $product_id );
$price = $product->get_price();
$price = ( int ) $quantity * $price;
$order_items[ $order_item_id ]->set_quantity( 2 );
$order_items[ $order_item_id ]->set_subtotal( $price );
$order->calculate_taxes();
$order->calculate_totals();
$order->save();
}
i have not tried what you are trying to achieve before. What i see in your code is that $order_items is an array of Items Objects produced from WC_Order::get_items(), but i dont see the WC_Order Instance being notified that the order items have changed. I would expect a method like $order->update_cart($order_items); I believe i found some usefull links for more research
https://hotexamples.com/examples/-/WC_Order/-/php-wc_order-class-examples.html
woocommerce - programmatically update cart item quantity
Sorry, i was not much of help!
Use the follows code snippet to do the above task -
function prefix_update_woo_order() {
$order_id = 123; // This needs to be a real order or there will be errors
$order_item_id = 5; // This needs to be a real order item ID or there will be errors.
$order = wc_get_order( $order_id );
if( $order && !wc_get_order_item_meta( $order_item_id, '_order_item_data_updated', true ) ) {
$item_price = wc_get_order_item_meta( $order_item_id, '_line_total', true );
$updated_item_quantity = 2;
wc_update_order_item_meta( $order_item_id, '_qty', $updated_item_quantity );
wc_update_order_item_meta( $order_item_id, '_line_total', $item_price * $updated_item_quantity );
$order->calculate_totals();
$order->save();
// set flag
wc_add_order_item_meta( $order_item_id, '_order_item_data_updated', true, true );
}
}
add_action( 'admin_init', 'prefix_update_woo_order' );
Codes goes to your active theme's functions.php
This is what I've come up with. I've tried to use the wc_format_decimal() where applicable. Seems like a lot of work to simply update an order items quantity but it is what it is.
The bottom comment is unnecessary but if you're using the Cost of Goods plugin then that will take care of that.
/**
* Update the order item quantity and totals
* #param Integer $order_id
* #param Integer $order_item_id
* #param Integer $quantity - Quantity to set
*
* #return void
*/
function prefix_update_woo_order( $order_id, $order_item_id, $quantity ) {
// Get Order, Item, and Product Data
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
$line_item = $order_items[ $order_item_id ];
$variation_id = $line_item->get_variation_id();
$product_id = $line_item->get_product_id();
$product = wc_get_product( $variation_id ? $variation_id : $product_id );
$quantity_old = $line_item->get_quantity();
// Calculate Old and New Discounts
$discount = wc_format_decimal( $line_item->get_subtotal() - $line_item->get_total(), '' );
if( ! empty( $discount ) ) {
$discount_per_qty = wc_format_decimal( ( $discount / $quantity_old ), '' );
$discount = wc_format_decimal( ( $discount_per_qty * $quantity ), '' );
}
// Set Quantity and Order Totals
$line_item->set_quantity( $quantity );
$total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) ); // Also see `wc_get_price_excluding_tax()`
$line_item->set_subtotal( $total ); // Without Discount
$line_item->set_total( $total - $discount ); // With Discount
// Save Everything
$line_item->save();
wc_save_order_items( $order_id, $order_items );
/**
* If using the 'Cost of Goods' Plugin
* - - - - - -
* $cog = $line_item->get_meta( '_wc_cog_item_cost', true );
* $new_cog = wc_format_decimal( ( $quantity * $cog ), '' );
* $line_item->update_meta_data( '_wc_cog_item_total_cost', $new_cog );
* wc_cog()->set_order_cost_meta( $order_id, true );
*/
}

How to change woocommerce price according to user input

I have used the hook woocommerce_before_calculate_totals but stuck in getting dynamic value in my function. Even i have used session but it didn't work on passing dynamic price.
The following snippet adds 100 to all the products in the cart, you can put your own conditions and pricing calculation there.
function calculate_product_price( $cart_object ) {
/* Gift wrap price */
$additionalPrice = 100;
foreach ( $cart_object->cart_contents as $key => $value ) {
//You can add your condition here
$quantity = floatval( $value['quantity'] );
$orgPrice = floatval( $value['data']->price );
$value['data']->price = ( ( $orgPrice + $additionalPrice ) * $quantity );
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_product_price', 1, 1 );

Add fee to woocommerce cart

I would like to add a fee to my cart when the customer has a specific billing country. For example Belgium (BE)
I found this code to add a fee by default.
Could anyone help me with an IF formula or something, so that it is only applied when the billing country = BE ?
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
This is what you need. Just add BE to the $county array (I just did that for you in the code below).
/**
* Add a 1% surcharge to your cart / checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*
* Change $percentage to set the surcharge to a value to suit
*
* Add countries to array('BE'); to include more countries to surcharge
* http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes
*
* Change in_array to !in_array to EXCLUDE the $countries array from surcharges
*
* Uses the WooCommerce fees API
* Add to theme functions.php
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('BE');
$percentage = 0.01;
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
endif;
}
source: https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/

Resources