| @@ -7,27 +7,15 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * @since 3.0.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -use WC_Shipping_Method; | |
| 12 | -use MakeCommerce\Shipping\Method\ShippingClasses; | |
| 11 | +abstract class Method extends \WC_Shipping_Method { | |
| 13 | 12 | |
| 14 | -abstract class Method extends WC_Shipping_Method { | |
| 15 | - use ShippingClasses; | |
| 13 | + use \MakeCommerce\Shipping\Method\ShippingClasses; | |
| 16 | 14 | |
| 17 | 15 | public $id; |
| 18 | 16 | public $instance_id; |
| 19 | - public $availability; | |
| 20 | - public $countries; | |
| 21 | 17 | |
| 22 | - protected $ext; | |
| 23 | - protected $order_country; | |
| 24 | - | |
| 25 | - private $enable; | |
| 26 | - private $free_shipping_min_amount; | |
| 27 | - private $maximum_weight; | |
| 28 | - private $name_ext; | |
| 29 | - | |
| 30 | 18 | public $supports = array( |
| 31 | 19 | 'shipping-zones', |
| 32 | 20 | 'instance-settings', |
| 33 | 21 | 'instance-settings-modal', |
| @@ -42,10 +30,10 @@ | ||
| 42 | 30 | public function __construct( $instance_id = 0 ) { |
| 43 | 31 | |
| 44 | 32 | //set required properties |
| 45 | 33 | $this->instance_id = absint( $instance_id ); |
| 46 | - $this->id = $this->identifier . '_' . $this->carrier_id; | |
| 47 | - $this->ext = $this->carrier_id; | |
| 34 | + $this->id = $this->identifier . '_' . mb_strtolower( $this->carrier ); | |
| 35 | + $this->ext = mb_strtolower( $this->carrier ); | |
| 48 | 36 | $this->title = $this->return_method_title(); |
| 49 | 37 | $this->method_title = $this->title; |
| 50 | 38 | $this->name_ext = $this->title; |
| 51 | 39 | |
| @@ -62,10 +50,8 @@ | ||
| 62 | 50 | $this->set_hooks(); |
| 63 | 51 | |
| 64 | 52 | //initilize method type |
| 65 | 53 | $this->initialize(); |
| 66 | - | |
| 67 | - $this->fields_validation( $this->settings ); | |
| 68 | 54 | } |
| 69 | 55 | |
| 70 | 56 | /** |
| 71 | 57 | * Set hooks used by all WooCommerce shipping methods |
| @@ -75,9 +61,8 @@ | ||
| 75 | 61 | final public function set_hooks() { |
| 76 | 62 | |
| 77 | 63 | //update options |
| 78 | 64 | add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 79 | - add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, array( $this, 'fields_validation' ) ); | |
| 80 | 65 | |
| 81 | 66 | //Woocommerce Multilingual overrides (change some titles) |
| 82 | 67 | if ( \MakeCommerce\i18n::using_language_plugins() ) { |
| 83 | 68 | add_filter( 'woocommerce_package_rates', array( $this, 'override_label_translation' ), 50 ); |
| @@ -89,71 +74,8 @@ | ||
| 89 | 74 | add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_checkout_fields' ) ); |
| 90 | 75 | } |
| 91 | 76 | |
| 92 | 77 | /** |
| 93 | - * Add check for shipping settings | |
| 94 | - * Checks if certain fields are valid | |
| 95 | - * | |
| 96 | - * @since 3.2.0 | |
| 97 | - */ | |
| 98 | - function fields_validation( $fields ) { | |
| 99 | - // Phone not set, can not validate | |
| 100 | - if ( !isset( $fields['shop_phone'] ) ) { | |
| 101 | - return $fields; | |
| 102 | - } | |
| 103 | - $fields['shop_phone'] = $this->sanitize_phone_number( $fields['shop_phone'] ); | |
| 104 | - if ( $fields['shop_phone'] != '' && !$this->valid_phone_number($fields['shop_phone']) ) { | |
| 105 | - add_action( 'admin_notices', array( $this, 'invalid_phone_number_notice' ) ); | |
| 106 | - } | |
| 107 | - return $fields; | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * Add notice for invalid phone number | |
| 112 | - * Adds shipping method specific notice to wp admin | |
| 113 | - * | |
| 114 | - * @since 3.2.0 | |
| 115 | - */ | |
| 116 | - function invalid_phone_number_notice() { | |
| 117 | - | |
| 118 | - // keep track of the added notices | |
| 119 | - static $added = []; | |
| 120 | - | |
| 121 | - $user_id = get_current_user_id(); | |
| 122 | - | |
| 123 | - $unique_id = 'mc_phone_number_notice_dismissed_' . $this->id; | |
| 124 | - $dismiss_href = '?' . $unique_id; | |
| 125 | - | |
| 126 | - if ( isset( $_GET ) && !empty( $_GET ) ) { | |
| 127 | - $dismiss_href = '&' . $unique_id; | |
| 128 | - } | |
| 129 | - // dismiss button pressed, add to db | |
| 130 | - if ( isset( $_GET[$unique_id] ) ) { | |
| 131 | - add_user_meta( $user_id, $unique_id, 'true', true ); | |
| 132 | - } | |
| 133 | - // value is dismissed in db, do not display error | |
| 134 | - if ( get_user_meta( $user_id, $unique_id ) ) { | |
| 135 | - return; | |
| 136 | - } | |
| 137 | - // error already displayed, do not add duplicate | |
| 138 | - if ( in_array( $unique_id, $added ) ) { | |
| 139 | - return; | |
| 140 | - } else { | |
| 141 | - // add value to array to eliminate duplicates | |
| 142 | - array_push( $added, $unique_id ); | |
| 143 | - // display error | |
| 144 | - echo ' | |
| 145 | - <div class="notice notice-error"> | |
| 146 | - <p> | |
| 147 | - ' . $this->phone_number_validation_error() . ' | |
| 148 | - <a href="admin.php?page=wc-settings&tab=shipping§ion='. $this->id . '">' . __('Update here', 'wc_makecommerce_domain' ) . '</a> | |
| 149 | - <a style="float: right; " href="' . $dismiss_href . '">' . __( 'Dismiss', 'wc_makecommerce_domain' ) . '</a> | |
| 150 | - </p> | |
| 151 | - </div>'; | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | 78 | * Add check for parcelmachine selectbox |
| 157 | 79 | * Checks if something is chosen |
| 158 | 80 | * |
| 159 | 81 | * @since 3.0.4 |
| @@ -277,11 +199,9 @@ | ||
| 277 | 199 | public function check_number( $number, $valid_numbers ) { |
| 278 | 200 | |
| 279 | 201 | foreach ( $valid_numbers as $start_digits=>$lenghts ) { |
| 280 | 202 | foreach ( $lenghts as $lenght ) { |
| 281 | - // If the starting digit(s) matches OR it is '*' for any starting number | |
| 282 | - // AND the length matches | |
| 283 | - if ( ( substr( $number, 0, strlen( $start_digits ) ) == $start_digits || $start_digits === '*' ) && strlen( $number ) == $lenght ) { | |
| 203 | + if ( substr( $number, 0, strlen( $start_digits ) ) == $start_digits && strlen( $number ) == $lenght ) { | |
| 284 | 204 | return true; |
| 285 | 205 | } |
| 286 | 206 | } |
| 287 | 207 | } |
| @@ -395,9 +315,9 @@ | ||
| 395 | 315 | |
| 396 | 316 | //check all free shipping options: |
| 397 | 317 | $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount']; |
| 398 | 318 | |
| 399 | - if ( $free_shipping_min_amount && $package['cart_subtotal'] >= $free_shipping_min_amount ) { | |
| 319 | + if ( $free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount ) { | |
| 400 | 320 | $rate["cost"] = 0; |
| 401 | 321 | } |
| 402 | 322 | |
| 403 | 323 | //check only for parcelmachines |
| @@ -404,8 +324,9 @@ | ||
| 404 | 324 | if ( $this->type == "apt" ) { |
| 405 | 325 | |
| 406 | 326 | $free_shipping = true; |
| 407 | 327 | foreach ( $package['contents'] as $line ) { |
| 328 | + | |
| 408 | 329 | $free_shipping = get_post_meta( $line['product_id'], '_no_shipping_cost', true ) === 'yes' ? $free_shipping : false; |
| 409 | 330 | } |
| 410 | 331 | |
| 411 | 332 | if ( $free_shipping ) { |
| @@ -458,19 +379,16 @@ | ||
| 458 | 379 | |
| 459 | 380 | //Initialize instance form fields first |
| 460 | 381 | $this->initialize_instance_form_fields(); |
| 461 | 382 | |
| 462 | - if ( isset ( $_GET['section'] ) && $_GET['section'] == $this->id ) { | |
| 463 | - | |
| 464 | - //Initialize basic form fields | |
| 465 | - $this->initialize_basic_form_fields(); | |
| 383 | + //Initialize basic form fields | |
| 384 | + $this->initialize_basic_form_fields(); | |
| 466 | 385 | |
| 467 | - //Initialize method specific form fields | |
| 468 | - $this->initialize_method_type_form_fields(); | |
| 386 | + //Initialize method specific form fields | |
| 387 | + $this->initialize_method_type_form_fields(); | |
| 469 | 388 | |
| 470 | - //Initialize return address fields | |
| 471 | - $this->initialize_return_address_form_fields(); | |
| 472 | - } | |
| 389 | + //Initialize return address fields | |
| 390 | + $this->initalize_return_addres_form_fields(); | |
| 473 | 391 | } |
| 474 | 392 | |
| 475 | 393 | /** |
| 476 | 394 | * Initializes basic shared form fields |
| @@ -518,9 +436,9 @@ | ||
| 518 | 436 | |
| 519 | 437 | $this->form_fields['method_name'] = array( |
| 520 | 438 | 'title' => __( 'Shipping Method Title', 'wc_makecommerce_domain' ), |
| 521 | 439 | 'type' => 'text', |
| 522 | - 'default' => $this->carrier_title . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() ) | |
| 440 | + 'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', \MakeCommerce\i18n::get_two_char_locale() ) | |
| 523 | 441 | ); |
| 524 | 442 | } else { |
| 525 | 443 | foreach ( $languages as $language_code => $language ) { |
| 526 | 444 | $this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array( |
| @@ -525,9 +443,9 @@ | ||
| 525 | 443 | foreach ( $languages as $language_code => $language ) { |
| 526 | 444 | $this->form_fields['method_name_'.substr( $language_code, 0, 2 )] = array( |
| 527 | 445 | 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)), |
| 528 | 446 | 'type' => 'text', |
| 529 | - 'default' => $this->carrier_title . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) ) | |
| 447 | + 'default' => $this->carrier . " " . \MakeCommerce\i18n::get_string_from_mo( $this->identifier, 'wc_makecommerce_domain', substr( $language_code, 0, 2 ) ) | |
| 530 | 448 | ); |
| 531 | 449 | } |
| 532 | 450 | } |
| 533 | 451 | |
| @@ -589,14 +507,14 @@ | ||
| 589 | 507 | * Used by all shipping methods |
| 590 | 508 | * |
| 591 | 509 | * @since 3.0.0 |
| 592 | 510 | */ |
| 593 | - public function initialize_return_address_form_fields() { | |
| 511 | + final public function initalize_return_addres_form_fields() { | |
| 594 | 512 | |
| 595 | 513 | $this->form_fields['return_address'] = array( |
| 596 | 514 | 'type' => 'title', |
| 597 | 515 | 'title' => __( 'Return address', 'wc_makecommerce_domain' ), |
| 598 | - 'description' => sprintf( __( 'Please define return address for %s shipments.<br><b>All fields are required.</b>', 'wc_makecommerce_domain' ), $this->carrier ) | |
| 516 | + 'description' => sprintf( __( 'Please define return address for %s shipments', 'wc_makecommerce_domain' ), $this->carrier ) | |
| 599 | 517 | ); |
| 600 | 518 | |
| 601 | 519 | $this->form_fields['shop_name'] = array( |
| 602 | 520 | 'type' => 'text', |
| @@ -605,9 +523,9 @@ | ||
| 605 | 523 | ); |
| 606 | 524 | |
| 607 | 525 | $this->form_fields['shop_phone'] = array( |
| 608 | 526 | 'type' => 'text', |
| 609 | - 'title' => __( 'Shop phone (mobile)', 'wc_makecommerce_domain' ), | |
| 527 | + 'title' => __( 'Shop phone', 'wc_makecommerce_domain' ), | |
| 610 | 528 | 'class' => 'input-text regular-input', |
| 611 | 529 | ); |
| 612 | 530 | |
| 613 | 531 | $this->form_fields['shop_email'] = array( |
| @@ -630,78 +548,36 @@ | ||
| 630 | 548 | $this->form_fields['shop_address_city'] = array( |
| 631 | 549 | 'type' => 'text', |
| 632 | 550 | 'title' => __( 'Shop address city', 'wc_makecommerce_domain' ), |
| 633 | 551 | 'class' => 'input-text regular-input', |
| 634 | - ); | |
| 552 | + ); | |
| 635 | 553 | |
| 554 | + $this->form_fields['shop_address_street'] = array( | |
| 555 | + 'type' => 'text', | |
| 556 | + 'title' => __( 'Shop address street', 'wc_makecommerce_domain' ), | |
| 557 | + 'class' => 'input-text regular-input', | |
| 558 | + ); | |
| 559 | + | |
| 636 | 560 | $this->form_fields['shop_postal_code'] = array( |
| 637 | 561 | 'type' => 'text', |
| 638 | 562 | 'title' => __( 'Shop postal code', 'wc_makecommerce_domain' ), |
| 639 | 563 | 'class' => 'input-text regular-input', |
| 640 | 564 | ); |
| 641 | - | |
| 642 | - $this->form_fields['shop_address_street'] = array( | |
| 643 | - 'type' => 'text', | |
| 644 | - 'title' => __( 'Shop address street', 'wc_makecommerce_domain' ), | |
| 645 | - 'class' => 'input-text regular-input', | |
| 646 | - ); | |
| 647 | 565 | } |
| 648 | 566 | |
| 649 | 567 | /** |
| 650 | - * Initializes method type specific form fields | |
| 651 | - * Loads method specific fields | |
| 568 | + * Initializes method type, required function for all method types | |
| 652 | 569 | * |
| 653 | 570 | * @since 3.0.0 |
| 654 | 571 | */ |
| 655 | - public function initialize_method_type_form_fields() { | |
| 572 | + abstract public function initialize(); | |
| 656 | 573 | |
| 657 | - //Initialize method specific fields | |
| 658 | - $this->initialize_method_form_fields(); | |
| 659 | - | |
| 660 | - $this->form_fields['service_user'] = array( | |
| 661 | - 'title' => sprintf( __( '%s web services username', 'wc_makecommerce_domain' ), $this->service_name ), | |
| 662 | - 'type' => 'text', | |
| 663 | - 'default' => '' | |
| 664 | - ); | |
| 665 | - | |
| 666 | - $this->form_fields['service_password'] = array( | |
| 667 | - 'title' => sprintf( __( '%s web services password', 'wc_makecommerce_domain' ), $this->service_name ), | |
| 668 | - 'type' => 'text', | |
| 669 | - 'default' => '' | |
| 670 | - ); | |
| 671 | - } | |
| 672 | - | |
| 673 | 574 | /** |
| 674 | - * Checks if an object of a class already has the filter initialized | |
| 675 | - * Returns true if it is added, false if it is not | |
| 676 | - * | |
| 677 | - * @since 3.5.0 | |
| 678 | - */ | |
| 679 | - public function check_filter_hook_initialization( $filter, $priority = 10 ) { | |
| 680 | - | |
| 681 | - global $wp_filter; | |
| 682 | - $hook = $wp_filter[ $filter ] ?? []; | |
| 683 | - | |
| 684 | - if ( empty( $hook ) ) { | |
| 685 | - return false; | |
| 686 | - } | |
| 687 | - | |
| 688 | - // Check if the object instance is already registered as a callback | |
| 689 | - foreach ( $hook->callbacks[$priority] ?? [] as $callback ) { | |
| 690 | - if ( is_array($callback['function'] ) && $callback['function'][0]->id === $this->id ) { | |
| 691 | - return true; | |
| 692 | - } | |
| 693 | - } | |
| 694 | - | |
| 695 | - return false; | |
| 696 | - } | |
| 697 | - | |
| 698 | - /** | |
| 699 | - * Initializes method type, required function for all method types | |
| 575 | + * Initializes method type form fields | |
| 700 | 576 | * |
| 701 | 577 | * @since 3.0.0 |
| 702 | 578 | */ |
| 703 | - abstract public function initialize(); | |
| 579 | + abstract public function initialize_method_type_form_fields(); | |
| 704 | 580 | |
| 705 | 581 | /** |
| 706 | 582 | * Sets title of the method |
| 707 | 583 | * |