Exclude shipping costs from cart's total on Woocommerce cart page - php

Is there any woocommerce hook / method through which I can exclude the shipping cost from the total of the cart?
I searched everywhere but I can't seem to find an answer.

Using this custom function hooked in woocommerce_calculate_totals action hook, you will exclude the shipping cost from the cart totals (just the display in cart page):
// For WooCommerce versions from 2.5.x up to 3+
add_action( 'woocommerce_calculate_totals', 'custom_cart_displayed_totals', 10, 1 );
function custom_cart_displayed_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only on cart page
if ( ! WC()->cart->is_empty() && is_cart() ):
## Get The shipping totals
$shipping_tax_amount = $cart_object->shipping_total;
$shipping_total_excl_tax = $cart_object->shipping_tax_total;
## Displayed subtotal
// $cart_object->subtotal = 0;
## Displayed TOTAL
// $cart_object->total = 0;
## Displayed TOTAL
$cart_object->tax_total -= $shipping_tax_amount;
## Displayed TOTAL
$cart_object->cart_contents_total -= $shipping_total_excl_tax;
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works…

Related

Deposit field ONLY for BACKORDER ITEMS [duplicate]

I've taken this code from another post and basically this code is trying to force the cart price with a discount.
What I want to do is force the discount only if the product is on backorder. So if product is on backorder, the client can order this item which leads to a deposit calculation in the cart.
From Deposit based on a percentage of total cart amount 2nd code snippet answer, I have tried to make code changes to get a specific discount on backordered cart items.
The original code works fine as it's, but how to make it work only for backordered items?
I tried several days now using for example $product->is_on_backorder( 1 ) but I can't get it to work. How to get backordered items total amount in cart?
I know it's an easy solution, but I have tried several solutions and can't get it to work.
Updated: To make that for backordered items only, you will use the following:
add_action( 'woocommerce_cart_calculate_fees', 'calculated_deposit_discount_on_backorders' );
function calculated_deposit_discount_on_backorders( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
$backordered_amount = 0;
foreach( $cart->get_cart() as $cart_item ) {
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$backordered_amount = $cart_item['line_total'] + $cart_item['line_tax'];
}
}
## ## CALCULATION ## ##
$calculated_amount = $backordered_amount * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
}
Code goes in function.php file of your active child theme (or active theme). It should work.

Deposit on backordered cart items in WooCommerce

I've taken this code from another post and basically this code is trying to force the cart price with a discount.
What I want to do is force the discount only if the product is on backorder. So if product is on backorder, the client can order this item which leads to a deposit calculation in the cart.
From Deposit based on a percentage of total cart amount 2nd code snippet answer, I have tried to make code changes to get a specific discount on backordered cart items.
The original code works fine as it's, but how to make it work only for backordered items?
I tried several days now using for example $product->is_on_backorder( 1 ) but I can't get it to work. How to get backordered items total amount in cart?
I know it's an easy solution, but I have tried several solutions and can't get it to work.
Updated: To make that for backordered items only, you will use the following:
add_action( 'woocommerce_cart_calculate_fees', 'calculated_deposit_discount_on_backorders' );
function calculated_deposit_discount_on_backorders( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
$backordered_amount = 0;
foreach( $cart->get_cart() as $cart_item ) {
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$backordered_amount = $cart_item['line_total'] + $cart_item['line_tax'];
}
}
## ## CALCULATION ## ##
$calculated_amount = $backordered_amount * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
}
Code goes in function.php file of your active child theme (or active theme). It should work.

Modify cart item price in WooCommerce version 3.0+ [duplicate]

I am trying to change product price in cart using the following function:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+
How can I make it work in WooCommerce Version 3.0+?
Thanks.
Update 2021 (Handling mini cart custom item price)
With WooCommerce version 3.0+ you need:
To use woocommerce_before_calculate_totals hook instead.
To use WC_Cart get_cart() method instead
To use WC_product set_price() method instead
Here is the code:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update):
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme).
This code is tested and works (still works on WooCommerce 5.1.x).
Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.
Related:
Set cart item price from a hidden input field custom price in Woocommerce 3
Change cart item prices based on custom cart item data in Woocommerce
Set a specific product price conditionally on Woocommerce single product page & cart
Add a select field that will change price in Woocommerce simple products
With WooCommerce version 3.2.6, #LoicTheAztec's answer works for me if I increase the priority to 1000.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.

Change cart item prices in Woocommerce 3

I am trying to change product price in cart using the following function:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+
How can I make it work in WooCommerce Version 3.0+?
Thanks.
Update 2021 (Handling mini cart custom item price)
With WooCommerce version 3.0+ you need:
To use woocommerce_before_calculate_totals hook instead.
To use WC_Cart get_cart() method instead
To use WC_product set_price() method instead
Here is the code:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update):
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme).
This code is tested and works (still works on WooCommerce 5.1.x).
Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.
Related:
Set cart item price from a hidden input field custom price in Woocommerce 3
Change cart item prices based on custom cart item data in Woocommerce
Set a specific product price conditionally on Woocommerce single product page & cart
Add a select field that will change price in Woocommerce simple products
With WooCommerce version 3.2.6, #LoicTheAztec's answer works for me if I increase the priority to 1000.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.

woocommerce change price in checkout and cart page

With woocommerce, in my website I'd like to add in the cart page a select input where the user can select a value between two options, and depending on this value I will change the price.
so far, I could get the total and change it using this :
function action_woocommerce_before_cart_totals( ) {
global $woocommerce;
$woocommerce->cart->total = $woocommerce->cart->total*0.25;
var_dump( $woocommerce->cart->total);};
The issue is that when I go to checkout page it doesn't take the total calculated in functions.php
Thanks for helping me.
You can use woocommerce_review_order_before_order_total hook too at the same time, to display your custom price in checkout, this way:
add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
WC()->cart->total *= 0.25;
//var_dump( WC()->cart->total);
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Payment gateway always uses $order->get_total() variable to fetch cart grand total. So in order to tweak use this filter woocommerce_order_amount_total
for your function if you do follow below steps. Your payment gateway always shows the total you tweaked.
add_filter( 'woocommerce_order_amount_total', 'custom_cart_total' );
function custom_cart_total($order_total) {
return $order_total *= 0.25;
}

Categories