1 <?php
  2 /**
  3  * WooCommerce Shipping Settings
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Settings_Shipping' ) ) :
 14 
 15 /**
 16  * WC_Settings_Shipping
 17  */
 18 class WC_Settings_Shipping extends WC_Settings_Page {
 19 
 20     /**
 21      * Constructor.
 22      */
 23     public function __construct() {
 24         $this->id    = 'shipping';
 25         $this->label = __( 'Shipping', 'woocommerce' );
 26 
 27         add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
 28         add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
 29         add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
 30         add_action( 'woocommerce_admin_field_shipping_methods', array( $this, 'shipping_methods_setting' ) );
 31         add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
 32     }
 33 
 34     /**
 35      * Get sections
 36      *
 37      * @return array
 38      */
 39     public function get_sections() {
 40         $sections = array(
 41             ''         => __( 'Shipping Options', 'woocommerce' )
 42         );
 43 
 44         // Load shipping methods so we can show any global options they may have
 45         $shipping_methods = WC()->shipping->load_shipping_methods();
 46 
 47         foreach ( $shipping_methods as $method ) {
 48 
 49             if ( ! $method->has_settings() ) continue;
 50 
 51             $title = empty( $method->method_title ) ? ucfirst( $method->id ) : $method->method_title;
 52 
 53             $sections[ strtolower( get_class( $method ) ) ] = esc_html( $title );
 54         }
 55 
 56         return $sections;
 57     }
 58 
 59     /**
 60      * Get settings array
 61      *
 62      * @return array
 63      */
 64     public function get_settings() {
 65         return apply_filters('woocommerce_shipping_settings', array(
 66 
 67             array( 'title' => __( 'Shipping Options', 'woocommerce' ), 'type' => 'title', 'id' => 'shipping_options' ),
 68 
 69             array(
 70                 'title'         => __( 'Shipping Calculations', 'woocommerce' ),
 71                 'desc'      => __( 'Enable shipping', 'woocommerce' ),
 72                 'id'        => 'woocommerce_calc_shipping',
 73                 'default'   => 'yes',
 74                 'type'      => 'checkbox',
 75                 'checkboxgroup'     => 'start'
 76             ),
 77 
 78             array(
 79                 'desc'      => __( 'Enable the shipping calculator on the cart page', 'woocommerce' ),
 80                 'id'        => 'woocommerce_enable_shipping_calc',
 81                 'default'   => 'yes',
 82                 'type'      => 'checkbox',
 83                 'checkboxgroup'     => '',
 84                 'autoload'      => false
 85             ),
 86 
 87             array(
 88                 'desc'      => __( 'Hide shipping costs until an address is entered', 'woocommerce' ),
 89                 'id'        => 'woocommerce_shipping_cost_requires_address',
 90                 'default'   => 'no',
 91                 'type'      => 'checkbox',
 92                 'checkboxgroup'     => 'end',
 93                 'autoload'      => false
 94             ),
 95 
 96             array(
 97                 'title'     => __( 'Shipping Display Mode', 'woocommerce' ),
 98                 'desc'      => __( 'This controls how multiple shipping methods are displayed on the frontend.', 'woocommerce' ),
 99                 'id'        => 'woocommerce_shipping_method_format',
100                 'default'   => '',
101                 'type'      => 'radio',
102                 'options' => array(
103                     ''              => __( 'Display shipping methods with "radio" buttons', 'woocommerce' ),
104                     'select'        => __( 'Display shipping methods in a dropdown', 'woocommerce' ),
105                 ),
106                 'desc_tip'  =>  true,
107                 'autoload'      => false
108             ),
109 
110             array(
111                 'title'           => __( 'Shipping Destination', 'woocommerce' ),
112                 'desc'            => __( 'Ship to billing address by default', 'woocommerce' ),
113                 'id'              => 'woocommerce_ship_to_billing',
114                 'default'         => 'yes',
115                 'type'            => 'checkbox',
116                 'checkboxgroup'   => 'start',
117                 'autoload'        => false,
118                 'show_if_checked' => 'option',
119             ),
120 
121             array(
122                 'desc'            => __( 'Only ship to the users billing address', 'woocommerce' ),
123                 'id'              => 'woocommerce_ship_to_billing_address_only',
124                 'default'         => 'no',
125                 'type'            => 'checkbox',
126                 'checkboxgroup'   => 'end',
127                 'autoload'        => false,
128                 'show_if_checked' => 'yes',
129             ),
130 
131             array(
132                 'title' => __( 'Restrict shipping to Location(s)', 'woocommerce' ),
133                 'desc'      => sprintf( __( 'Choose which countries you want to ship to, or choose to ship to all <a href="%s">locations you sell to</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=general' ) ),
134                 'id'        => 'woocommerce_ship_to_countries',
135                 'default'   => '',
136                 'type'      => 'select',
137                 'class'     => 'chosen_select',
138                 'desc_tip'  => false,
139                 'options' => array(
140                     ''         => __( 'Ship to all countries you sell to', 'woocommerce' ),
141                     'all'      => __( 'Ship to all countries', 'woocommerce' ),
142                     'specific' => __( 'Ship to specific countries only', 'woocommerce' )
143                 )
144             ),
145 
146             array(
147                 'title' => __( 'Specific Countries', 'woocommerce' ),
148                 'desc'      => '',
149                 'id'        => 'woocommerce_specific_ship_to_countries',
150                 'css'       => '',
151                 'default'   => '',
152                 'type'      => 'multi_select_countries'
153             ),
154 
155             array(
156                 'type'      => 'shipping_methods',
157             ),
158 
159             array( 'type' => 'sectionend', 'id' => 'shipping_options' ),
160 
161         )); // End shipping settings
162     }
163 
164     /**
165      * Output the settings
166      */
167     public function output() {
168         global $current_section;
169 
170         // Load shipping methods so we can show any global options they may have
171         $shipping_methods = WC()->shipping->load_shipping_methods();
172 
173         if ( $current_section ) {
174             foreach ( $shipping_methods as $method ) {
175                 if ( strtolower( get_class( $method ) ) == strtolower( $current_section ) && $method->has_settings() ) {
176                     $method->admin_options();
177                     break;
178                 }
179             }
180         } else {
181             $settings = $this->get_settings();
182 
183             WC_Admin_Settings::output_fields( $settings );
184         }
185     }
186 
187     /**
188      * Output shipping method settings.
189      *
190      * @access public
191      * @return void
192      */
193     public function shipping_methods_setting() {
194         $default_shipping_method = esc_attr( get_option('woocommerce_default_shipping_method') );
195         ?>
196         <tr valign="top">
197             <th scope="row" class="titledesc"><?php _e( 'Shipping Methods', 'woocommerce' ) ?></th>
198             <td class="forminp">
199                 <table class="wc_shipping widefat" cellspacing="0">
200                     <thead>
201                         <tr>
202                             <th class="default"><?php _e( 'Default', 'woocommerce' ); ?></th>
203                             <th class="name"><?php _e( 'Name', 'woocommerce' ); ?></th>
204                             <th class="id"><?php _e( 'ID', 'woocommerce' ); ?></th>
205                             <th class="status"><?php _e( 'Status', 'woocommerce' ); ?></th>
206                             <th class="settings">&nbsp;</th>
207                         </tr>
208                     </thead>
209                     <tfoot>
210                         <tr>
211                             <th width="1%" class="default">
212                                 <input type="radio" name="default_shipping_method" value="" <?php checked( $default_shipping_method, '' ); ?> />
213                             </th>
214                             <th><?php _e( 'Automatic', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'The cheapest available shipping method will be selected by default.', 'woocommerce' ); ?>">[?]</a></th>
215                             <th colspan="3"><span class="description"><?php _e( 'Drag and drop the above shipping methods to control their display order.', 'woocommerce' ); ?></span></th>
216                         </tr>
217                     </tfoot>
218                     <tbody>
219                         <?php
220                         foreach ( WC()->shipping->load_shipping_methods() as $key => $method ) {
221                             echo '<tr>
222                                 <td width="1%" class="default">
223                                     <input type="radio" name="default_shipping_method" value="' . esc_attr( $method->id ) . '" ' . checked( $default_shipping_method, $method->id, false ) . ' />
224                                     <input type="hidden" name="method_order[]" value="' . esc_attr( $method->id ) . '" />
225                                 </td>
226                                 <td class="name">
227                                     ' . $method->get_title() . '
228                                 </td>
229                                 <td class="id">
230                                     ' . $method->id . '
231                                 </td>
232                                 <td class="status">';
233 
234                             if ( $method->enabled == 'yes' )
235                                 echo '<span class="status-enabled tips" data-tip="' . __ ( 'Enabled', 'woocommerce' ) . '">' . __ ( 'Enabled', 'woocommerce' ) . '</span>';
236                             else
237                                 echo '-';
238 
239                             echo '</td>
240                                 <td class="settings">';
241 
242                             if ( $method->has_settings ) {
243                                 echo '<a class="button" href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping&section=' . strtolower( get_class( $method ) ) ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>';
244                             }
245 
246                             echo '</td>
247                             </tr>';
248                         }
249                         ?>
250                     </tbody>
251                 </table>
252             </td>
253         </tr>
254         <?php
255     }
256 
257     /**
258      * Save settings
259      */
260     public function save() {
261         global $current_section;
262 
263         if ( ! $current_section ) {
264 
265             $settings = $this->get_settings();
266 
267             WC_Admin_Settings::save_fields( $settings );
268             WC()->shipping->process_admin_options();
269 
270         } elseif ( class_exists( $current_section ) ) {
271 
272             $current_section_class = new $current_section();
273 
274             do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section_class->id );
275         }
276     }
277 }
278 
279 endif;
280 
281 return new WC_Settings_Shipping();
282 
WooCommerce API documentation generated by ApiGen 2.8.0