Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_total' );
  2. function custom_cart_total($order_total) {
  3.   $user = wp_get_current_user();
  4.  
  5.   if( in_array( 'practitioner', (array) $user->roles) ):
  6.     $tradeDiscountTotal = 0;
  7.     $loggedInUser = get_current_user_id();
  8.  
  9.     //Get discount from user & work out numerical value as a percentage
  10.     $discountApplied = get_field('trade_discount', 'user_'. $loggedInUser );
  11.     $discountApplied = 100 / $discountApplied;
  12.  
  13.     foreach( WC()->cart->get_cart() as $cart_item ) {
  14.       if(!get_field('trade_discount_exempt', $cart_item['product_id'])) {
  15.         $tradeDiscountTotal += $cart_item['line_total'];
  16.       }
  17.     }
  18.  
  19.     //Work out discount using user discount & update the price
  20.     $totalDiscount = $tradeDiscountTotal / $discountApplied;
  21.     WC()->cart->add_fee( __('Trade Discount', 'woocommerce'), -$totalDiscount );
  22.   endif;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement