Add fee to woocommerce cart - wordpress

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/

Related

How do I remove the surcharge for subscriptions but keep it for all other products?

I added a surcharge to my checkout page for products sold on my site. However, I need to remove the surcharge for subscriptions. How do I amend the code to make it possible?
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, '' );
}

woocommerce additional fees based on product input

The products contain 2 different Attributes (packaging= crate/container) and I have to put 3 different fee and name for 2 different attributes . The fee also need Multiply Fee by Quantity and display name & fee 1 by 1 on cart page and checkout page. In addition, the fee on cart page and checkout page will auto calculate to the total price . Any plugin or add the code for this problem? thank you.
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, '' );
}
Here is a sample code on how to add fee

Update fee dynamically in Woocommerce checkout

Based on this code : Update fee dynamically based on radio buttons in Woocommerce checkout I am trying to have a dynamic conditional percentage fee in my checkout page on Woocommerce, based on 3 conditions:
Here is my actual custom code:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage3 = 0.01;
$fee3 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage3;
$woocommerce->cart->add_fee( 'Fee 3', $fee3, true, '' );
$percentage2 = 0.02;
$fee2 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage2;
$woocommerce->cart->add_fee( 'Fee 2', $fee2, true, '' );
$percentage1 = 0.03;
$fee1 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage1;
$woocommerce->cart->add_fee( 'Fee 1', $fee1, true, '' );
}
But It doesn't work… How can I make it work like in the linked answer?
add_action('woocommerce_cart_calculate_fees', 'add_ship_fee');
function add_ship_fee() {
$custom_shippingcost = 10;
WC()->cart->add_fee('VAT: ', $custom_shippingcost);
}
you can add this cost dynamically on cart and checkout. you can also change the cost of variable by getting the cost of cart and add some % of total cost of cart..

Woocommerce: recognize custom fee as "shipping" specific charge

I know I can add a fee to the cart using the below code, but it just adds a generic fee to the cart. Woocommerce recognizes shipping specific fees and I am wondering if there is a way to make the fees addes via the below method something that woocommerce sees as a "shipping" specific fee. Some sort of metadata that gets added to shipping specific fees. our API is not recognizing these fees as shipping charges and therefore does not log them and that is what I am trying to resolve.
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, '' );
}
You can do something like this:
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
$percentage = 0.01;
foreach($rates as $key => $rate ) {
$surcharge = ( wc()->cart->cart_contents_total + $rates[$key]->cost ) * $percentage;
$rates[$key]->label .= ": {$rates[$key]->cost} + Fee: {$surcharge}";
$rates[$key]->cost += $surcharge;
}
return $rates;
}
add the surcharge to the rates of shipping. Code above will produce something like this:
More about shipping fees here: http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/

Woocommerce Include Tax on Cart but not on checkout

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.

Resources