1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14
15 class WC_Shipping_International_Delivery extends WC_Shipping_Flat_Rate {
16
17 var $id = 'international_delivery';
18
19 20 21 22 23 24
25 public function __construct() {
26
27 $this->id = 'international_delivery';
28 $this->flat_rate_option = 'woocommerce_international_delivery_flat_rates';
29 $this->method_title = __( 'International Delivery', 'woocommerce' );
30 $this->method_description = __( 'International delivery based on flat rate shipping.', 'woocommerce' );
31
32 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
33 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_flat_rates' ) );
34
35 $this->init();
36 }
37
38
39 40 41 42 43 44
45 public function init_form_fields() {
46
47 $this->form_fields = array(
48 'enabled' => array(
49 'title' => __( 'Enable/Disable', 'woocommerce' ),
50 'type' => 'checkbox',
51 'label' => __( 'Enable this shipping method', 'woocommerce' ),
52 'default' => 'no'
53 ),
54 'title' => array(
55 'title' => __( 'Method Title', 'woocommerce' ),
56 'type' => 'text',
57 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
58 'default' => __( 'International Delivery', 'woocommerce' ),
59 'desc_tip' => true,
60 ),
61 'availability' => array(
62 'title' => __( 'Availability', 'woocommerce' ),
63 'type' => 'select',
64 'description' => '',
65 'default' => 'including',
66 'options' => array(
67 'including' => __( 'Selected countries', 'woocommerce' ),
68 'excluding' => __( 'Excluding selected countries', 'woocommerce' ),
69 )
70 ),
71 'countries' => array(
72 'title' => __( 'Countries', 'woocommerce' ),
73 'type' => 'multiselect',
74 'class' => 'chosen_select',
75 'css' => 'width: 450px;',
76 'default' => '',
77 'options' => WC()->countries->get_shipping_countries(),
78 'custom_attributes' => array(
79 'data-placeholder' => __( 'Select some countries', 'woocommerce' )
80 )
81 ),
82 'tax_status' => array(
83 'title' => __( 'Tax Status', 'woocommerce' ),
84 'type' => 'select',
85 'default' => 'taxable',
86 'options' => array(
87 'taxable' => __( 'Taxable', 'woocommerce' ),
88 'none' => _x( 'None', 'Tax status', 'woocommerce' )
89 )
90 ),
91 'type' => array(
92 'title' => __( 'Cost Added...', 'woocommerce' ),
93 'type' => 'select',
94 'default' => 'order',
95 'options' => array(
96 'order' => __( 'Per Order - charge shipping for the entire order as a whole', 'woocommerce' ),
97 'item' => __( 'Per Item - charge shipping for each item individually', 'woocommerce' ),
98 'class' => __( 'Per Class - charge shipping for each shipping class in an order', 'woocommerce' ),
99 ),
100 ),
101 'cost' => array(
102 'title' => __( 'Cost', 'woocommerce' ),
103 'type' => 'price',
104 'placeholder' => wc_format_localized_price( 0 ),
105 'description' => __( 'Cost excluding tax. Enter an amount, e.g. 2.50. Default is 0', 'woocommerce' ),
106 'default' => '',
107 'desc_tip' => true
108 ),
109 'fee' => array(
110 'title' => __( 'Handling Fee', 'woocommerce' ),
111 'type' => 'text',
112 'description' => __( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce' ),
113 'default' => '',
114 'desc_tip' => true,
115 'placeholder' => __( 'N/A', 'woocommerce' )
116 ),
117 'minimum_fee' => array(
118 'title' => __( 'Minimum Handling Fee', 'woocommerce' ),
119 'type' => 'decimal',
120 'description' => __( 'Enter a minimum fee amount. Fee\'s less than this will be increased. Leave blank to disable.', 'woocommerce' ),
121 'default' => '',
122 'desc_tip' => true,
123 'placeholder' => __( 'N/A', 'woocommerce' )
124 ),
125 );
126
127 }
128
129
130 131 132 133 134 135 136
137 public function is_available( $package ) {
138
139 if ( "no" === $this->enabled ) {
140 return false;
141 }
142
143 if ( 'including' === $this->availability ) {
144
145 if ( is_array( $this->countries ) && ! in_array( $package['destination']['country'], $this->countries ) ) {
146 return false;
147 }
148
149 } else {
150
151 if ( is_array( $this->countries ) && in_array( $package['destination']['country'], $this->countries ) ) {
152 return false;
153 }
154
155 }
156
157 return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package );
158 }
159
160 161 162 163 164 165 166
167 public function calculate_shipping( $package = array() ) {
168 $this->rates = array();
169
170 if ( 'order' === $this->type ) {
171
172 $shipping_total = $this->order_shipping( $package );
173
174 $rate = array(
175 'id' => $this->id,
176 'label' => $this->title,
177 'cost' => $shipping_total ? $shipping_total : 0,
178 );
179
180 } elseif ( 'class' === $this->type ) {
181
182 $shipping_total = $this->class_shipping( $package );
183
184 $rate = array(
185 'id' => $this->id,
186 'label' => $this->title,
187 'cost' => $shipping_total ? $shipping_total : 0,
188 );
189
190 } elseif ( 'item' === $this->type ) {
191
192 $costs = $this->item_shipping( $package );
193
194 if ( ! is_array( $costs ) ) {
195 $costs = array();
196 }
197
198 $rate = array(
199 'id' => $this->id,
200 'label' => $this->title,
201 'cost' => $costs,
202 'calc_tax' => 'per_item',
203 );
204 }
205
206 if ( isset( $rate ) ) {
207 $this->add_rate( $rate );
208 }
209 }
210
211 }