I want to surcharge special tax based on specific attribute value (billing_company) at checkout page.
Problem is that order details are not automatically recalculated and refreshed. I can refresh the whole page with F5 and then change is visible (8% vat is added to order).
I would like to write a code which will automatically do this for me after user enters specific attribute value on checkout.
Code review:
// Add 8% tax for Companies
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_companies', 10, 1 );
function custom_tax_surcharge_for_companies( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// Only for Companies (if not we exit)
if ( 'TEST COMPANY' != WC()->customer->get_billing_company() ) return;
$percent = 8;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'VAT', 'woocommerce')." ($percent%)", $surcharge, false );
}
Many thanks in advance.
Related
I applied a 5% discount to the Woocommerce cart, based on cart amount and shipping method, which seems to be working fine. But I cannot manage to change the shipping method title when the rule applies, i need to add a message "5% discount" to all local-pickup methods.
I found a workarround using conditional shipping, but that won´t work in this website, I need to use the same method, but adding a message to the title.
Any ideas?
This is my code:
function local_pickup_discount( $cart ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$carrito_total = WC()->cart->get_cart_contents_total();
$chosen_shipping_no_ajax = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping_no_ajax, 'local_pickup' ) && $carrito_total < 60000) {
$discount = $cart->subtotal * 0.05; // 5% discount applied here
$cart->add_fee( __( 'Descuento por retiro en sucursal', 'yourtext-domain' ) , -$discount ); // Fee descripton
}
}
add_action( 'woocommerce_cart_calculate_fees', 'local_pickup_discount');
Thanks in advance, i know this must be a very simple thing to do, but i am not used to work with php.
You have to use this hook to rename your label woocommerce_cart_shipping_method_full_label. Inside you can apply any conditional logic you want and output label with custom one.
Note: This label is used in both cart and checkout. So if you want it only on checkout add condition is_checkout() or is_cart() if you want to limit it.
function wc_change_local_pickup_label( $label, $method ) {
$carrito_total = WC()->cart->get_cart_contents_total();
if ( $carrito_total < 60000 && 'local_pickup' === $method->method_id) {
$label = "Local pickup 5% discount";
}
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'wc_change_local_pickup_label', 10, 2 );
I have a woocommerce store, with 3 user roles, I want to provide a 10% discount on the cart total only for a user role 'company'.
I found "Percentage discount based on user role and payment method in Woocommerce" answer that is very neer of what I need, but don't know how to modify the code to feet my needs, as it contains also a condition based on payment method and displaying the discount only at checkout, but I need it to display in cart as well.
The following code will apply a 10% discount for 'company' user role (on cart and checkout):
// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('company') )
return; // Exit
// HERE define the percentage discount
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
Code goes on functions.php file of your active child theme (or active theme). It should works.
I want to add custom % value at woocommerce checkout page, but I want to show it only for Swiss country and hide it for others. Now I have the right code working for that, but the problems is that Im not able to show it when user choose switzerland. Here is a code so please help me see what Im doing wrong here
//Add tax for CH country
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( WC()->customer->get_shipping_country('CH') )
return;
$percentage = 0.08;
$taxes = array_sum($woocommerce->cart->taxes);
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
// Make sure that you return false here. We can't double tax people!
$woocommerce->cart->add_fee( 'TAX', $surcharge, false, '' );
}
Im sure Im doing wrong inside here:
if ( WC()->customer->get_shipping_country('CH') )
thanks for help
The WC_Customer get_shipping_country() doesn't accept any country code as you are getting a country code. So you need to set it differently in your code condition.
Also as your hooked function has already the WC_Cart object as argument, you don't need global $woocommerce and $woocommerce->cart…
So your revisited code should be:
// Add tax for Swiss country
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// Only for Swiss country (if not we exit)
if ( 'CH' != WC()->customer->get_shipping_country() ) return;
$percent = 8;
# $taxes = array_sum( $cart->taxes ); // <=== This is not used in your function
// 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 );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works… you will get something like:
But for taxes, you should better use default WooCommerce tax feature in Settings > Tax (tab), where con can set the tax rates for each country…
I am developing a food ordering WooCommerce store. I am trying to add an input field or a grouped button to let the customer add delivery tips to checkout total.
Backend Part:
I wrote an action that add a tip to checkout based on the percentage given (let's say 10% of total order)
add_action( 'woocommerce_cart_calculate_fees', 'calculateTipsPercentage', 10, 1 );
function calculateTipsPercentage( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$total_tax = 0;
$carttotal= 0;
// Get the unformated taxes array
$taxes = $cart->get_taxes();
//get Tax amount from taxes array
foreach($taxes as $tax) $total_tax += $tax;
global $woocommerce;
$percentage = 0.10;//percentage of tips (must be entered by customer)
$carttotalamount = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ); //Cart total without tax
$totalorderwithTax = ( $carttotalamount + $total_tax); //cart total with tax
$extrafee= round( $totalorderwithTax * $percentage, 2 ); //extra fee amount
$percetagetoShow = ( $percentage * 100 );
$woocommerce->cart->add_fee( "Tip ({$percetagetoShow}%)", $extrafee, true, '' );
}
My problem is with the Front-end part.
how can I add any kind of input field with a button (add tips to checkout) that let the customer add the percentage and click this button which will fire the action found above. or if I can do it through ajax/jquery without the button (without refreshing the page) which would be better.
any help would be appreciated.
Woocommerce has various hooks that you can use to add your custom fields to the checkout page such as *
woocommerce_review_order_before_shipping
woocommerce_review_order_after_shipping
woocommerce_review_order_before_order_total
or you can visit here
In my application there is need to add extra price to each product if customer wants certain services, so I have added price from session (the extra price ) to each product by using woocommerce_before_calculate_totals hook,It works fine, whenever the total is showing including my extra price.
But the extra price is not added while making payment, only the product original price is sent to payment gateway, other wise in all places like (cart / checkout the right amount is showing)
Is there any hook that I am missing for payment where the extra price is not added.
Thanks in advance.
You have to use the following hook woocommerce_cart_calculate_fees.
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, '' );
}
For reference: https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
add_fee reference: http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/