css
2 years ago
js
2 years ago
class-css.php
2 years ago
class-date.php
2 years ago
class-delivery-type.php
2 years ago
class-display-field.php
2 years ago
class-js.php
2 years ago
class-main.php
2 years ago
class-order.php
2 years ago
class-pickup-location.php
2 years ago
class-shipping-method.php
2 years ago
class-time-range.php
2 years ago
class-time-slot.php
2 years ago
class-time.php
2 years ago
class-validate.php
2 years ago
class-woo-app.php
2 years ago
class-validate.php
118 lines
| 1 | <?php |
| 2 | if ( ! defined( 'WPINC' ) ) { |
| 3 | die; |
| 4 | } |
| 5 | class pi_dtt_validate{ |
| 6 | function __construct(){ |
| 7 | /* validating field */ |
| 8 | /** old validation method */ |
| 9 | //add_action('woocommerce_checkout_process', array($this,'validateCheckout')); |
| 10 | |
| 11 | /** |
| 12 | * New validation based on this hook |
| 13 | * https://github.com/woocommerce/woocommerce/blob/23710744c01ded649d6a94a4eaea8745e543159f/includes/class-wc-checkout.php#L878 |
| 14 | */ |
| 15 | add_action('woocommerce_after_checkout_validation', array($this,'validateCheckout'),10,2); |
| 16 | } |
| 17 | |
| 18 | function validateCheckout($data, $errors){ |
| 19 | |
| 20 | $pisol_disable_dtt_completely = apply_filters('pisol_disable_dtt_completely',false); |
| 21 | if($pisol_disable_dtt_completely){ |
| 22 | return ; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | $dateLabel = __('Date','pisol-dtt'); |
| 27 | $timeLabel = __('Time','pisol-dtt'); |
| 28 | |
| 29 | $date = isset($data['pi_system_delivery_date']) ? sanitize_text_field($data['pi_system_delivery_date']) : ""; |
| 30 | $time = isset($data['pi_delivery_time']) ? sanitize_text_field($data['pi_delivery_time']) : ""; |
| 31 | |
| 32 | if(pi_dtt_display_fields::showDateAndTime()): |
| 33 | if(pi_dtt_display_fields::isDateRequired()){ |
| 34 | if (!isset($data['pi_system_delivery_date']) || empty( $data['pi_system_delivery_date'])){ |
| 35 | $errors->add('error', sprintf(__( '<strong>%s</strong> is a required field','pisol-dtt' ), $dateLabel)); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | if(!empty($date)){ |
| 40 | if(!pi_dtt_date::isDateValid($date)){ |
| 41 | $errors->add('error', sprintf(__( '<strong>%s</strong> date you selected is not available any more, please refresh page to get available dates ','pisol-dtt' ), $dateLabel)); |
| 42 | } |
| 43 | } |
| 44 | }else{ |
| 45 | |
| 46 | if(!pi_dtt_date::isDateValid($date) && !empty($date)){ |
| 47 | $errors->add('error', sprintf(__( '<strong>%s</strong> date you selected is not available any more, please refresh page to get available dates ','pisol-dtt' ), $dateLabel)); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | if(pi_dtt_display_fields::enableTimeField() && pi_dtt_display_fields::isTimeRequired()){ |
| 53 | if (!isset($data['pi_delivery_time']) || empty($data['pi_delivery_time'])){ |
| 54 | $errors->add('error', sprintf(__( '<strong>%s</strong> is a required field','pisol-dtt' ), $timeLabel)); |
| 55 | } |
| 56 | |
| 57 | if(!empty($data['pi_delivery_time'])){ |
| 58 | if(!pisol_dtt_time::isTimeValid($date, $time)){ |
| 59 | $errors->add('error', sprintf(__( '<strong>%s</strong> time you inserted is not available any more, please refresh page to get available time','pisol-dtt' ), $timeLabel)); |
| 60 | } |
| 61 | } |
| 62 | }else{ |
| 63 | |
| 64 | if(!pisol_dtt_time::isTimeValid($date, $time) && !empty($time)){ |
| 65 | $errors->add('error', sprintf(__( '<strong>%s</strong> time you inserted is not available any more, please refresh page to get available time','pisol-dtt' ), $timeLabel)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | endif; |
| 70 | |
| 71 | |
| 72 | if(self::isLocationRequired()){ |
| 73 | |
| 74 | if (!isset($data['pickup_location']) || empty($data['pickup_location'])){ |
| 75 | $errors->add('error', sprintf(__( '<strong>%s</strong> is a required field','pisol-dtt' ), __('Pickup Location', 'pisol-dtt'))); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Required: |
| 83 | * if location is marked as required field in plugin setting |
| 84 | * and there is location created in plugin but user zone is not having any location |
| 85 | * |
| 86 | * not required: |
| 87 | * if user has marked it not required |
| 88 | * |
| 89 | * if user has marked it required but there is no pickup location created in the system |
| 90 | */ |
| 91 | static function isLocationRequired(){ |
| 92 | $required = 1; |
| 93 | |
| 94 | $type = pi_dtt_delivery_type::getType(); |
| 95 | |
| 96 | if( $type == 'pickup' && !empty($required) && $required == '1' && pi_dtt_pickup_location::isLocationPresentInSystem()){ |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | } |
| 105 | |
| 106 | add_action('wp_loaded',function(){ |
| 107 | /** |
| 108 | * This filter allow you to hide all the fields added by this plugin |
| 109 | * so you can use this to disable the plugin when you have virtual product in |
| 110 | * your cart |
| 111 | */ |
| 112 | $pisol_disable_dtt_completely = apply_filters('pisol_disable_dtt_completely',false); |
| 113 | if($pisol_disable_dtt_completely){ |
| 114 | return ; |
| 115 | } |
| 116 | |
| 117 | new pi_dtt_validate(); |
| 118 | }); |