Woocommerce: recognize custom fee as "shipping" specific charge - wordpress

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/

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

Add Extra charges in shipping woocommerce

using XPS for Shipping rates. How can I add handling charges in shipping.
like if the shipping charges rates $39.52, it should add 3$ in shipping rates ( like 42.52 ) even we can code in function.php.
How do I accomplish this?
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_handling' );
function woocommerce_custom_handling() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$shipping_method = WC()->session->get( 'chosen_shipping_methods' );
if($shipping_method[0]){
$handling = 3.00;
$woocommerce->cart->add_fee('Handling', $handling, true, 'standard');
}
}
To add directly to shipping costs: Since I don't know the shipping key for XPS this is my best guess to add it to all shipping methods, including XPS. See if this works
add_filter( 'woocommerce_package_rates','woocommerce_custom_handling' );
function woocommerce_custom_handling($rates, $package) {
$handling = 3.00;
foreach( $rates as $rate_key => $rate ){
$rates[$rate_key]->cost = ($handling + $rates[$rate_key]->cost);
}
return $rates;
}

Hide shipping and payment methods in WooCommerce

I'm building a WooCommerce e-shop and I need to tweak my checkout page by doing the following:
Hide a certain shipping method (only one) if the order total > 100€.
Hide the cash on delivery payment method if local pickup is selected.
Does anyone know how to do that? I have the Code Snippets plugin so I can easily add any custom code.
To hide specific shipping method based on the cart total, you can use below code snippet. You need to update your shipping method name in the code.
Disable shipping method as per cart total
Add this snippet in your theme's functions.php file or custom plugin file.
add_filter( 'woocommerce_package_rates', 'shipping_based_on_price', 10, 2 );
function shipping_based_on_price( $rates, $package ) {
$total = WC()->cart->cart_contents_total;
//echo $total;
if ( $total > 100 ) {
unset( $rates['local_delivery'] ); // Unset your shipping method
}
return $rates;
}
Disable Payment Gateway For Specific Shipping Method
Use below code snippet. Update code as per your payment method & shipping method.
add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );
function x34fg_gateway_disable_shipping( $available_gateways ) {
global $woocommerce;
if ( !is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
There are a number of plugins that will do this for you, take a look at this one WooCommerce Conditional Shipping and Payments
You'll want to tie in to the "woocommerce_payment_gateways" action
Something along these lines:
function alter_payment_gateways( $gateways ){
$chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if( in_array( 'local-pickup:6', $chosen_rates ) ) {
$array_diff = array('cod');
$list = array_diff( $list, $array_diff );
}
return $list;
}
add_action('woocommerce_payment_gateways', 'alter_payment_gateways', 50, 1);
The number on the end of 'local-pickup' on line 4 will depend on your woocommerce setup. You can find the string you need to put in here by adding something to a basket, going to the checkout, right clicking on the "Local Pickup" option in the delivery methods and looking at the value attribute.

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