|
19 | 19 | add_filter('plugin_action_links_' . plugin_basename( __FILE__ ), array($settings_page, 'add_action_link'));
|
20 | 20 | }
|
21 | 21 |
|
22 |
| -function coopcycle_load_plugin_textdomain() { |
23 |
| - load_plugin_textdomain('coopcycle', false, basename(dirname( __FILE__ )) . '/i18n/languages/'); |
24 |
| -} |
25 |
| -add_action('plugins_loaded', 'coopcycle_load_plugin_textdomain'); |
| 22 | +add_action('plugins_loaded', 'coopcycle_init'); |
26 | 23 |
|
27 | 24 | // https://github.com/woocommerce/woocommerce/tree/trunk/packages/js/extend-cart-checkout-block
|
28 | 25 |
|
@@ -83,141 +80,148 @@ function ( $integration_registry ) {
|
83 | 80 | }
|
84 | 81 | );
|
85 | 82 |
|
86 |
| -/** |
87 |
| - * Check if WooCommerce is active |
88 |
| - */ |
89 |
| -// https://github.com/woocommerce/woocommerce/blob/trunk/docs/extension-development/check-if-woo-is-active.md |
90 |
| -if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { |
91 |
| - |
92 |
| - function coopcycle_shipping_method_init() { |
93 |
| - require __DIR__ . '/src/ShippingMethod.php'; |
94 |
| - } |
95 |
| - |
96 |
| - add_action('woocommerce_shipping_init', 'coopcycle_shipping_method_init'); |
| 83 | +function coopcycle_init() { |
97 | 84 |
|
98 |
| - function add_your_shipping_method($methods) { |
99 |
| - $methods['coopcycle_shipping_method'] = 'CoopCycle_ShippingMethod'; |
100 |
| - return $methods; |
101 |
| - } |
102 |
| - |
103 |
| - add_filter('woocommerce_shipping_methods', 'add_your_shipping_method'); |
104 |
| - |
105 |
| - // The checkout shortcode is used |
106 |
| - if (!CartCheckoutUtils::is_cart_block_default()) { |
107 |
| - require_once __DIR__ . '/legacy_shortcode.php'; |
108 |
| - } |
109 |
| - |
110 |
| - require_once __DIR__ . '/custom_colums.php'; |
| 85 | + load_plugin_textdomain('coopcycle', false, basename(dirname( __FILE__ )) . '/i18n/languages/'); |
111 | 86 |
|
112 |
| - function coopcycle_woocommerce_order_status_changed($order_id, $old_status, $new_status) { |
| 87 | + /** |
| 88 | + * Check if WooCommerce is active |
| 89 | + */ |
| 90 | + // https://github.com/woocommerce/woocommerce/blob/trunk/docs/extension-development/check-if-woo-is-active.md |
| 91 | + if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { |
113 | 92 |
|
114 |
| - if ('processing' === $new_status) { |
| 93 | + function coopcycle_shipping_method_init() { |
| 94 | + require __DIR__ . '/src/ShippingMethod.php'; |
| 95 | + } |
115 | 96 |
|
116 |
| - $order = wc_get_order($order_id); |
| 97 | + add_action('woocommerce_shipping_init', 'coopcycle_shipping_method_init'); |
117 | 98 |
|
118 |
| - if (!CoopCycle::accept_order($order)) { |
119 |
| - return; |
120 |
| - } |
| 99 | + function add_your_shipping_method($methods) { |
| 100 | + $methods['coopcycle_shipping_method'] = 'CoopCycle_ShippingMethod'; |
| 101 | + return $methods; |
| 102 | + } |
121 | 103 |
|
122 |
| - // Avoid creating the delivery twice |
123 |
| - // if the order changes to "processing" more than once |
124 |
| - $coopcycle_delivery = $order->get_meta('coopcycle_delivery', true); |
125 |
| - if (!empty($coopcycle_delivery)) { |
126 |
| - return; |
127 |
| - } |
| 104 | + add_filter('woocommerce_shipping_methods', 'add_your_shipping_method'); |
128 | 105 |
|
129 |
| - $shipping_date = $order->get_meta('shipping_date', true); |
130 |
| - |
131 |
| - // Array |
132 |
| - // ( |
133 |
| - // [first_name] |
134 |
| - // [last_name] |
135 |
| - // [company] |
136 |
| - // [address_1] |
137 |
| - // [address_2] |
138 |
| - // [city] |
139 |
| - // [state] |
140 |
| - // [postcode] |
141 |
| - // [country] |
142 |
| - // ) |
143 |
| - $shipping_address = $order->get_address('shipping'); |
144 |
| - |
145 |
| - $street_address = sprintf('%s %s %s', |
146 |
| - $shipping_address['address_1'], |
147 |
| - $shipping_address['postcode'], |
148 |
| - $shipping_address['city'] |
149 |
| - ); |
150 |
| - |
151 |
| - $contact_name = implode(' ', array_filter(array( |
152 |
| - $shipping_address['first_name'], |
153 |
| - $shipping_address['last_name'] |
154 |
| - ))); |
155 |
| - |
156 |
| - $wp_name = get_bloginfo('name'); |
157 |
| - $wp_url = get_bloginfo('url'); |
158 |
| - |
159 |
| - $items = ""; |
160 |
| - |
161 |
| - foreach ($order->get_items() as &$it) { |
162 |
| - $items .= sprintf("%sx %s \n", $it->get_quantity(), $it->get_name()); |
163 |
| - } |
| 106 | + // Check if the shortcode is used |
| 107 | + // https://stackoverflow.com/questions/77948982/check-programatically-if-cart-or-checkout-blocks-are-used-in-woocommerce |
| 108 | + if (!CartCheckoutUtils::is_cart_block_default()) { |
| 109 | + require_once __DIR__ . '/legacy_shortcode.php'; |
| 110 | + } |
164 | 111 |
|
165 |
| - $task_comments = |
166 |
| - /* translators: order number, website, url. */ |
167 |
| - sprintf(__('Order #%1$s from %2$s (%3$s)', 'coopcycle'), $order->get_order_number(), $wp_name, $wp_url); |
| 112 | + require_once __DIR__ . '/custom_colums.php'; |
| 113 | + |
| 114 | + function coopcycle_woocommerce_order_status_changed($order_id, $old_status, $new_status) { |
| 115 | + |
| 116 | + if ('processing' === $new_status) { |
| 117 | + |
| 118 | + $order = wc_get_order($order_id); |
| 119 | + |
| 120 | + if (!CoopCycle::accept_order($order)) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Avoid creating the delivery twice |
| 125 | + // if the order changes to "processing" more than once |
| 126 | + $coopcycle_delivery = $order->get_meta('coopcycle_delivery', true); |
| 127 | + if (!empty($coopcycle_delivery)) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + $shipping_date = $order->get_meta('shipping_date', true); |
| 132 | + |
| 133 | + // Array |
| 134 | + // ( |
| 135 | + // [first_name] |
| 136 | + // [last_name] |
| 137 | + // [company] |
| 138 | + // [address_1] |
| 139 | + // [address_2] |
| 140 | + // [city] |
| 141 | + // [state] |
| 142 | + // [postcode] |
| 143 | + // [country] |
| 144 | + // ) |
| 145 | + $shipping_address = $order->get_address('shipping'); |
| 146 | + |
| 147 | + $street_address = sprintf('%s %s %s', |
| 148 | + $shipping_address['address_1'], |
| 149 | + $shipping_address['postcode'], |
| 150 | + $shipping_address['city'] |
| 151 | + ); |
| 152 | + |
| 153 | + $contact_name = implode(' ', array_filter(array( |
| 154 | + $shipping_address['first_name'], |
| 155 | + $shipping_address['last_name'] |
| 156 | + ))); |
| 157 | + |
| 158 | + $wp_name = get_bloginfo('name'); |
| 159 | + $wp_url = get_bloginfo('url'); |
| 160 | + |
| 161 | + $items = ""; |
| 162 | + |
| 163 | + foreach ($order->get_items() as &$it) { |
| 164 | + $items .= sprintf("%sx %s \n", $it->get_quantity(), $it->get_name()); |
| 165 | + } |
| 166 | + |
| 167 | + $task_comments = |
| 168 | + /* translators: order number, website, url. */ |
| 169 | + sprintf(__('Order #%1$s from %2$s (%3$s)', 'coopcycle'), $order->get_order_number(), $wp_name, $wp_url); |
| 170 | + |
| 171 | + $customer_note = $order->get_customer_note(); |
| 172 | + if (!empty($customer_note)) { |
| 173 | + $task_comments .= "\n\n".$customer_note; |
| 174 | + } |
| 175 | + |
| 176 | + $task_comments.= "\n******\nItems : \n".$items; |
| 177 | + |
| 178 | + $data = array( |
| 179 | + // We only specify the dropoff data |
| 180 | + // Pickup is fully implicit |
| 181 | + 'pickup' => array( |
| 182 | + 'comments' => $task_comments, |
| 183 | + ), |
| 184 | + 'dropoff' => array( |
| 185 | + 'address' => array( |
| 186 | + 'streetAddress' => $street_address, |
| 187 | + 'contactName' => $contact_name, |
| 188 | + ), |
| 189 | + 'timeSlot' => $shipping_date, |
| 190 | + 'comments' => $task_comments, |
| 191 | + ) |
| 192 | + ); |
168 | 193 |
|
169 |
| - $customer_note = $order->get_customer_note(); |
170 |
| - if (!empty($customer_note)) { |
171 |
| - $task_comments .= "\n\n".$customer_note; |
172 |
| - } |
| 194 | + $phone_number = get_user_meta($order->get_customer_id(), 'billing_phone', true); |
| 195 | + if (!$phone_number) { |
| 196 | + $phone_number = $order->get_billing_phone(); |
| 197 | + } |
173 | 198 |
|
174 |
| - $task_comments.= "\n******\nItems : \n".$items; |
175 |
| - |
176 |
| - $data = array( |
177 |
| - // We only specify the dropoff data |
178 |
| - // Pickup is fully implicit |
179 |
| - 'pickup' => array( |
180 |
| - 'comments' => $task_comments, |
181 |
| - ), |
182 |
| - 'dropoff' => array( |
183 |
| - 'address' => array( |
184 |
| - 'streetAddress' => $street_address, |
185 |
| - 'contactName' => $contact_name, |
186 |
| - ), |
187 |
| - 'timeSlot' => $shipping_date, |
188 |
| - 'comments' => $task_comments, |
189 |
| - ) |
190 |
| - ); |
191 |
| - |
192 |
| - $phone_number = get_user_meta($order->get_customer_id(), 'billing_phone', true); |
193 |
| - if (!$phone_number) { |
194 |
| - $phone_number = $order->get_billing_phone(); |
195 |
| - } |
| 199 | + if ($phone_number) { |
| 200 | + $data['dropoff']['address']['telephone'] = $phone_number; |
| 201 | + } |
196 | 202 |
|
197 |
| - if ($phone_number) { |
198 |
| - $data['dropoff']['address']['telephone'] = $phone_number; |
199 |
| - } |
| 203 | + $http_client = CoopCycle::http_client(); |
200 | 204 |
|
201 |
| - $http_client = CoopCycle::http_client(); |
| 205 | + try { |
202 | 206 |
|
203 |
| - try { |
| 207 | + $delivery = $http_client->post('/api/deliveries', $data); |
204 | 208 |
|
205 |
| - $delivery = $http_client->post('/api/deliveries', $data); |
| 209 | + // Save useful info in order meta |
| 210 | + $order->update_meta_data('coopcycle_delivery', $delivery['@id']); |
206 | 211 |
|
207 |
| - // Save useful info in order meta |
208 |
| - $order->update_meta_data('coopcycle_delivery', $delivery['@id']); |
| 212 | + // Legacy |
| 213 | + $order->update_meta_data('task_id', $delivery['dropoff']['id']); |
209 | 214 |
|
210 |
| - // Legacy |
211 |
| - $order->update_meta_data('task_id', $delivery['dropoff']['id']); |
| 215 | + $order->save(); |
212 | 216 |
|
213 |
| - $order->save(); |
| 217 | + } catch (HttpClientException $e) { |
| 218 | + // TODO Store something to retry API call later? |
| 219 | + } |
214 | 220 |
|
215 |
| - } catch (HttpClientException $e) { |
216 |
| - // TODO Store something to retry API call later? |
217 | 221 | }
|
218 |
| - |
219 | 222 | }
|
| 223 | + |
| 224 | + add_action('woocommerce_order_status_changed', 'coopcycle_woocommerce_order_status_changed', 10, 3); |
220 | 225 | }
|
221 | 226 |
|
222 |
| - add_action('woocommerce_order_status_changed', 'coopcycle_woocommerce_order_status_changed', 10, 3); |
223 | 227 | }
|
0 commit comments