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