WC_GZD_Checkout

The WooCommerce Germanized WC GZD Checkout class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-gzd-checkout.php  
  1. class WC_GZD_Checkout { 
  2.  
  3. public $custom_fields = array(); 
  4. public $custom_fields_admin = array(); 
  5.  
  6. protected static $_instance = null; 
  7.  
  8. public static function instance() { 
  9. if ( is_null( self::$_instance ) ) 
  10. self::$_instance = new self(); 
  11. return self::$_instance; 
  12.  
  13. /** 
  14. * Cloning is forbidden. 
  15. * @since 1.0 
  16. */ 
  17. public function __clone() { 
  18. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  19.  
  20. /** 
  21. * Unserializing instances of this class is forbidden. 
  22. * @since 1.0 
  23. */ 
  24. public function __wakeup() { 
  25. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  26.  
  27. public function __construct() { 
  28.  
  29. add_action( 'init', array( $this, 'init_fields' ), 30 ); 
  30. add_filter( 'woocommerce_billing_fields', array( $this, 'set_custom_fields' ), 0, 1 ); 
  31. add_filter( 'woocommerce_shipping_fields', array( $this, 'set_custom_fields_shipping' ), 0, 1 ); 
  32.  
  33. // Add Fields to Order Edit Page 
  34. add_filter( 'woocommerce_admin_billing_fields', array( $this, 'set_custom_fields_admin_billing' ), 0, 1 ); 
  35. add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'set_custom_fields_admin_shipping' ), 0, 1 ); 
  36.  
  37. // Save Fields on order 
  38. add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_fields' ) ); 
  39.  
  40. // Add Title to billing address format 
  41. add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'set_formatted_billing_address' ), 0, 2 ); 
  42. add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'set_formatted_shipping_address' ), 0, 2 ); 
  43. add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'set_formatted_address' ), 0, 2 ); 
  44.  
  45. // Support Checkout Field Managers (which are unable to map options to values) 
  46. add_filter( 'woocommerce_gzd_custom_title_field_value', array( $this, 'set_title_field_mapping_editors' ), 10, 1 ); 
  47.  
  48. // Add item desc to order 
  49. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  50. add_action( 'woocommerce_new_order_item', array( $this, 'set_order_item_meta_crud' ), 0, 3 ); 
  51. } else { 
  52. add_action( 'woocommerce_order_add_product', array( $this, 'set_order_meta' ), 0, 5 ); 
  53.  
  54. add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'set_order_meta_hidden' ), 0 ); 
  55.  
  56. // Deactivate checkout shipping selection 
  57. add_action( 'woocommerce_review_order_before_shipping', array( $this, 'remove_shipping_rates' ), 0 ); 
  58.  
  59. // Add better fee taxation 
  60. add_action( 'woocommerce_cart_calculate_fees', array( $this, 'do_fee_tax_calculation' ), PHP_INT_MAX, 1 ); 
  61.  
  62. // Disallow user order cancellation 
  63. if ( get_option( 'woocommerce_gzd_checkout_stop_order_cancellation' ) == 'yes' ) { 
  64. add_filter( 'woocommerce_get_cancel_order_url', array( $this, 'cancel_order_url' ), PHP_INT_MAX, 1 ); 
  65. add_filter( 'woocommerce_get_cancel_order_url_raw', array( $this, 'cancel_order_url' ), PHP_INT_MAX, 1 ); 
  66. add_filter( 'user_has_cap', array( $this, 'disallow_user_order_cancellation' ), 15, 3 ); 
  67. add_action( 'woocommerce_germanized_order_confirmation_sent', array( $this, 'maybe_reduce_order_stock' ), 5, 1 ); 
  68. add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'remove_cancel_button' ), 10, 2 ); 
  69.  
  70. // Woo 3.0 stock reducing checks - mark order as stock-reduced so that stock reducing fails upon second attempt 
  71. add_action( 'woocommerce_reduce_order_stock', array( $this, 'set_order_stock_reduced_meta' ), 10, 1 ); 
  72. add_filter( 'woocommerce_can_reduce_order_stock', array( $this, 'maybe_disallow_order_stock_reducing' ), 10, 2 ); 
  73.  
  74. // Free Shipping auto select 
  75. if ( get_option( 'woocommerce_gzd_display_checkout_free_shipping_select' ) == 'yes' ) 
  76. add_filter( 'woocommerce_package_rates', array( $this, 'free_shipping_auto_select' ) ); 
  77.  
  78. // Pay for order 
  79. add_action( 'wp', array( $this, 'force_pay_order_redirect' ), 15 ); 
  80.  
  81. if ( get_option( 'woocommerce_gzd_checkout_disallow_belated_payment_method_selection' ) === 'yes' ) { 
  82. add_filter( 'woocommerce_get_checkout_payment_url', array( $this, 'set_payment_url_to_force_payment' ), 10, 2 ); 
  83.  
  84. if ( wc_gzd_is_parcel_delivery_data_transfer_checkbox_enabled() ) 
  85. add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'order_parcel_delivery_data_transfer' ), 10, 2 ); 
  86.  
  87.  
  88. public function remove_cancel_button( $actions, $order ) { 
  89.  
  90. if ( isset( $actions[ 'cancel' ] ) ) 
  91. unset( $actions[ 'cancel' ] ); 
  92.  
  93. return $actions; 
  94.  
  95. public function order_parcel_delivery_data_transfer( $order_id, $posted ) { 
  96. if ( isset( $_POST[ 'parcel-delivery' ] ) ) { 
  97. update_post_meta( $order_id, '_parcel_delivery_opted_in', 'yes' ); 
  98. } else { 
  99. update_post_meta( $order_id, '_parcel_delivery_opted_in', 'no' ); 
  100.  
  101. public function set_payment_url_to_force_payment( $url, $order ) { 
  102. if ( strpos( $url, 'pay_for_order' ) !== false ) { 
  103. $url = add_query_arg( array( 'force_pay_order' => true ), $url ); 
  104.  
  105. return $url; 
  106.  
  107. public function force_pay_order_redirect() { 
  108.  
  109. global $wp; 
  110.  
  111. if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET[ 'force_pay_order' ] ) ) { 
  112.  
  113. // Manipulate $_POST 
  114. $order_key = $_GET['key']; 
  115. $order_id = absint( $wp->query_vars[ 'order-pay' ] ); 
  116. $order = wc_get_order( $order_id ); 
  117.  
  118. if ( ! $order ) 
  119. return; 
  120.  
  121. if ( wc_gzd_get_crud_data( $order, 'order_key' ) != $order_key ) 
  122. return; 
  123.  
  124. // Check if gateway is available - otherwise don't force redirect - would lead to errors in pay_action 
  125. $gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  126.  
  127. if ( ! isset( $gateways[ wc_gzd_get_crud_data( $order, 'payment_method' ) ] ) ) 
  128. return; 
  129.  
  130. // Hide terms checkbox 
  131. add_filter( 'woocommerce_germanized_checkout_show_terms', array( $this, 'disable_terms_order_pay' ) ); 
  132.  
  133. // Set $_POST to disable double payment method selection -> redirect by WC_Form_Handler::pay_action() 
  134. $_POST['woocommerce_pay'] = 1; 
  135. $_POST['_wpnonce'] = wp_create_nonce( 'woocommerce-pay' ); 
  136. $_POST['terms'] = 1; 
  137. $_POST['payment_method'] = wc_gzd_get_crud_data( $order, 'payment_method' ); 
  138.  
  139.  
  140.  
  141. public function disable_terms_order_pay( $show ) { 
  142. return false; 
  143.  
  144. public function free_shipping_auto_select( $rates ) { 
  145.  
  146. if ( ! is_checkout() && ! is_cart() ) 
  147. return $rates; 
  148.  
  149. $keep = array(); 
  150. $hide = false; 
  151.  
  152. // Legacy Support 
  153. if ( isset( $rates[ 'free_shipping' ] ) ) { 
  154. $keep[] = 'free_shipping'; 
  155. $hide = true; 
  156.  
  157. // Check for cost-free shipping 
  158. foreach ( $rates as $key => $rate ) { 
  159.  
  160. // Do only hide if free_shipping exists 
  161. if ( strpos( $key, 'free_shipping' ) !== false ) { 
  162. $hide = true; 
  163.  
  164. if ( $rate->cost == 0 ) { 
  165. $keep[] = $key; 
  166.  
  167. // Unset all other rates 
  168. if ( ! empty( $keep ) && $hide ) { 
  169.  
  170. // Unset chosen shipping method to avoid key errors 
  171. unset( WC()->session->chosen_shipping_methods ); 
  172.  
  173. foreach ( $rates as $key => $rate ) { 
  174.  
  175. if ( ! in_array( $key, $keep ) ) 
  176. unset( $rates[ $key ] ); 
  177.  
  178. return $rates; 
  179.  
  180. public function add_payment_link( $order_id ) { 
  181.  
  182. if ( get_option( 'woocommerce_gzd_order_pay_now_button' ) === 'no' ) 
  183. return false; 
  184.  
  185. $order = wc_get_order( $order_id ); 
  186.  
  187. if ( ! $order->needs_payment() ) 
  188. return; 
  189.  
  190. wc_get_template( 'order/order-pay-now-button.php', array( 'url' => add_query_arg( array( 'force_pay_order' => true ), $order->get_checkout_payment_url() ), 'order_id' => $order_id ) ); 
  191.  
  192. public function maybe_reduce_order_stock( $order_id ) { 
  193. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  194. wc_reduce_stock_levels( $order_id ); 
  195. } else { 
  196. $order = wc_get_order( $order_id ); 
  197.  
  198. if ( $order ) { 
  199. // Reduce order stock for non-cancellable orders 
  200. if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! get_post_meta( $order->id, '_order_stock_reduced', true ), wc_gzd_get_crud_data( $order, 'id' ) ) ) { 
  201. $order->reduce_order_stock(); 
  202.  
  203. public function set_order_stock_reduced_meta( $order ) { 
  204. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  205. $order = wc_gzd_set_crud_meta_data( $order, '_order_stock_reduced', '1' ); 
  206. $order->save(); 
  207.  
  208. public function maybe_disallow_order_stock_reducing( $reduce_stock, $order ) { 
  209. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  210. if ( wc_gzd_get_crud_data( $order, '_order_stock_reduced' ) ) { 
  211.  
  212. // Delete the meta so that third party plugins may reduce/change order stock later 
  213. $order = wc_gzd_unset_crud_meta_data( $order, '_order_stock_reduced' ); 
  214. $order->save(); 
  215.  
  216. return false; 
  217. return $reduce_stock; 
  218.  
  219. public function disallow_user_order_cancellation( $allcaps, $caps, $args ) { 
  220. if ( isset( $caps[0] ) ) { 
  221. switch ( $caps[0] ) { 
  222. case 'cancel_order' : 
  223. $allcaps['cancel_order'] = false; 
  224. break; 
  225. return $allcaps; 
  226.  
  227. public function cancel_order_url( $url ) { 
  228.  
  229. // Default to home url 
  230. $return = get_permalink( wc_get_page_id( 'shop' ) ); 
  231.  
  232. // Extract order id and use order success page as return url 
  233. $search = preg_match( '/order_id=([0-9]+)/', $url, $matches ); 
  234.  
  235. if ( $search && isset( $matches[1] ) ) { 
  236. $order_id = absint( $matches[1] ); 
  237. $order = wc_get_order( $order_id ); 
  238. $return = apply_filters( 'woocommerce_gzd_attempt_order_cancellation_url', add_query_arg( array( 'retry' => true ), $order->get_checkout_order_received_url(), $order ) ); 
  239.  
  240. return $return; 
  241.  
  242. public function init_fields() { 
  243. if ( get_option( 'woocommerce_gzd_checkout_address_field' ) == 'yes' ) { 
  244.  
  245. $this->custom_fields[ 'title' ] = array( 
  246. 'type' => 'select',  
  247. 'required' => 1,  
  248. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  249. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  250. 'before' => 'first_name',  
  251. 'group' => array( 'billing', 'shipping' ),  
  252. ); 
  253.  
  254. $this->custom_fields_admin[ 'title' ] = array( 
  255. 'before' => 'first_name',  
  256. 'type' => 'select',  
  257. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  258. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  259. 'show' => false,  
  260. ); 
  261.  
  262.  
  263. if ( get_option( 'woocommerce_gzd_checkout_phone_required' ) == 'no' ) { 
  264.  
  265. $this->custom_fields[ 'phone' ] = array( 
  266. 'before' => '',  
  267. 'override' => true,  
  268. 'required' => false,  
  269. 'group' => array( 'billing' ) 
  270. ); 
  271.  
  272.  
  273. $this->custom_fields_admin = apply_filters( 'woocommerce_gzd_custom_checkout_admin_fields', $this->custom_fields_admin, $this ); 
  274. $this->custom_fields = apply_filters( 'woocommerce_gzd_custom_checkout_fields', $this->custom_fields, $this ); 
  275.  
  276. public function set_title_field_mapping_editors( $val ) { 
  277.  
  278. $values = array( 
  279. __( 'Mr.', 'woocommerce-germanized' ) => 1,  
  280. __( 'Ms.', 'woocommerce-germanized' ) => 2,  
  281. ); 
  282.  
  283. if ( isset( $values[ $val ] ) ) 
  284. return $values[ $val ]; 
  285.  
  286. return $val; 
  287.  
  288. /** 
  289. * Recalculate fee taxes to split tax based on different tax rates contained within cart 
  290. *  
  291. * @param WC_Cart $cart 
  292. */ 
  293. public function do_fee_tax_calculation( WC_Cart $cart ) { 
  294. if ( get_option( 'woocommerce_gzd_fee_tax' ) != 'yes' ) 
  295. return; 
  296. if ( ! empty( $cart->fees ) ) { 
  297. $tax_shares = wc_gzd_get_cart_tax_share( 'fee' ); 
  298. foreach ( $cart->fees as $key => $fee ) { 
  299. if ( ! $fee->taxable && get_option( 'woocommerce_gzd_fee_tax_force' ) != 'yes' ) 
  300. continue;  
  301. // Calculate gross price if necessary 
  302. if ( $fee->taxable ) { 
  303. $fee_tax_rates = WC_Tax::get_rates( $fee->tax_class ); 
  304. $fee_tax = WC_Tax::calc_tax( $fee->amount, $fee_tax_rates, false ); 
  305. $fee->amount += array_sum( $fee_tax ); 
  306. // Set fee to nontaxable to avoid WooCommerce default tax calculation 
  307. $fee->taxable = false; 
  308. // Calculate tax class share 
  309. if ( ! empty( $tax_shares ) ) { 
  310. $fee_taxes = array(); 
  311. foreach ( $tax_shares as $rate => $class ) { 
  312. $tax_rates = WC_Tax::get_rates( $rate ); 
  313. $tax_shares[ $rate ][ 'fee_tax_share' ] = $fee->amount * $class[ 'share' ]; 
  314. $tax_shares[ $rate ][ 'fee_tax' ] = WC_Tax::calc_tax( ( $fee->amount * $class[ 'share' ] ), $tax_rates, true ); 
  315. $fee_taxes += $tax_shares[ $rate ][ 'fee_tax' ]; 
  316. foreach ( $tax_shares as $rate => $class ) { 
  317. $cart->fees[ $key ]->tax_data = $cart->fees[ $key ]->tax_data + $class[ 'fee_tax' ]; 
  318. // Add fee taxes to cart taxes 
  319. foreach ( array_keys( $cart->taxes + $fee_taxes ) as $sub ) { 
  320. $cart->taxes[ $sub ] = ( isset( $fee_taxes[ $sub ] ) ? $fee_taxes[ $sub ] : 0 ) + ( isset( $cart->taxes[ $sub ] ) ? $cart->taxes[ $sub ] : 0 ); 
  321. // Update fee 
  322. $cart->fees[ $key ]->tax = array_sum( $cart->fees[ $key ]->tax_data ); 
  323. $cart->fees[ $key ]->amount = $cart->fees[ $key ]->amount - $cart->fees[ $key ]->tax; 
  324.  
  325. /** 
  326. * Temporarily removes all shipping rates (except chosen one) from packages to only show chosen package within checkout.  
  327. */ 
  328. public function remove_shipping_rates() { 
  329. if ( get_option( 'woocommerce_gzd_display_checkout_shipping_rate_select' ) == 'no' ) 
  330. return; 
  331.  
  332. $packages = WC()->shipping->get_packages(); 
  333.  
  334. foreach ( $packages as $i => $package ) { 
  335.  
  336. $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : ''; 
  337.  
  338. if ( ! empty( $package[ 'rates' ] ) ) { 
  339. foreach ( $package[ 'rates' ] as $key => $rate ) { 
  340. if ( $key != $chosen_method ) 
  341. unset( WC()->shipping->packages[ $i ][ 'rates' ][ $key ] ); 
  342. }  
  343.  
  344. /** 
  345. * Adds product description to order meta 
  346. *  
  347. * @param int $order_id  
  348. * @param int $item_id  
  349. * @param object $product  
  350. * @param int $qty  
  351. * @param array $args  
  352. */ 
  353. public function set_order_meta( $order_id, $item_id, $product, $qty, $args ) { 
  354. wc_add_order_item_meta( $item_id, '_units', wc_gzd_get_gzd_product( $product )->get_product_units_html() ); 
  355. wc_add_order_item_meta( $item_id, '_delivery_time', wc_gzd_get_gzd_product( $product )->get_delivery_time_html() ); 
  356. wc_add_order_item_meta( $item_id, '_item_desc', wc_gzd_get_gzd_product( $product )->get_mini_desc() ); 
  357. wc_add_order_item_meta( $item_id, '_unit_price', wc_gzd_get_gzd_product( $product )->get_unit_html( false ) ); 
  358.  
  359. public function set_order_item_meta_crud( $item_id, $item, $order_id ) { 
  360.  
  361. $order = wc_get_order( $order_id ); 
  362.  
  363. if ( ! $order ) { 
  364. return; 
  365.  
  366. $item = $order->get_item( $item_id ); 
  367.  
  368. if ( 'line_item' === $item->get_type() && $item->get_product() ) { 
  369. $this->set_order_meta( $order_id, $item_id, $item->get_product(), $item->get_quantity(), array() ); 
  370.  
  371. /** 
  372. * Hide product description from order meta default output 
  373. *  
  374. * @param array $metas 
  375. */ 
  376. public function set_order_meta_hidden( $metas ) { 
  377. array_push( $metas, '_item_desc' ); 
  378. array_push( $metas, '_units' ); 
  379. array_push( $metas, '_delivery_time' ); 
  380. array_push( $metas, '_unit_price' ); 
  381. return $metas; 
  382.  
  383. public function set_formatted_billing_address( $fields = array(), $order ) { 
  384.  
  385. if ( 'yes' !== get_option( 'woocommerce_gzd_checkout_address_field' ) ) 
  386. return $fields; 
  387.  
  388. if ( wc_gzd_get_crud_data( $order, 'billing_title' ) ) 
  389. $fields[ 'title' ] = $this->get_customer_title( wc_gzd_get_crud_data( $order, 'billing_title' ) ); 
  390.  
  391. return $fields; 
  392.  
  393. public function set_formatted_shipping_address( $fields = array(), $order ) { 
  394.  
  395. if ( 'yes' !== get_option( 'woocommerce_gzd_checkout_address_field' ) ) 
  396. return $fields; 
  397.  
  398. if ( wc_gzd_get_crud_data( $order, 'shipping_title' ) ) 
  399. $fields[ 'title' ] = $this->get_customer_title( wc_gzd_get_crud_data( $order, 'shipping_title' ) ); 
  400. return $fields; 
  401.  
  402. public function get_customer_title( $option = 1 ) { 
  403. return ( 1 == $option ? __( 'Mr.', 'woocommerce-germanized' ) : __( 'Ms.', 'woocommerce-germanized' ) ); 
  404.  
  405. public function set_formatted_address( $placeholder, $args ) { 
  406. if ( isset( $args[ 'title' ] ) ) { 
  407. $placeholder[ '{title}' ] = $args[ 'title' ]; 
  408. $placeholder[ '{title_upper}' ] = strtoupper( $args[ 'title' ] ); 
  409. $placeholder[ '{name}' ] = $placeholder[ '{title}' ] . ' ' . $placeholder[ '{name}' ]; 
  410. $placeholder[ '{name_upper}' ] = $placeholder[ '{title_upper}' ] . ' ' . $placeholder[ '{name_upper}' ]; 
  411. return $placeholder; 
  412.  
  413. public function set_custom_fields( $fields = array(), $type = 'billing' ) { 
  414.  
  415. if ( ! empty( $this->custom_fields ) ) { 
  416.  
  417. foreach ( $this->custom_fields as $key => $custom_field ) { 
  418.  
  419. $new = array(); 
  420.  
  421. if ( in_array( $type, $custom_field[ 'group' ] ) ) { 
  422.  
  423. if ( ! empty( $fields ) ) { 
  424.  
  425. foreach ( $fields as $name => $field ) { 
  426.  
  427. if ( $name == $type . '_' . $custom_field[ 'before' ] && ! isset( $custom_field[ 'override' ] ) ) { 
  428. $new[ $type . '_' . $key ] = $custom_field; 
  429.  
  430. $new[ $name ] = $field; 
  431.  
  432. if ( $name == $type . '_' . $key && isset( $custom_field[ 'override' ] ) ) { 
  433. $new[ $name ] = array_merge( $field, $custom_field ); 
  434.  
  435. if ( ! empty( $new ) ) { 
  436. $fields = $new; 
  437.  
  438. return $fields; 
  439.  
  440. public function set_custom_fields_shipping( $fields ) { 
  441. return $this->set_custom_fields( $fields, 'shipping' ); 
  442.  
  443. public function set_custom_fields_admin( $fields = array(), $type = 'billing' ) { 
  444. $new = array(); 
  445.  
  446. if ( ! empty( $this->custom_fields_admin ) ) { 
  447.  
  448. foreach ( $this->custom_fields_admin as $key => $custom_field ) { 
  449.  
  450. $new = array(); 
  451.  
  452. if ( isset( $custom_field[ 'address_type' ] ) && $custom_field[ 'address_type' ] !== $type ) 
  453. continue; 
  454.  
  455. if ( ! empty( $fields ) ) { 
  456.  
  457. foreach ( $fields as $name => $field ) { 
  458. if ( $name == $custom_field[ 'before' ] && ! isset( $custom_field[ 'override' ] ) ) 
  459. $new[ $key ] = $custom_field; 
  460.  
  461. $new[ $name ] = $field; 
  462.  
  463. if ( ! empty( $new ) ) { 
  464. $fields = $new; 
  465.  
  466. return $fields; 
  467.  
  468. public function set_custom_fields_admin_billing( $fields = array() ) { 
  469. return $this->set_custom_fields_admin( $fields, 'billing' ); 
  470.  
  471. public function set_custom_fields_admin_shipping( $fields = array() ) { 
  472. return $this->set_custom_fields_admin( $fields, 'shipping' ); 
  473.  
  474. public function save_fields( $order_id ) { 
  475. $checkout = WC()->checkout(); 
  476.  
  477. if ( ! empty( $this->custom_fields ) ) { 
  478. foreach ( $this->custom_fields as $key => $custom_field ) { 
  479. if ( ! empty( $custom_field[ 'group' ] ) && ! isset( $custom_field[ 'override' ] ) ) { 
  480. foreach ( $custom_field[ 'group' ] as $group ) { 
  481.  
  482. $val = ( isset( $checkout->posted[ $group . '_' . $key ] ) ? $checkout->posted[ $group . '_' . $key ] : '' ); 
  483.  
  484. if ( $group == 'shipping' || $group == 'billing' ) 
  485. $val = $checkout->get_posted_address_data( $key, $group ); 
  486.  
  487. if ( ! empty( $val ) ) 
  488. update_post_meta( $order_id, '_' . $group . '_' . $key, apply_filters( 'woocommerce_gzd_custom_' . $key . '_field_value', sanitize_text_field( $val ) ) ); 
  489.