| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Shipping; |
| 4 |
|
| 5 |
/** |
| 6 |
* Creates minimal functionality needed for WooCommerce shipping method |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
abstract class Method extends \WC_Shipping_Method { |
| 12 |
|
| 13 |
public $id; |
| 14 |
public $instance_id; |
| 15 |
|
| 16 |
public $supports = array( |
| 17 |
'shipping-zones', |
| 18 |
'instance-settings', |
| 19 |
'instance-settings-modal', |
| 20 |
'settings' |
| 21 |
); |
| 22 |
|
| 23 |
/** |
| 24 |
* Construct WooCommerce shipping method |
| 25 |
* |
| 26 |
* @since 3.0.0 |
| 27 |
*/ |
| 28 |
public function __construct( $instance_id = 0 ) { |
| 29 |
|
| 30 |
//set required properties |
| 31 |
$this->instance_id = absint( $instance_id ); |
| 32 |
$this->id = $this->identifier . '_' . mb_strtolower( $this->carrier ); |
| 33 |
$this->ext = mb_strtolower( $this->carrier ); |
| 34 |
$this->title = $this->return_method_title(); |
| 35 |
$this->method_title = $this->title; |
| 36 |
$this->name_ext = $this->title; |
| 37 |
|
| 38 |
//get settings from WC_Shipping_Method |
| 39 |
$this->init_settings(); |
| 40 |
|
| 41 |
//set default settings used by all shipping methods |
| 42 |
$this->set_default_settings(); |
| 43 |
|
| 44 |
//initialize form fields for admin |
| 45 |
$this->initialize_form_fields(); |
| 46 |
|
| 47 |
//set hooks |
| 48 |
$this->set_hooks(); |
| 49 |
|
| 50 |
//initilize method type |
| 51 |
$this->initialize(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Set hooks used by all WooCommerce shipping methods |
| 56 |
* |
| 57 |
* @since 3.0.0 |
| 58 |
*/ |
| 59 |
final private function set_hooks() { |
| 60 |
|
| 61 |
//update options |
| 62 |
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 63 |
|
| 64 |
// Woocommerce Multilingual overrides (change some titles) |
| 65 |
if ( \MakeCommerce\i18n::using_language_plugins() ) { |
| 66 |
add_filter( 'woocommerce_package_rates', array( $this, 'override_label_translation' ), 50 ); |
| 67 |
add_filter( 'woocommerce_order_get_items', array( $this, 'override_shipping_title_translation' ), 50, 2 ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Label translation override for WPML (woocommerce_package_rates) |
| 73 |
* |
| 74 |
* @since 3.0.0 |
| 75 |
*/ |
| 76 |
public function override_label_translation( $rates ) { |
| 77 |
|
| 78 |
$id_instance = $this->id . ':' . $this->instance_id; |
| 79 |
|
| 80 |
if ( array_key_exists( $id_instance, $rates ) ) { |
| 81 |
$rates[$id_instance]->label = $this->title; |
| 82 |
} |
| 83 |
|
| 84 |
return $rates; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Title translation override for WPML (woocommerce_order_get_items) |
| 89 |
* |
| 90 |
* @since 3.0.0 |
| 91 |
*/ |
| 92 |
public function override_shipping_title_translation( $items, $order ) { |
| 93 |
|
| 94 |
foreach ( $items as $key => $item ) { |
| 95 |
|
| 96 |
if ( $item['type'] == 'shipping' ) { |
| 97 |
foreach ( $item['item_meta_array'] as $meta ) { |
| 98 |
|
| 99 |
if ( $meta->key === 'method_id' ) { |
| 100 |
|
| 101 |
$tmp = explode( ':', $meta->value ); |
| 102 |
|
| 103 |
if ( $tmp[0] == $this->id ) { |
| 104 |
$items[$key]['name'] = $this->title; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $items; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Sets default settings used by all shipping methods |
| 116 |
* |
| 117 |
* @since 3.0.0 |
| 118 |
*/ |
| 119 |
final private function set_default_settings() { |
| 120 |
|
| 121 |
$this->enable = !empty( $this->settings['active'] ) ? $this->settings['active'] : null; |
| 122 |
$this->availability = 'specific'; |
| 123 |
$this->order_country = 'unknown'; |
| 124 |
$this->maximum_weight = !empty( $this->settings['maximum_weight']) ? ( double )$this->settings['maximum_weight'] : 0; |
| 125 |
$this->free_shipping_min_amount = !empty( $this->settings['free_shipping_min_amount'] ) ? $this->settings['free_shipping_min_amount'] : 0; |
| 126 |
$this->countries = !empty( $this->settings['countries'] ) ? $this->settings['countries'] : array(); |
| 127 |
|
| 128 |
//Overrides title if its set in settings already |
| 129 |
$this->override_title(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if the shipping method is available (automatically called by parent \WC_Shipping_Method) |
| 134 |
* |
| 135 |
* @since 3.0.0 |
| 136 |
*/ |
| 137 |
public function is_available( $package ) { |
| 138 |
|
| 139 |
//check if cart weight exceeds max weight |
| 140 |
if ( WC()->cart->cart_contents_weight > $this->maximum_weight ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
$is_available = $this->enable === 'yes'; |
| 145 |
|
| 146 |
if ( !$is_available || !\MakeCommerce::is_api_set() ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$this->order_country = $package['destination']['country']; |
| 151 |
|
| 152 |
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Calculate price for shipping |
| 157 |
* |
| 158 |
* @since 3.0.0 |
| 159 |
*/ |
| 160 |
public function calculate_shipping( $package = array() ) { |
| 161 |
|
| 162 |
$price = isset( $this->instance_settings['price'] ) ? $this->instance_settings['price'] : ( isset( $this->settings['price_'.$package['destination']['country']] ) ? $this->settings['price_'.$package['destination']['country']] : 0 ); |
| 163 |
|
| 164 |
$free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount']; |
| 165 |
|
| 166 |
if ( $free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount ) { |
| 167 |
$price = 0; |
| 168 |
} |
| 169 |
|
| 170 |
//check only for parcelmachines |
| 171 |
if ( $this->type == "apt" ) { |
| 172 |
|
| 173 |
$free_shipping = true; |
| 174 |
foreach ( $package['contents'] as $line ) { |
| 175 |
|
| 176 |
$free_shipping = get_post_meta( $line['product_id'], '_no_shipping_cost', true ) === 'yes' ? $free_shipping : false; |
| 177 |
} |
| 178 |
|
| 179 |
if ( $free_shipping ) { |
| 180 |
$price = 0; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
//check if there is a free shipping coupon (if it's free then allow free shipping) |
| 185 |
if ( isset( $package['applied_coupons'] ) && $this->instance_settings["allow_free_shipping_coupons"] === "yes" ) { |
| 186 |
foreach ( $package['applied_coupons'] as $coupon_code ) { |
| 187 |
|
| 188 |
$coupon = new \WC_Coupon( $coupon_code ); |
| 189 |
if ( $coupon->get_free_shipping() === true ) { |
| 190 |
$price = 0; |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$rate = array( |
| 197 |
'id' => $this->get_rate_id(), |
| 198 |
'label' => $this->title, |
| 199 |
'cost' => $price, |
| 200 |
'package' => $package, |
| 201 |
'calc_tax' => 'per_order', |
| 202 |
); |
| 203 |
$this->add_rate( $rate ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* This function overrides shipping method title |
| 208 |
* if you have set language specific titles in admin. |
| 209 |
* |
| 210 |
* @since 3.0.0 |
| 211 |
*/ |
| 212 |
final private function override_title() { |
| 213 |
|
| 214 |
$language_code = \MakeCommerce\i18n::get_two_char_locale(); |
| 215 |
|
| 216 |
//default (no language) |
| 217 |
if ( !empty( $this->settings['method_name'] ) ) { |
| 218 |
$this->title = $this->settings['method_name']; |
| 219 |
} |
| 220 |
|
| 221 |
//if method name setting has been set with current language code |
| 222 |
if ( !empty( $this->settings['method_name_' . $language_code] ) ) { |
| 223 |
$this->title = $this->settings['method_name_' . $language_code]; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Initializes basic fields used by all shipping methods. |
| 229 |
* |
| 230 |
* @since 3.0.0 |
| 231 |
*/ |
| 232 |
public function initialize_form_fields() { |
| 233 |
|
| 234 |
//Initialize instance form fields first |
| 235 |
$this->initialize_instance_form_fields(); |
| 236 |
|
| 237 |
//Initialize basic form fields |
| 238 |
$this->initialize_basic_form_fields(); |
| 239 |
|
| 240 |
//Initialize method specific form fields |
| 241 |
$this->initialize_method_type_form_fields(); |
| 242 |
|
| 243 |
//Initialize return address fields |
| 244 |
$this->initalize_return_addres_form_fields(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Initializes basic shared form fields |
| 249 |
* Used by all shipping methods |
| 250 |
* |
| 251 |
* since 3.0.0 |
| 252 |
*/ |
| 253 |
final private function initialize_basic_form_fields() { |
| 254 |
|
| 255 |
$this->form_fields = array(); |
| 256 |
|
| 257 |
$this->form_fields['logo'] = array( |
| 258 |
'type' => 'title', |
| 259 |
'description' => \MakeCommerce::get_logo_html() |
| 260 |
); |
| 261 |
|
| 262 |
$this->form_fields['generic'] = array( |
| 263 |
'type' => 'title', |
| 264 |
'title' => __( 'Generic and pricing options', 'wc_makecommerce_domain' ), |
| 265 |
); |
| 266 |
|
| 267 |
$this->form_fields['active'] = array( |
| 268 |
'title' => __( 'Enable', 'wc_makecommerce_domain' ), |
| 269 |
'type' => 'checkbox', |
| 270 |
'label' => __( 'enabled', 'wc_makecommerce_domain' ), |
| 271 |
'default' => 'no', |
| 272 |
); |
| 273 |
|
| 274 |
$this->form_fields['maximum_weight'] = array( |
| 275 |
'title' => sprintf( __( 'Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain' ), get_option( 'woocommerce_weight_unit' ) ), |
| 276 |
'type' => 'text', |
| 277 |
'default' => $this->default_max_weight |
| 278 |
); |
| 279 |
|
| 280 |
$this->form_fields['look_and_feel'] = array( |
| 281 |
'type' => 'title', |
| 282 |
'title' => '<hr><br>'.__( 'Look and feel options', 'wc_makecommerce_domain' ), |
| 283 |
'description' => __( 'Options for presentation on check-out page', 'wc_makecommerce_domain' ) |
| 284 |
); |
| 285 |
|
| 286 |
//get a list of all active languages |
| 287 |
$languages = \MakeCommerce\i18n::get_active_languages(); |
| 288 |
|
| 289 |
if ( empty( $languages ) ) { |
| 290 |
|
| 291 |
$this->form_fields['method_name'] = array( |
| 292 |
'title' => __( 'Shipping Method Title', 'wc_makecommerce_domain' ), |
| 293 |
'type' => 'text', |
| 294 |
'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() ) |
| 295 |
); |
| 296 |
} else { |
| 297 |
foreach ( $languages as $language_code => $language ) { |
| 298 |
$this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array( |
| 299 |
'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)), |
| 300 |
'type' => 'text', |
| 301 |
'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) ) |
| 302 |
); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
$this->initialize_method_type_checkout_fields(); |
| 307 |
|
| 308 |
if ( \MakeCommerce::new_woo_version() ) { |
| 309 |
$a = 'admin.php?page=wc-settings&tab=advanced§ion=mk_api'; |
| 310 |
} else { |
| 311 |
$a = 'admin.php?page=wc-settings&tab=api§ion=mk_api'; |
| 312 |
} |
| 313 |
|
| 314 |
$this->form_fields['api_access'] = array( |
| 315 |
'type' => 'title', |
| 316 |
'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, |
| 317 |
'description' => sprintf(__('You can automatically create shipments into %s system and print the out the package labels right here, at the shop orders view. <br> Please set your %s web services account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain' ), $this->carrier, $this->carrier, admin_url( $a ) ) |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Initializes basic instance fields used by all shipping methods. |
| 323 |
* Automatically called by \WC_Shipping_Method |
| 324 |
* |
| 325 |
* @since 3.0.0 |
| 326 |
*/ |
| 327 |
final private function initialize_instance_form_fields() { |
| 328 |
|
| 329 |
$this->instance_form_fields = array(); |
| 330 |
|
| 331 |
$this->instance_form_fields['logo'] = array( |
| 332 |
'type' => 'title', |
| 333 |
'description' => \MakeCommerce::get_logo_html() |
| 334 |
); |
| 335 |
|
| 336 |
$this->instance_form_fields['price'] = array( |
| 337 |
'title' => __('Shipping price', 'wc_makecommerce_domain'), |
| 338 |
'type' => 'text', |
| 339 |
'default' => $this->default_price |
| 340 |
); |
| 341 |
|
| 342 |
$this->instance_form_fields['free_shipping_min_amount'] = array( |
| 343 |
'title' => __('Free shipping amount', 'wc_makecommerce_domain'), |
| 344 |
'type' => 'number', |
| 345 |
'default' => '0', |
| 346 |
'desc_tip' => __( '(0 means no free shipping)', 'wc_makecommerce_domain' ), |
| 347 |
); |
| 348 |
|
| 349 |
$this->instance_form_fields['allow_free_shipping_coupons'] = array( |
| 350 |
'title' => __( 'Free shipping coupons', 'wc_makecommerce_domain' ), |
| 351 |
'type' => 'checkbox', |
| 352 |
'default' => 'no', |
| 353 |
'desc_tip' => __( 'Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain' ), |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Initializes return address form fields |
| 359 |
* Used by all shipping methods |
| 360 |
* |
| 361 |
* @since 3.0.0 |
| 362 |
*/ |
| 363 |
final private function initalize_return_addres_form_fields() { |
| 364 |
|
| 365 |
$this->form_fields['return_address'] = array( |
| 366 |
'type' => 'title', |
| 367 |
'title' => __( 'Return address', 'wc_makecommerce_domain' ), |
| 368 |
'description' => __( 'Please define return address for Omniva shipments', 'wc_makecommerce_domain' ) |
| 369 |
); |
| 370 |
|
| 371 |
$this->form_fields['shop_name'] = array( |
| 372 |
'type' => 'text', |
| 373 |
'title' => __( 'Shop name', 'wc_makecommerce_domain' ), |
| 374 |
'class' => 'input-text regular-input', |
| 375 |
); |
| 376 |
|
| 377 |
$this->form_fields['shop_phone'] = array( |
| 378 |
'type' => 'text', |
| 379 |
'title' => __( 'Shop phone', 'wc_makecommerce_domain' ), |
| 380 |
'class' => 'input-text regular-input', |
| 381 |
); |
| 382 |
|
| 383 |
$this->form_fields['shop_email'] = array( |
| 384 |
'type' => 'text', |
| 385 |
'title' => __( 'Shop email', 'wc_makecommerce_domain' ), |
| 386 |
'class' => 'input-text regular-input', |
| 387 |
); |
| 388 |
|
| 389 |
$this->form_fields['shop_address_country'] = array( |
| 390 |
'type' => 'select', |
| 391 |
'title' => __( 'Shop address country', 'wc_makecommerce_domain' ), |
| 392 |
'options' => array( |
| 393 |
'EE' => 'EE', |
| 394 |
'LV' => 'LV', |
| 395 |
'LT' => 'LT', |
| 396 |
), |
| 397 |
'default' => 'EE', |
| 398 |
); |
| 399 |
|
| 400 |
$this->form_fields['shop_address_city'] = array( |
| 401 |
'type' => 'text', |
| 402 |
'title' => __( 'Shop address city', 'wc_makecommerce_domain' ), |
| 403 |
'class' => 'input-text regular-input', |
| 404 |
); |
| 405 |
|
| 406 |
$this->form_fields['shop_address_street'] = array( |
| 407 |
'type' => 'text', |
| 408 |
'title' => __( 'Shop address street', 'wc_makecommerce_domain' ), |
| 409 |
'class' => 'input-text regular-input', |
| 410 |
); |
| 411 |
|
| 412 |
$this->form_fields['shop_postal_code'] = array( |
| 413 |
'type' => 'text', |
| 414 |
'title' => __( 'Shop postal code', 'wc_makecommerce_domain' ), |
| 415 |
'class' => 'input-text regular-input', |
| 416 |
); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Initializes method type, required function for all method types |
| 421 |
* |
| 422 |
* @since 3.0.0 |
| 423 |
*/ |
| 424 |
abstract public function initialize(); |
| 425 |
|
| 426 |
/** |
| 427 |
* Initializes method type form fields |
| 428 |
* |
| 429 |
* @since 3.0.0 |
| 430 |
*/ |
| 431 |
abstract public function initialize_method_type_form_fields(); |
| 432 |
|
| 433 |
/** |
| 434 |
* Sets title of the method |
| 435 |
* |
| 436 |
* @since 3.0.0 |
| 437 |
*/ |
| 438 |
abstract public function return_method_title(); |
| 439 |
} |