1 <?php
  2 
  3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  4 
  5 /**
  6  * Local Pickup Shipping Method
  7  *
  8  * A simple shipping method allowing free pickup as a shipping method
  9  *
 10  * @class       WC_Shipping_Local_Pickup
 11  * @version     2.0.0
 12  * @package     WooCommerce/Classes/Shipping
 13  * @author      WooThemes
 14  */
 15 class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
 16 
 17     /**
 18      * __construct function.
 19      *
 20      * @access public
 21      * @return void
 22      */
 23     function __construct() {
 24         $this->id           = 'local_pickup';
 25         $this->method_title = __( 'Local Pickup', 'woocommerce' );
 26         $this->init();
 27     }
 28 
 29     /**
 30      * init function.
 31      *
 32      * @access public
 33      * @return void
 34      */
 35     function init() {
 36 
 37         // Load the settings.
 38         $this->init_form_fields();
 39         $this->init_settings();
 40 
 41         // Define user set variables
 42         $this->enabled      = $this->get_option( 'enabled' );
 43         $this->title        = $this->get_option( 'title' );
 44         $this->codes        = $this->get_option( 'codes' );
 45         $this->availability = $this->get_option( 'availability' );
 46         $this->countries    = $this->get_option( 'countries' );
 47 
 48         // Actions
 49         add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
 50     }
 51 
 52     /**
 53      * calculate_shipping function.
 54      *
 55      * @access public
 56      * @return void
 57      */
 58     function calculate_shipping() {
 59         $rate = array(
 60             'id'        => $this->id,
 61             'label'     => $this->title,
 62         );
 63         $this->add_rate($rate);
 64     }
 65 
 66     /**
 67      * init_form_fields function.
 68      *
 69      * @access public
 70      * @return void
 71      */
 72     function init_form_fields() {
 73         $this->form_fields = array(
 74             'enabled' => array(
 75                 'title'         => __( 'Enable', 'woocommerce' ),
 76                 'type'          => 'checkbox',
 77                 'label'         => __( 'Enable local pickup', 'woocommerce' ),
 78                 'default'       => 'no'
 79             ),
 80             'title' => array(
 81                 'title'         => __( 'Title', 'woocommerce' ),
 82                 'type'          => 'text',
 83                 'description'   => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
 84                 'default'       => __( 'Local Pickup', 'woocommerce' ),
 85                 'desc_tip'      => true,
 86             ),
 87             'codes' => array(
 88                 'title'         => __( 'Zip/Post Codes', 'woocommerce' ),
 89                 'type'          => 'textarea',
 90                 'description'   => __( 'What zip/post codes are available for local pickup? Separate codes with a comma. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'woocommerce' ),
 91                 'default'       => '',
 92                 'desc_tip'      => true,
 93                 'placeholder'   => '12345, 56789 etc'
 94             ),
 95             'availability' => array(
 96                 'title'         => __( 'Method availability', 'woocommerce' ),
 97                 'type'          => 'select',
 98                 'default'       => 'all',
 99                 'class'         => 'availability',
100                 'options'       => array(
101                     'all'       => __( 'All allowed countries', 'woocommerce' ),
102                     'specific'  => __( 'Specific Countries', 'woocommerce' )
103                 )
104             ),
105             'countries' => array(
106                 'title'         => __( 'Specific Countries', 'woocommerce' ),
107                 'type'          => 'multiselect',
108                 'class'         => 'chosen_select',
109                 'css'           => 'width: 450px;',
110                 'default'       => '',
111                 'options'       => WC()->countries->get_shipping_countries(),
112                 'custom_attributes' => array(
113                     'data-placeholder' => __( 'Select some countries', 'woocommerce' )
114                 )
115             )
116         );
117     }
118 
119     /**
120      * admin_options function.
121      *
122      * @access public
123      * @return void
124      */
125     function admin_options() {
126         global $woocommerce; ?>
127         <h3><?php echo $this->method_title; ?></h3>
128         <p><?php _e( 'Local pickup is a simple method which allows the customer to pick up their order themselves.', 'woocommerce' ); ?></p>
129         <table class="form-table">
130             <?php $this->generate_settings_html(); ?>
131         </table> <?php
132     }
133 
134     /**
135      * is_available function.
136      *
137      * @access public
138      * @param array $package
139      * @return bool
140      */
141     function is_available( $package ) {
142 
143         $is_available = true;
144 
145         if ( $this->enabled == "no" ) {
146 
147             $is_available = false;
148 
149         } else {
150 
151             // If post codes are listed, let's use them.
152             $codes = '';
153             if ( $this->codes != '' ) {
154                 foreach( explode( ',', $this->codes ) as $code ) {
155                     $codes[] = $this->clean( $code );
156                 }
157             }
158 
159             if ( is_array( $codes ) ) {
160 
161                 $found_match = false;
162 
163                 if ( in_array( $this->clean( $package['destination']['postcode'] ), $codes ) )
164                     $found_match = true;
165 
166                 // Wildcard search
167                 if ( ! $found_match ) {
168 
169                     $customer_postcode = $this->clean( $package['destination']['postcode'] );
170                     $customer_postcode_length = strlen( $customer_postcode );
171 
172                     for ( $i = 0; $i <= $customer_postcode_length; $i++ ) {
173 
174                         if ( in_array( $customer_postcode, $codes ) )
175                             $found_match = true;
176 
177                         $customer_postcode = substr( $customer_postcode, 0, -2 ) . '*';
178                     }
179                 }
180 
181                 if ( ! $found_match ) {
182 
183                     $is_available = false;
184 
185                 } else {
186 
187                     if ( $this->availability == 'specific' )
188                         $ship_to_countries = $this->countries;
189                     else
190                         $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
191 
192                     if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
193                         $is_available = false;
194                     }
195 
196                 }
197             } else {
198                 if ( $this->availability == 'specific' )
199                     $ship_to_countries = $this->countries;
200                 else
201                     $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
202 
203                 if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
204                     $is_available = false;
205                 }
206             }
207 
208         }
209 
210         return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
211     }
212 
213     /**
214      * clean function.
215      *
216      * @access public
217      * @param mixed $code
218      * @return string
219      */
220     function clean( $code ) {
221         return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
222     }
223 
224 }
WooCommerce API documentation generated by ApiGen 2.8.0