borkolivic

Custom fee for COD (if free shipping is not available)

Jan 7th, 2021 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
  2. function custom_handling_fee ( $cart ) {
  3.     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  4.         return;
  5.  
  6.     $chosen_payment_id = WC()->session->get('chosen_payment_method');
  7.  
  8.     if ( empty( $chosen_payment_id ) )
  9.         return;
  10.  
  11.     $subtotal = $cart->subtotal;
  12.  
  13.     // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
  14.     $targeted_payment_ids = array(
  15.         'cod' => 17, // Fixed fee
  16.     );
  17.     $minimum_amount = 500; // Amount needed for free shipping
  18.  
  19.     // Loop through defined payment Ids array
  20.     if ( $subtotal < $minimum_amount ) {
  21.         foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
  22.             if ( $chosen_payment_id === $payment_id) {
  23.                 $cart->add_fee( __('Plaćanje pouzećem', 'woocommerce'), $fee_cost, true );
  24.             }
  25.         }
  26.      }
  27. }
Add Comment
Please, Sign In to add comment