PluginProbe
MakeCommerce for WooCommerce / 2.5.1
MakeCommerce for WooCommerce v2.5.1
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
← All changes | makecommerce-simplecheckout.php +133 -43 2.2.02.5.1 View file →
@@ -23,9 +23,12 @@
23 23 echo json_encode(array('code' => -1));
24 24 exit;
25 25 }
26 26 $tmp_cart = new WC_Cart();
27 - foreach (json_decode($tmp_order->content) as $item_row) {
27 + $tmp_order->content = json_decode($tmp_order->content);
28 + $content = !empty($tmp_order->content->order) ? $tmp_order->content->order : $tmp_order->content;
29 + $coupons = !empty($tmp_order->content->coupons) ? $tmp_order->content->coupons : array();
30 + foreach ($content as $item_row) {
28 31 $tmp_cart->add_to_cart($item_row->id, $item_row->qty);
29 32 }
30 33 if ($tmp_order->order_id) {
31 34 $order = new WC_Order($tmp_order->order_id);
@@ -30,11 +33,37 @@
30 33 if ($tmp_order->order_id) {
31 34 $order = new WC_Order($tmp_order->order_id);
32 35 } else {
33 36 $order = wc_create_order();
34 - foreach (json_decode($tmp_order->content) as $item_row) {
35 - $item_id = $order->add_product(get_product($item_row->id), $item_row->qty);
37 + foreach ($content as $item_row) {
38 + $product_id = !empty($item_row->var) ? $item_row->var : $item_row->id;
39 + if (function_exists('wc_get_product')) {
40 + $item_id = $order->add_product(wc_get_product($product_id), $item_row->qty);
41 + } else {
42 + $item_id = $order->add_product(get_product($product_id), $item_row->qty);
43 + }
36 44 }
45 + $order->calculate_totals();
46 + $free_shipping = 0;
47 + foreach ($coupons as $coupon) {
48 + $coupon = new WC_Coupon($coupon);
49 + $amount = $coupon->get_amount();
50 + if ($coupon->is_type('percent')) {
51 + $amount = $order->get_total() / 100 * $amount;
52 + }
53 + if (method_exists($order, 'add_item')) {
54 + $item = new WC_Order_Item_Coupon();
55 + $item->set_props(array(
56 + 'code' => $coupon->get_code(),
57 + 'discount' => $amount,
58 + 'discount_tax' => 0
59 + ));
60 + $order->add_item($item);
61 + } else {
62 + //$order->add_code($coupon->get_code(), $amount);
63 + }
64 + if ((method_exists($coupon, 'get_free_shipping') && $coupon->get_free_shipping()) || $coupon->free_shipping) { $free_shipping = true; }
65 + }
37 66 }
38 67 $payment_method = new woocommerce_makecommerce();
39 68 $order->set_payment_method($payment_method);
40 69 $billing_address = array(
@@ -100,43 +129,57 @@
100 129 }
101 130 }
102 131 }
103 132 $order->set_address($billing_address, 'billing');
104 - $order->set_address($shipment_address, 'shipping');
105 - //$order->update_shipping($item_id, array('method_title' => 'Mingi transport nii', 'cost' => 1.99));
106 - $order->calculate_totals();
133 + if (empty($shipment_address)) {
134 + $order->set_address($billing_address, 'shipping');
135 + } else {
136 + $order->set_address($shipment_address, 'shipping');
137 + }
107 138 $order->remove_order_items('shipping');
108 139 if ($shipment_method) {
109 140 $package = $tmp_cart->get_shipping_packages();
110 141 if (!empty($package)) { $package = $package[0]; }
111 - $price_int = array_pop($shipment_method->get_rates_for_package($package));
112 - $price_int = !empty($price_int) ? $price_int->cost : 0;
142 + $price_int = $shipment_method->get_rates_for_package($package);
143 + $price_int = get_rate_without_taxes($shipment_method->id.':'.$shipment_method->instance_id, $price_int);
113 144 $price = !empty($cart_info->shipmentMethod->amount) ? (double)$cart_info->shipmentMethod->amount : 0;
114 - if ($price_int != $price) { error_log('Price does not match'); exit; }
115 - $rate = new WC_Shipping_Rate($shipment_method->id.':'.$shipment_method->instance_id, !empty($shipment_method->name_ext) ? $shipment_method->name_ext : $shipment_method->method_title, $price, array(), $shipment_method->id.':'.$shipment_method->instance_id);
116 - $order->add_shipping($rate);
145 + //if (round($price_int,2) != round($price,2)) { error_log('Price does not match ['.round($price,2).'] vs ['.round($price_int,2).']'); exit; }
146 + if ($free_shipping) $price = $price_int = 0;
147 + $rate = new WC_Shipping_Rate($shipment_method->id.':'.$shipment_method->instance_id, !empty($shipment_method->name_ext) ? $shipment_method->name_ext : $shipment_method->method_title, $price_int, array(), $shipment_method->id.':'.$shipment_method->instance_id);
148 + if (class_exists('WC_Order_Item_Shipping')) {
149 + $item = new WC_Order_Item_Shipping();
150 + $item->set_order_id($order->get_id());
151 + $item->set_shipping_rate($rate);
152 + $shipping_id = $item->save();
153 + } else {
154 + $order->add_shipping($rate);
155 + }
117 156 if (!empty($shipment_method->type) && $shipment_method->type === 'apt') {
118 157 $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId);
119 158 if ($machine) {
120 - update_post_meta($order->id, '_shipping_first_name', get_post_meta($order->id, '_billing_first_name', true));
121 - update_post_meta($order->id, '_shipping_last_name', get_post_meta($order->id, '_billing_last_name', true));
122 - update_post_meta($order->id, '_shipping_address_1', sanitize_text_field($machine['name']));
123 - update_post_meta($order->id, '_shipping_address_2', sanitize_text_field($machine['address']));
124 - update_post_meta($order->id, '_shipping_city', sanitize_text_field($machine['city']));
125 - update_post_meta($order->id, '_shipping_postcode', '');
126 - update_post_meta($order->id, '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId);
159 + update_post_meta($order->get_id(), '_shipping_first_name', get_post_meta($order->get_id(), '_billing_first_name', true));
160 + update_post_meta($order->get_id(), '_shipping_last_name', get_post_meta($order->get_id(), '_billing_last_name', true));
161 + update_post_meta($order->get_id(), '_shipping_address_1', sanitize_text_field($machine['name']));
162 + update_post_meta($order->get_id(), '_shipping_address_2', sanitize_text_field($machine['address']));
163 + update_post_meta($order->get_id(), '_shipping_city', sanitize_text_field($machine['city']));
164 + update_post_meta($order->get_id(), '_shipping_postcode', '');
165 + update_post_meta($order->get_id(), '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId);
127 166 }
128 167 }
129 - $order->calculate_totals();
130 168 }
131 - update_post_meta($order->id, '_makecommerce_sc_cart_id', $cart_info->cartId);
132 - $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->id, 'modified' => time()), array('cart_id' => $cart_info->cartId));
169 + update_post_meta($order->get_id(), '_makecommerce_sc_cart_id', $cart_info->cartId);
170 + $order->calculate_totals();
171 + if (!empty($tmp_order->content->discount)) { $order->set_discount_total($tmp_order->content->discount); error_log($tmp_order->content->discount); }
172 + if (!empty($tmp_order->content->discount_tax)) { $order->set_discount_tax($tmp_order->content->discount_tax); error_log($tmp_order->content->discount_tax); }
173 + $order->set_total($order->get_total() - ($order->get_discount_total() + $order->get_discount_tax()));
174 + $order->save();
175 + $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->get_id(), 'modified' => time()), array('cart_id' => $cart_info->cartId));
133 176 $locale = get_locale();
134 177 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
135 178 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
136 179 $response = array(
137 180 'cartId' => $cart_info->cartId,
138 - 'reference' => (string)$order->id,
181 + 'reference' => (string)$order->get_id(),
139 182 'locale' => $locale,
140 183 'transactionUrl' => array(
141 184 'returnUrl' => array(
142 185 'url' => site_url('/?mc_cart_update=1'),
@@ -199,8 +242,9 @@
199 242 }
200 243 function init() {
201 244 $this->init_form_fields();
202 245 $this->init_settings();
246 + $this->enabled = $this->settings['active'];
203 247 add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1);
204 248 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
205 249 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
206 250 }
@@ -207,8 +251,9 @@
207 251 function before_cart() {
208 252 if (!$this->is_available()) { return; }
209 253 if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') {
210 254 echo '<style>.shipping,.order-total{display:none;}</style>';
255 + echo '<style>.tax-rate{display:none;}</style>';
211 256 }
212 257 }
213 258 function install_tables() {
214 259 global $wpdb;
@@ -233,14 +278,31 @@
233 278 return;
234 279 }
235 280 $this->install_tables();
236 281 global $woocommerce, $wpdb;
237 - $data = array(); $qty = 0; $amount = 0;
282 + if (get_option('woocommerce_enable_guest_checkout') !== 'yes' && !is_user_logged_in()) {
283 + wc_add_notice(__('You have to log in to continue to checkout', 'wc_makecommerce_domain'), 'error');
284 + $redir_url = $woocommerce->cart->get_cart_url();
285 + wp_redirect($redir_url);
286 + exit;
287 + }
288 + $data = array(
289 + 'order' => array(),
290 + 'coupons' => $woocommerce->cart->get_applied_coupons(),
291 + 'discount' => $woocommerce->cart->get_cart_discount_total(),
292 + 'discount_tax' => $woocommerce->cart->get_cart_discount_tax_total()
293 + );
294 + $free_shipping = false;
295 + foreach ($woocommerce->cart->get_applied_coupons() as $coupon) {
296 + $coupon = new WC_Coupon($coupon);
297 + if ((method_exists($coupon, 'get_free_shipping') && $coupon->get_free_shipping()) || $coupon->free_shipping) { $free_shipping = true; }
298 + }
299 + $qty = 0; $amount = 0;
238 300 $cart = $woocommerce->cart->get_cart();
239 301 foreach ($cart as $cart_item) {
240 - $data[] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity']);
302 + $data['order'][] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity'], 'var' => $cart_item['variation_id']);
241 303 $qty += $cart_item['quantity'];
242 - $amount += $cart_item['line_total'];
304 + $amount += $cart_item['line_total'] + $cart_item['line_tax'];
243 305 }
244 306 if (count($data) > 0) {
245 307 $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array(
246 308 'created' => time(),
@@ -263,22 +325,29 @@
263 325 'cartToOrder' => $cart_to_order_url,
264 326 'calculateShipment' => $calculate_shipment_url,
265 327 'tos' => $tos_url
266 328 ),
267 - 'amount' => round($amount, 2),
329 + 'amount' => sprintf("%.2f", round(max($amount, 0.01), 2)),
268 330 'currency' => 'EUR',
269 - 'sourceCountry' => 'EE',
331 + 'sourceCountry' => WC()->countries->get_base_country(), // 'EE'
270 332 'locale' => $locale,
271 333 'shipmentMethods' => array()
272 334 );
273 335 $package = $woocommerce->cart->get_shipping_packages();
274 336 if (!empty($package)) { $package = $package[0]; }
275 - $zones = WC_Shipping_Zones::get_zones();
337 + $zones = array();
338 +
339 + $zone = new WC_Shipping_Zone(0);
340 + $zones[ $zone->get_id() ] = $zone->get_data();
341 + $zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
342 + $zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods();
343 + $zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );
276 344 $continents = WC()->countries->get_continents();
277 345 $method_country_x = array();
278 346 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
279 347 foreach ($zones as $zone) {
280 348 foreach ($zone['shipping_methods'] as $method_key => $method) {
349 + if ($method->enabled !== 'yes') { continue; }
281 350 if (!empty($supported_methods[$method->id])) {
282 351 foreach ($woocommerce->cart->get_shipping_packages() as $package) {
283 352 if (!$method->is_available($package)) { continue(2); }
284 353 }
@@ -285,31 +354,34 @@
285 354 $method_type = $supported_methods[$method->id];
286 355 if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); }
287 356 $carrier = array('countries' => array());
288 357 if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); }
289 - foreach ($zone['zone_locations'] as $location) {
290 - if ($location->type === 'continent' && !empty($continents[$location->code])) {
291 - $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]);
292 - $carrier['countries'] = array_merge($carrier['countries'], $countries);
293 - } else if ($location->type === 'state') {
294 - list($country, $state) = explode(':', $location->code);
295 - $carrier['countries'][] = $country;
296 - } else {
297 - $carrier['countries'][] = $location->code;
358 + if (empty($zone['zone_locations'])) {
359 + $carrier['countries'] = array_keys(WC()->countries->get_allowed_countries());
360 + } else {
361 + foreach ($zone['zone_locations'] as $location) {
362 + if ($location->type === 'continent' && !empty($continents[$location->code])) {
363 + $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]);
364 + $carrier['countries'] = array_merge($carrier['countries'], $countries);
365 + } else if ($location->type === 'state') {
366 + list($country, $state) = explode(':', $location->code);
367 + $carrier['countries'][] = $country;
368 + } else if ($location->type === 'country') {
369 + $carrier['countries'][] = $location->code;
370 + }
298 371 }
299 372 }
300 373 $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']);
301 - $price = $method->get_rates_for_package($package);
302 - $price = !empty($price[$method->id.':'.$method_key]) ? $price[$method->id.':'.$method_key]->cost : 0;
374 + $prices = $method->get_rates_for_package($package);
375 + $price = $free_shipping ? 0.00 : get_rate_with_taxes($method->id.':'.$method_key, $prices);
303 376 $carrier['type'] = strtoupper($method_type);
304 377 $carrier['name'] = $method->title;
305 378 $carrier['methodId'] = $method->id . ':' . $method_key;
306 - $carrier['amount'] = round($price, 2);
379 + $carrier['amount'] = sprintf("%.2f", round($price, 2));
307 380 $data['shipmentMethods'][] = $carrier;
308 381 }
309 382 }
310 383 }
311 -
312 384 $MK = mk_get_api();
313 385 $cart = $MK->createCart($data);
314 386 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id));
315 387 wp_redirect($cart->scoUrl);
@@ -327,9 +399,9 @@
327 399 $this->form_fields['active'] = array(
328 400 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
329 401 'type' => 'checkbox',
330 402 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'),
331 - 'default' => 'no'
403 + 'default' => 'yes'
332 404 );
333 405 $this->form_fields['shop_tos_url'] = array(
334 406 'title' => __('Shop ToS url', 'wc_makecommerce_domain'),
335 407 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'),
@@ -342,9 +414,9 @@
342 414 'default' => 'no'
343 415 );
344 416 }
345 417 public function is_available() {
346 - if ($this->settings['active'] == "yes") {
418 + if ($this->settings['active'] == "yes" || empty($this->settings['active'])) {
347 419 return true;
348 420 }
349 421 return false;
350 422 }
@@ -350,8 +422,26 @@
350 422 }
351 423
352 424 }
353 425
426 +}
427 +
428 +function get_rate_without_taxes($method, $rate) {
429 + if (empty($rate[$method])) { return 0; }
430 + $rate = $rate[$method];
431 + $price = $rate->cost;
432 + return $price;
433 +}
434 +
435 +function get_rate_with_taxes($method, $rate) {
436 + if (empty($rate[$method])) { return 0; }
437 + $rate = $rate[$method];
438 + $price = $rate->cost;
439 + if (empty($rate->taxes)) { return $price; }
440 + foreach ($rate->taxes as $tax) {
441 + $price += $tax;
442 + }
443 + return $price;
354 444 }
355 445
356 446 function woocommerce_payment_makecommerce_sc_add($methods) {
357 447 $methods[] = 'woocommerce_makecommerce_sc';