'shipping', 'message' => 'error' ) ) ); exit; } $order = isset( $_POST['method_order'] ) && is_array( $_POST['method_order'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['method_order'] ) ) : array(); $raw_rules = isset( $_POST['rule'] ) && is_array( $_POST['rule'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['rule'] ) ) : array(); $known_methods = WC_EDM_Helpers::get_all_shipping_methods(); // Rules are replaced wholesale: only methods with an explicit show/hide choice are stored. $rules = array(); foreach ( $order as $index => $method_id ) { if ( ! isset( $known_methods[ $method_id ] ) ) { continue; } $action = isset( $raw_rules[ $method_id ] ) ? $raw_rules[ $method_id ] : ''; if ( ! in_array( $action, array( 'show', 'hide' ), true ) ) { continue; } $rules[] = array( 'shipping_method_id' => $method_id, 'action' => $action, 'sort_order' => $index, ); } WC_EDM_DB::replace_shipping_rules( $group_id, $rules ); wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'shipping', 'group_id' => $group_id, 'message' => 'saved', ) ) ); exit; } /** * Render the shipping rules tab. */ public function render() { $groups = WC_EDM_DB::get_groups(); ?>
render_rules_form( $group_id ); } /** * Render the rules form for a selected group. * * @param int $group_id Group ID. */ private function render_rules_form( $group_id ) { $methods = WC_EDM_Helpers::get_all_shipping_methods(); $rules = WC_EDM_DB::get_shipping_rules( $group_id ); if ( empty( $methods ) ) { echo '
' . esc_html__( 'No shipping methods found. Configure shipping zones in WooCommerce first.', 'wc-email-discount' ) . '
'; return; } // Existing rules by method id, and the display order: ruled methods first (by sort_order), then the rest. $rules_by_method = array(); foreach ( $rules as $rule ) { $rules_by_method[ $rule->shipping_method_id ] = $rule; } $ordered_keys = array_merge( array_values( array_intersect( array_keys( $rules_by_method ), array_keys( $methods ) ) ), array_values( array_diff( array_keys( $methods ), array_keys( $rules_by_method ) ) ) ); ?>