class-wc-settings-shipping.php 12 KB

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