PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / src / Frontend / Delivery_Day.php

Delivery_Day.php in PostNL for WooCommerce 5.9.12, at src/Frontend/Delivery_Day.php

218 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Frontend/Delivery_Day file.
4 *
5 * @package PostNLWooCommerce\Frontend
6 */
7
8 namespace PostNLWooCommerce\Frontend;
9
10 use PostNLWooCommerce\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class Delivery_Day
18 *
19 * @package PostNLWooCommerce\Frontend
20 */
21 class Delivery_Day extends Base {
22
23 /**
24 * Set the primary field name.
25 */
26 public function set_primary_field_name() {
27 $this->primary_field = 'delivery_day';
28 }
29
30 /**
31 * Set the template filename.
32 */
33 public function set_template_file() {
34 $this->template_file = 'checkout/postnl-delivery-day.php';
35 }
36
37 /**
38 * List of frontend delivery day fields.
39 */
40 public function get_fields() {
41 $fields = array(
42 array(
43 'id' => $this->prefix . $this->primary_field,
44 'primary' => true,
45 'error_text' => esc_html__( 'Please choose the delivery day!', 'postnl-for-woocommerce' ),
46 ),
47 array(
48 'id' => $this->prefix . $this->primary_field . '_date',
49 'primary' => false,
50 'hidden' => true,
51 ),
52 array(
53 'id' => $this->prefix . $this->primary_field . '_from',
54 'primary' => false,
55 'hidden' => true,
56 ),
57 array(
58 'id' => $this->prefix . $this->primary_field . '_to',
59 'primary' => false,
60 'hidden' => true,
61 ),
62 array(
63 'id' => $this->prefix . $this->primary_field . '_price',
64 'primary' => false,
65 'hidden' => true,
66 ),
67 array(
68 'id' => $this->prefix . $this->primary_field . '_type',
69 'primary' => false,
70 'hidden' => true,
71 ),
72 array(
73 'id' => $this->prefix . $this->primary_field . '_fees',
74 'primary' => false,
75 'hidden' => true,
76 ),
77 );
78
79 return apply_filters( 'postnl_frontend_delivery_day_fields', $fields );
80 }
81
82 /**
83 * Check if this feature is enabled from the settings.
84 *
85 * @return bool
86 */
87 public function is_enabled() {
88 return true;
89 }
90
91 /**
92 * Check if customer allowed to choose a delivery day.
93 *
94 * @return bool
95 */
96 public function is_customer_allowed_to_pick_delivery_day() {
97 return $this->settings->is_delivery_days_enabled();
98 }
99
100 /**
101 * Adding a tab in the frontend checkout.
102 *
103 * @param array $tabs List of displayed tabs.
104 * @param array $response Response from PostNL Checkout Rest API.
105 *
106 * @return array
107 */
108 public function add_checkout_tab( $tabs, $response ) {
109 if ( empty( $response['DeliveryOptions'] ) ) {
110 return $tabs;
111 }
112
113 $tabs[] = array(
114 'id' => $this->primary_field,
115 'name' => esc_html__( 'Delivery', 'postnl-for-woocommerce' ),
116 );
117
118 return $tabs;
119 }
120
121 /**
122 * Get delivery option value from the API response.
123 *
124 * @param array $response PostNL API response.
125 * @param array $post_data Post data on checkout page.
126 *
127 * @return array.
128 */
129 public function get_content_data( $response, $post_data ) {
130 if ( empty( $response['DeliveryOptions'] ) ) {
131 return array();
132 }
133
134 $non_standard_fees = Base::non_standard_fees_data();
135 $return_data = $this->get_init_content_data( $post_data );
136 $show_delivery_days = $this->is_customer_allowed_to_pick_delivery_day();
137 $return_data['is_delivery_days_enabled'] = $show_delivery_days;
138 $is_free_shipping = Utils::is_free_shipping_applied() || $this->is_postnl_rate_free();
139
140 foreach ( $response['DeliveryOptions'] as $delivery_option ) {
141 if ( empty( $delivery_option['DeliveryDate'] ) || empty( $delivery_option['Timeframe'] ) ) {
142 continue;
143 }
144
145 $options = array_map(
146 function ( $timeframe ) use ( $non_standard_fees, $is_free_shipping ) {
147 $type = array_shift( $timeframe['Options'] );
148 $price = ( $is_free_shipping || ! isset( $non_standard_fees[ $type ] ) ) ? 0 : $non_standard_fees[ $type ]['fee_price'];
149
150 return array(
151 'from' => Utils::get_hour_min( $timeframe['From'] ),
152 'to' => Utils::get_hour_min( $timeframe['To'] ),
153 'type' => $type,
154 'price' => $price,
155 'price_display' => Utils::get_fee_total_price( $price ),
156 'price_formatted' => Utils::get_formatted_fee_total_price( $price ),
157 );
158 },
159 $delivery_option['Timeframe']
160 );
161
162 if ( ! $show_delivery_days ) {
163 $options = array_filter(
164 $options,
165 function ( $option ) {
166 return $option['price'] === 0;
167 }
168 );
169
170 if ( empty( $options ) ) {
171 continue; // Looking for a delivery day with free option.
172 }
173
174 $options = array_slice( $options, 0, 1 ); // Keep only the first free option.
175 }
176
177 $timestamp = strtotime( $delivery_option['DeliveryDate'] );
178 $day = strtolower( gmdate( 'D', $timestamp ) );
179 $days_of_week = Utils::days_of_week();
180 $day_name = $days_of_week[ $day ];
181
182 $return_data['delivery_options'][] = array(
183 'day' => $day_name,
184 'date' => gmdate( 'Y-m-d', $timestamp ),
185 'display_date' => wp_date( get_option( 'date_format' ), $timestamp ),
186 'options' => $options,
187 );
188
189 if ( ! $this->is_customer_allowed_to_pick_delivery_day() ) {
190 break;
191 }
192 }
193
194 return $return_data;
195 }
196
197 /**
198 * Validate delivery type fields.
199 *
200 * @param array $data Array of posted data.
201 * @param array $posted_data Array of global _POST data.
202 *
203 * @return array
204 */
205 public function validate_fields( $data, $posted_data ) {
206 foreach ( $this->get_fields() as $field ) {
207 if ( empty( $posted_data[ $field['id'] ] ) && ! empty( $field['error_text'] ) ) {
208 wc_add_notice( $field['error_text'], 'error' );
209 return $data;
210 }
211
212 $data[ $field['id'] ] = sanitize_text_field( wp_unslash( $posted_data[ $field['id'] ] ) );
213 }
214
215 return $data;
216 }
217 }
218