| @@ -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( |
| @@ -105,42 +134,52 @@ | ||
| 105 | 134 | $order->set_address($billing_address, 'shipping'); |
| 106 | 135 | } else { |
| 107 | 136 | $order->set_address($shipment_address, 'shipping'); |
| 108 | 137 | } |
| 109 | - //$order->update_shipping($item_id, array('method_title' => 'Mingi transport nii', 'cost' => 1.99)); | |
| 110 | - $order->calculate_totals(); | |
| 111 | 138 | $order->remove_order_items('shipping'); |
| 112 | 139 | if ($shipment_method) { |
| 113 | 140 | $package = $tmp_cart->get_shipping_packages(); |
| 114 | 141 | if (!empty($package)) { $package = $package[0]; } |
| 115 | - $price_int = array_pop($shipment_method->get_rates_for_package($package)); | |
| 116 | - $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); | |
| 117 | 144 | $price = !empty($cart_info->shipmentMethod->amount) ? (double)$cart_info->shipmentMethod->amount : 0; |
| 118 | - if ($price_int != $price) { error_log('Price does not match'); exit; } | |
| 119 | - $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); | |
| 120 | - $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 | + } | |
| 121 | 156 | if (!empty($shipment_method->type) && $shipment_method->type === 'apt') { |
| 122 | 157 | $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId); |
| 123 | 158 | if ($machine) { |
| 124 | - update_post_meta($order->id, '_shipping_first_name', get_post_meta($order->id, '_billing_first_name', true)); | |
| 125 | - update_post_meta($order->id, '_shipping_last_name', get_post_meta($order->id, '_billing_last_name', true)); | |
| 126 | - update_post_meta($order->id, '_shipping_address_1', sanitize_text_field($machine['name'])); | |
| 127 | - update_post_meta($order->id, '_shipping_address_2', sanitize_text_field($machine['address'])); | |
| 128 | - update_post_meta($order->id, '_shipping_city', sanitize_text_field($machine['city'])); | |
| 129 | - update_post_meta($order->id, '_shipping_postcode', ''); | |
| 130 | - 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); | |
| 131 | 166 | } |
| 132 | 167 | } |
| 133 | - $order->calculate_totals(); | |
| 134 | 168 | } |
| 135 | - update_post_meta($order->id, '_makecommerce_sc_cart_id', $cart_info->cartId); | |
| 136 | - $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)); | |
| 137 | 176 | $locale = get_locale(); |
| 138 | 177 | if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); } |
| 139 | 178 | $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale; |
| 140 | 179 | $response = array( |
| 141 | 180 | 'cartId' => $cart_info->cartId, |
| 142 | - 'reference' => (string)$order->id, | |
| 181 | + 'reference' => (string)$order->get_id(), | |
| 143 | 182 | 'locale' => $locale, |
| 144 | 183 | 'transactionUrl' => array( |
| 145 | 184 | 'returnUrl' => array( |
| 146 | 185 | 'url' => site_url('/?mc_cart_update=1'), |
| @@ -203,8 +242,9 @@ | ||
| 203 | 242 | } |
| 204 | 243 | function init() { |
| 205 | 244 | $this->init_form_fields(); |
| 206 | 245 | $this->init_settings(); |
| 246 | + $this->enabled = $this->settings['active']; | |
| 207 | 247 | add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1); |
| 208 | 248 | add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); |
| 209 | 249 | add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options')); |
| 210 | 250 | } |
| @@ -211,8 +251,9 @@ | ||
| 211 | 251 | function before_cart() { |
| 212 | 252 | if (!$this->is_available()) { return; } |
| 213 | 253 | if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') { |
| 214 | 254 | echo '<style>.shipping,.order-total{display:none;}</style>'; |
| 255 | + echo '<style>.tax-rate{display:none;}</style>'; | |
| 215 | 256 | } |
| 216 | 257 | } |
| 217 | 258 | function install_tables() { |
| 218 | 259 | global $wpdb; |
| @@ -237,14 +278,31 @@ | ||
| 237 | 278 | return; |
| 238 | 279 | } |
| 239 | 280 | $this->install_tables(); |
| 240 | 281 | global $woocommerce, $wpdb; |
| 241 | - $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; | |
| 242 | 300 | $cart = $woocommerce->cart->get_cart(); |
| 243 | 301 | foreach ($cart as $cart_item) { |
| 244 | - $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']); | |
| 245 | 303 | $qty += $cart_item['quantity']; |
| 246 | - $amount += $cart_item['line_total']; | |
| 304 | + $amount += $cart_item['line_total'] + $cart_item['line_tax']; | |
| 247 | 305 | } |
| 248 | 306 | if (count($data) > 0) { |
| 249 | 307 | $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array( |
| 250 | 308 | 'created' => time(), |
| @@ -267,22 +325,29 @@ | ||
| 267 | 325 | 'cartToOrder' => $cart_to_order_url, |
| 268 | 326 | 'calculateShipment' => $calculate_shipment_url, |
| 269 | 327 | 'tos' => $tos_url |
| 270 | 328 | ), |
| 271 | - 'amount' => round($amount, 2), | |
| 329 | + 'amount' => sprintf("%.2f", round(max($amount, 0.01), 2)), | |
| 272 | 330 | 'currency' => 'EUR', |
| 273 | - 'sourceCountry' => 'EE', | |
| 331 | + 'sourceCountry' => WC()->countries->get_base_country(), // 'EE' | |
| 274 | 332 | 'locale' => $locale, |
| 275 | 333 | 'shipmentMethods' => array() |
| 276 | 334 | ); |
| 277 | 335 | $package = $woocommerce->cart->get_shipping_packages(); |
| 278 | 336 | if (!empty($package)) { $package = $package[0]; } |
| 279 | - $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() ); | |
| 280 | 344 | $continents = WC()->countries->get_continents(); |
| 281 | 345 | $method_country_x = array(); |
| 282 | 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'); |
| 283 | 347 | foreach ($zones as $zone) { |
| 284 | 348 | foreach ($zone['shipping_methods'] as $method_key => $method) { |
| 349 | + if ($method->enabled !== 'yes') { continue; } | |
| 285 | 350 | if (!empty($supported_methods[$method->id])) { |
| 286 | 351 | foreach ($woocommerce->cart->get_shipping_packages() as $package) { |
| 287 | 352 | if (!$method->is_available($package)) { continue(2); } |
| 288 | 353 | } |
| @@ -289,31 +354,34 @@ | ||
| 289 | 354 | $method_type = $supported_methods[$method->id]; |
| 290 | 355 | if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); } |
| 291 | 356 | $carrier = array('countries' => array()); |
| 292 | 357 | if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); } |
| 293 | - foreach ($zone['zone_locations'] as $location) { | |
| 294 | - if ($location->type === 'continent' && !empty($continents[$location->code])) { | |
| 295 | - $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]); | |
| 296 | - $carrier['countries'] = array_merge($carrier['countries'], $countries); | |
| 297 | - } else if ($location->type === 'state') { | |
| 298 | - list($country, $state) = explode(':', $location->code); | |
| 299 | - $carrier['countries'][] = $country; | |
| 300 | - } else { | |
| 301 | - $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 | + } | |
| 302 | 371 | } |
| 303 | 372 | } |
| 304 | 373 | $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']); |
| 305 | - $price = $method->get_rates_for_package($package); | |
| 306 | - $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); | |
| 307 | 376 | $carrier['type'] = strtoupper($method_type); |
| 308 | 377 | $carrier['name'] = $method->title; |
| 309 | 378 | $carrier['methodId'] = $method->id . ':' . $method_key; |
| 310 | - $carrier['amount'] = round($price, 2); | |
| 379 | + $carrier['amount'] = sprintf("%.2f", round($price, 2)); | |
| 311 | 380 | $data['shipmentMethods'][] = $carrier; |
| 312 | 381 | } |
| 313 | 382 | } |
| 314 | 383 | } |
| 315 | - | |
| 316 | 384 | $MK = mk_get_api(); |
| 317 | 385 | $cart = $MK->createCart($data); |
| 318 | 386 | $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id)); |
| 319 | 387 | wp_redirect($cart->scoUrl); |
| @@ -331,9 +399,9 @@ | ||
| 331 | 399 | $this->form_fields['active'] = array( |
| 332 | 400 | 'title' => __('Enable/Disable', 'wc_makecommerce_domain'), |
| 333 | 401 | 'type' => 'checkbox', |
| 334 | 402 | 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'), |
| 335 | - 'default' => 'no' | |
| 403 | + 'default' => 'yes' | |
| 336 | 404 | ); |
| 337 | 405 | $this->form_fields['shop_tos_url'] = array( |
| 338 | 406 | 'title' => __('Shop ToS url', 'wc_makecommerce_domain'), |
| 339 | 407 | 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'), |
| @@ -346,9 +414,9 @@ | ||
| 346 | 414 | 'default' => 'no' |
| 347 | 415 | ); |
| 348 | 416 | } |
| 349 | 417 | public function is_available() { |
| 350 | - if ($this->settings['active'] == "yes") { | |
| 418 | + if ($this->settings['active'] == "yes" || empty($this->settings['active'])) { | |
| 351 | 419 | return true; |
| 352 | 420 | } |
| 353 | 421 | return false; |
| 354 | 422 | } |
| @@ -354,8 +422,26 @@ | ||
| 354 | 422 | } |
| 355 | 423 | |
| 356 | 424 | } |
| 357 | 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; | |
| 358 | 444 | } |
| 359 | 445 | |
| 360 | 446 | function woocommerce_payment_makecommerce_sc_add($methods) { |
| 361 | 447 | $methods[] = 'woocommerce_makecommerce_sc'; |