WC_Settings_Shipping

WC_Settings_Shipping.

Defined (1)

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

/includes/admin/settings/class-wc-settings-shipping.php  
  1. class WC_Settings_Shipping extends WC_Settings_Page { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'shipping'; 
  8. $this->label = __( 'Shipping', 'woocommerce' ); 
  9. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); 
  10. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); 
  11. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); 
  12. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); 
  13.  
  14. /** 
  15. * Add this page to settings. 
  16. */ 
  17. public function add_settings_page( $pages ) { 
  18. return wc_shipping_enabled() ? parent::add_settings_page( $pages ) : $pages; 
  19.  
  20. /** 
  21. * Get sections. 
  22. * @return array 
  23. */ 
  24. public function get_sections() { 
  25. $sections = array( 
  26. '' => __( 'Shipping zones', 'woocommerce' ),  
  27. 'options' => __( 'Shipping options', 'woocommerce' ),  
  28. 'classes' => __( 'Shipping classes', 'woocommerce' ),  
  29. ); 
  30.  
  31. if ( ! defined( 'WC_INSTALLING' ) ) { 
  32. // Load shipping methods so we can show any global options they may have 
  33. $shipping_methods = WC()->shipping->load_shipping_methods(); 
  34.  
  35. foreach ( $shipping_methods as $method ) { 
  36. if ( ! $method->has_settings() ) { 
  37. continue; 
  38. $title = empty( $method->method_title ) ? ucfirst( $method->id ) : $method->method_title; 
  39. $sections[ strtolower( $method->id ) ] = esc_html( $title ); 
  40.  
  41. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); 
  42.  
  43. /** 
  44. * Get settings array. 
  45. * @return array 
  46. */ 
  47. public function get_settings( $current_section = '' ) { 
  48. $settings = array(); 
  49.  
  50. if ( '' === $current_section ) { 
  51. $settings = apply_filters( 'woocommerce_shipping_settings', array( 
  52.  
  53. array( 'title' => __( 'Shipping options', 'woocommerce' ), 'type' => 'title', 'id' => 'shipping_options' ),  
  54.  
  55. array( 
  56. 'title' => __( 'Calculations', 'woocommerce' ),  
  57. 'desc' => __( 'Enable the shipping calculator on the cart page', 'woocommerce' ),  
  58. 'id' => 'woocommerce_enable_shipping_calc',  
  59. 'default' => 'yes',  
  60. 'type' => 'checkbox',  
  61. 'checkboxgroup' => 'start',  
  62. 'autoload' => false,  
  63. ),  
  64.  
  65. array( 
  66. 'desc' => __( 'Hide shipping costs until an address is entered', 'woocommerce' ),  
  67. 'id' => 'woocommerce_shipping_cost_requires_address',  
  68. 'default' => 'no',  
  69. 'type' => 'checkbox',  
  70. 'checkboxgroup' => 'end',  
  71. 'autoload' => false,  
  72. ),  
  73.  
  74. array( 
  75. 'title' => __( 'Shipping destination', 'woocommerce' ),  
  76. 'desc' => __( 'This controls which shipping address is used by default.', 'woocommerce' ),  
  77. 'id' => 'woocommerce_ship_to_destination',  
  78. 'default' => 'billing',  
  79. 'type' => 'radio',  
  80. 'options' => array( 
  81. 'shipping' => __( 'Default to customer shipping address', 'woocommerce' ),  
  82. 'billing' => __( 'Default to customer billing address', 'woocommerce' ),  
  83. 'billing_only' => __( 'Force shipping to the customer billing address', 'woocommerce' ),  
  84. ),  
  85. 'autoload' => false,  
  86. 'desc_tip' => true,  
  87. 'show_if_checked' => 'option',  
  88. ),  
  89.  
  90. array( 
  91. 'title' => __( 'Debug mode', 'woocommerce' ),  
  92. 'desc' => __( 'Enable debug mode', 'woocommerce' ),  
  93. 'desc_tip' => __( 'Enable shipping debug mode to show matching shipping zones and to bypass shipping rate cache.', 'woocommerce' ),  
  94. 'id' => 'woocommerce_shipping_debug_mode',  
  95. 'default' => 'no',  
  96. 'type' => 'checkbox',  
  97. 'autoload' => false,  
  98. ),  
  99.  
  100. array( 'type' => 'sectionend', 'id' => 'shipping_options' ),  
  101.  
  102. ) ); 
  103.  
  104. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section ); 
  105.  
  106. /** 
  107. * Output the settings. 
  108. */ 
  109. public function output() { 
  110. global $current_section, $hide_save_button; 
  111.  
  112. // Load shipping methods so we can show any global options they may have 
  113. $shipping_methods = WC()->shipping->load_shipping_methods(); 
  114.  
  115. if ( '' === $current_section ) { 
  116. $this->output_zones_screen(); 
  117. } elseif ( 'options' === $current_section ) { 
  118. $settings = $this->get_settings(); 
  119. WC_Admin_Settings::output_fields( $settings ); 
  120. } elseif ( 'classes' === $current_section ) { 
  121. $hide_save_button = true; 
  122. $this->output_shipping_class_screen(); 
  123. } else { 
  124. foreach ( $shipping_methods as $method ) { 
  125. if ( in_array( $current_section, array( $method->id, sanitize_title( get_class( $method ) ) ) ) && $method->has_settings() ) { 
  126. $method->admin_options(); 
  127.  
  128. /** 
  129. * Save settings. 
  130. */ 
  131. public function save() { 
  132. global $current_section; 
  133.  
  134. switch ( $current_section ) { 
  135. case 'options' : 
  136. WC_Admin_Settings::save_fields( $this->get_settings() ); 
  137. break; 
  138. case 'classes' : 
  139. case '' : 
  140. break; 
  141. default : 
  142. $wc_shipping = WC_Shipping::instance(); 
  143.  
  144. foreach ( $wc_shipping->get_shipping_methods() as $method_id => $method ) { 
  145. if ( in_array( $current_section, array( $method->id, sanitize_title( get_class( $method ) ) ) ) ) { 
  146. do_action( 'woocommerce_update_options_' . $this->id . '_' . $method->id ); 
  147. break; 
  148.  
  149. // Increments the transient version to invalidate cache 
  150. WC_Cache_Helper::get_transient_version( 'shipping', true ); 
  151.  
  152. /** 
  153. * Handles output of the shipping zones page in admin. 
  154. */ 
  155. protected function output_zones_screen() { 
  156. global $hide_save_button; 
  157.  
  158. if ( isset( $_REQUEST['zone_id'] ) ) { 
  159. $hide_save_button = true; 
  160. $this->zone_methods_screen( wc_clean( $_REQUEST['zone_id'] ) ); 
  161. } elseif ( isset( $_REQUEST['instance_id'] ) ) { 
  162. $this->instance_settings_screen( absint( $_REQUEST['instance_id'] ) ); 
  163. } else { 
  164. $hide_save_button = true; 
  165. $this->zones_screen(); 
  166.  
  167. /** 
  168. * Show method for a zone 
  169. * @param int $zone_id 
  170. */ 
  171. protected function zone_methods_screen( $zone_id ) { 
  172. if ( 'new' === $zone_id ) { 
  173. $zone = new WC_Shipping_Zone(); 
  174. } else { 
  175. $zone = WC_Shipping_Zones::get_zone( absint( $zone_id ) ); 
  176.  
  177. if ( ! $zone ) { 
  178. wp_die( __( 'Zone does not exist!', 'woocommerce' ) ); 
  179.  
  180. $allowed_countries = WC()->countries->get_allowed_countries(); 
  181. $wc_shipping = WC_Shipping ::instance(); 
  182. $shipping_methods = $wc_shipping->get_shipping_methods(); 
  183. $continents = WC()->countries->get_continents(); 
  184.  
  185. // Prepare locations 
  186. $locations = array(); 
  187. $postcodes = array(); 
  188.  
  189. foreach ( $zone->get_zone_locations() as $location ) { 
  190. if ( 'postcode' === $location->type ) { 
  191. $postcodes[] = $location->code; 
  192. } else { 
  193. $locations[] = $location->type . ':' . $location->code; 
  194.  
  195. wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array( 
  196. 'methods' => $zone->get_shipping_methods(),  
  197. 'zone_name' => $zone->get_zone_name(),  
  198. 'zone_id' => $zone->get_id(),  
  199. 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),  
  200. 'strings' => array( 
  201. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),  
  202. 'save_changes_prompt' => __( 'Do you wish to save your changes first? Your changed data will be discarded if you choose to cancel.', 'woocommerce' ),  
  203. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),  
  204. 'add_method_failed' => __( 'Shipping method could not be added. Please retry.', 'woocommerce' ),  
  205. 'yes' => __( 'Yes', 'woocommerce' ),  
  206. 'no' => __( 'No', 'woocommerce' ),  
  207. 'default_zone_name' => __( 'Zone', 'woocommerce' ),  
  208. ),  
  209. ) ); 
  210. wp_enqueue_script( 'wc-shipping-zone-methods' ); 
  211.  
  212. include_once( dirname( __FILE__ ) . '/views/html-admin-page-shipping-zone-methods.php' ); 
  213.  
  214. /** 
  215. * Show zones 
  216. */ 
  217. protected function zones_screen() { 
  218. global $wpdb; 
  219.  
  220. $allowed_countries = WC()->countries->get_allowed_countries(); 
  221. $continents = WC()->countries->get_continents(); 
  222. $method_count = wc_get_shipping_method_count(); 
  223.  
  224. wp_localize_script( 'wc-shipping-zones', 'shippingZonesLocalizeScript', array( 
  225. 'zones' => WC_Shipping_Zones::get_zones(),  
  226. 'default_zone' => array( 
  227. 'zone_id' => 0,  
  228. 'zone_name' => '',  
  229. 'zone_order' => null,  
  230. ),  
  231. 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),  
  232. 'strings' => array( 
  233. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),  
  234. 'delete_confirmation_msg' => __( 'Are you sure you want to delete this zone? This action cannot be undone.', 'woocommerce' ),  
  235. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),  
  236. 'no_shipping_methods_offered' => __( 'No shipping methods offered to this zone.', 'woocommerce' ),  
  237. ),  
  238. ) ); 
  239. wp_enqueue_script( 'wc-shipping-zones' ); 
  240.  
  241. include_once( dirname( __FILE__ ) . '/views/html-admin-page-shipping-zones.php' ); 
  242.  
  243. /** 
  244. * Show instance settings 
  245. * @param int $instance_id 
  246. */ 
  247. protected function instance_settings_screen( $instance_id ) { 
  248. $zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id ); 
  249. $shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id ); 
  250.  
  251. if ( ! $shipping_method ) { 
  252. wp_die( __( 'Invalid shipping method!', 'woocommerce' ) ); 
  253. if ( ! $zone ) { 
  254. wp_die( __( 'Zone does not exist!', 'woocommerce' ) ); 
  255. if ( ! $shipping_method->has_settings() ) { 
  256. wp_die( __( 'This shipping method does not have any settings to configure.', 'woocommerce' ) ); 
  257.  
  258. if ( ! empty( $_POST['save'] ) ) { 
  259.  
  260. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { 
  261. echo '<div class="updated error"><p>' . __( 'Edit failed. Please try again.', 'woocommerce' ) . '</p></div>'; 
  262.  
  263. $shipping_method->process_admin_options(); 
  264. $shipping_method->display_errors(); 
  265.  
  266. include_once( dirname( __FILE__ ) . '/views/html-admin-page-shipping-zones-instance.php' ); 
  267.  
  268. /** 
  269. * Handles output of the shipping class settings screen. 
  270. */ 
  271. protected function output_shipping_class_screen() { 
  272. $wc_shipping = WC_Shipping::instance(); 
  273. wp_localize_script( 'wc-shipping-classes', 'shippingClassesLocalizeScript', array( 
  274. 'classes' => $wc_shipping->get_shipping_classes(),  
  275. 'default_shipping_class' => array( 
  276. 'term_id' => 0,  
  277. 'name' => '',  
  278. 'description' => '',  
  279. ),  
  280. 'wc_shipping_classes_nonce' => wp_create_nonce( 'wc_shipping_classes_nonce' ),  
  281. 'strings' => array( 
  282. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),  
  283. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),  
  284. ),  
  285. ) ); 
  286. wp_enqueue_script( 'wc-shipping-classes' ); 
  287.  
  288. // Extendable columns to show on the shipping classes screen. 
  289. $shipping_class_columns = apply_filters( 'woocommerce_shipping_classes_columns', array( 
  290. 'wc-shipping-class-name' => __( 'Shipping class', 'woocommerce' ),  
  291. 'wc-shipping-class-slug' => __( 'Slug', 'woocommerce' ),  
  292. 'wc-shipping-class-description' => __( 'Description', 'woocommerce' ),  
  293. 'wc-shipping-class-count' => __( 'Product count', 'woocommerce' ),  
  294. ) ); 
  295.  
  296. include_once( dirname( __FILE__ ) . '/views/html-admin-page-shipping-classes.php' );