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.
Related
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.
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.
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.
I have this code below but I want it to be effective only if the user is not using another code, and if the user uses another coupon, then disable this one.
based on "Apply a discount for a specific user role in Woocommerce" answer code, I've been trying to look how to check if there's any coupon but can't figure it out.
// 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('affiliate') )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100;
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
Any help is appreciated.
Try the following that will set a custom discount based on specific user role, if there is no applied coupons in cart:
// Applying conditionally a discount for a specific user role and no applied coupons
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 'affiliate' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// Only if there is no applied coupons in cart
if ( ! empty( $cart->get_applied_coupons() ) )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
Code goes on functions.php file of your active child theme (or active theme). Tested and works.
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/