From d86a948f04ad8eb0e6b5e5abd7fbea494463eb50 Mon Sep 17 00:00:00 2001 From: Manos Psychogyiopoulos Date: Thu, 25 Jan 2018 16:37:10 +0200 Subject: [PATCH] Add method instances in COD, grouped by method --- .../gateways/cod/class-wc-gateway-cod.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/includes/gateways/cod/class-wc-gateway-cod.php b/includes/gateways/cod/class-wc-gateway-cod.php index a20be862050..b4cf078fd97 100644 --- a/includes/gateways/cod/class-wc-gateway-cod.php +++ b/includes/gateways/cod/class-wc-gateway-cod.php @@ -58,10 +58,40 @@ class WC_Gateway_COD extends WC_Payment_Gateway { * Initialise Gateway Settings Form Fields. */ public function init_form_fields() { - $shipping_methods = array(); + + $options = array(); + $data_store = WC_Data_Store::load( 'shipping-zone' ); + $raw_zones = $data_store->get_zones(); + + foreach ( $raw_zones as $raw_zone ) { + $zones[] = new WC_Shipping_Zone( $raw_zone ); + } + + $zones[] = new WC_Shipping_Zone(0); foreach ( WC()->shipping()->load_shipping_methods() as $method ) { - $shipping_methods[ $method->id ] = $method->get_method_title(); + + $options[ $method->get_method_title() ] = array(); + + $options[ $method->get_method_title() ][ $method->id ] = sprintf( __( 'Any "%1$s" method', 'woocommerce' ), $method->get_method_title() ); + + foreach ( $zones as $zone ) { + + $shipping_method_instances = $zone->get_shipping_methods(); + + foreach ( $shipping_method_instances as $shipping_method_instance_id => $shipping_method_instance ) { + + if ( $shipping_method_instance->id !== $method->id ) { + continue; + } + + $option_id = $shipping_method_instance->get_rate_id(); + $option_instance_title = sprintf( __( '%1$s (#%2$s)', 'woocommerce' ), $shipping_method_instance->get_title(), $shipping_method_instance_id ); + $option_title = sprintf( __( '%1$s – %2$s', 'woocommerce' ), $zone->get_id() ? $zone->get_zone_name() : __( 'Other locations', 'woocommerce' ), $option_instance_title ); + + $options[ $method->get_method_title() ][ $option_id ] = $option_title; + } + } } $this->form_fields = array( @@ -100,7 +130,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway { 'css' => 'width: 400px;', 'default' => '', 'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ), - 'options' => $shipping_methods, + 'options' => $options, 'desc_tip' => true, 'custom_attributes' => array( 'data-placeholder' => __( 'Select shipping methods', 'woocommerce' ),