Advertisement
YordanSoares

WooCommerce: Save minimum amount within the order data when a free shipping method is selected

Dec 20th, 2023
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | Software | 0 0
  1. <?php
  2. /**
  3.  * WooCommerce:
  4.  * Save the free shipping methodminimum amount (if set) within the order data when a free shipping method is selected  
  5.  */
  6. add_action( 'woocommerce_checkout_create_order', 'wpo_safe_free_shipping_amount_within_order_data' );
  7. function wpo_safe_free_shipping_amount_within_order_data( $order ) {
  8.     $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
  9.    
  10.     // If a free shipping method is selected...
  11.     if ( strpos( $chosen_shipping_methods[0], 'free_shipping' ) !== false ) {
  12.         list( $shipping_name, $shipping_id ) = explode( ':', $chosen_shipping_methods[0] );
  13.        
  14.         // ...get the minimum amount from its settings
  15.         $free_shipping_settings = get_option( "woocommerce_{$shipping_name}_{$shipping_id}_settings" );
  16.         $min_amount = $free_shipping_settings['min_amount'];
  17.  
  18.         // If there is a minimum amount set, save it within the order data
  19.         if ( ! empty( $min_amount ) ) {
  20.             $order->update_meta_data( '_free_shipping_min_amount', $min_amount );
  21.             $order->save();
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement