From 80e47a884f4973e5c72d31f5761781d00a8e970f Mon Sep 17 00:00:00 2001 From: Timo Janssrn Date: Fri, 8 Jun 2018 11:00:14 +0200 Subject: [PATCH] bugfix checkout script --- includes/checkout.php | 75 ++++++++++++++++++++++------------------- woocommerce-wuunder.php | 40 +++++++++++----------- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/includes/checkout.php b/includes/checkout.php index 65aae6f..ac56c6e 100644 --- a/includes/checkout.php +++ b/includes/checkout.php @@ -5,19 +5,23 @@ // add_action('woocommerce_review_order_before_submit', 'parcelshop_html'); add_action('woocommerce_review_order_after_submit', 'parcelshop_html'); -function callback_for_setting_up_scripts() { - $style_file = dirname(plugin_dir_url(__FILE__)) . "/assets/css/parcelshop.css"; - $shipping_method = new WC_wuunder_parcelshop(); - $google_api_key = $shipping_method->get_option('google_api_key'); - $script_file = "//maps.googleapis.com/maps/api/js?key=" . $google_api_key; - wp_register_style( 'wuunderCSS', $style_file); - wp_enqueue_style( 'wuunderCSS' ); - - wp_register_script( 'googleMapsJS', $script_file); - wp_enqueue_script( 'googleMapsJS' ); +function callback_for_setting_up_scripts() +{ + if (class_exists('WC_wuunder_parcelshop')) { + $style_file = dirname(plugin_dir_url(__FILE__)) . "/assets/css/parcelshop.css"; + $shipping_method = new WC_wuunder_parcelshop(); + $google_api_key = $shipping_method->get_option('google_api_key'); + $script_file = "//maps.googleapis.com/maps/api/js?key=" . $google_api_key; + wp_register_style('wuunderCSS', $style_file); + wp_enqueue_style('wuunderCSS'); + + wp_register_script('googleMapsJS', $script_file); + wp_enqueue_script('googleMapsJS'); + } } -function parcelshop_html(){ +function parcelshop_html() +{ $pluginPath = dirname(plugin_dir_url(__FILE__)); $pluginPathJS = $pluginPath . "/assets/js/parcelshop.js"; $pluginPathImg = $pluginPath . "/assets/images/parcelshop/bring-to-parcelshop.png"; @@ -66,38 +70,41 @@ function parcelshop_html(){ // Field added for the parcelshop_id, so that it can be requested from backend add_action('woocommerce_after_order_notes', 'add_parcelshop_id_field'); -function add_parcelshop_id_field($checkout) { - woocommerce_form_field('parcelshop_id', array( - 'type' => 'text', - 'class' => array( - 'my-field-class form-row-wide' - ) , - ) , $checkout->get_value('parcelshop_id')); - - woocommerce_form_field('parcelshop_country', array( - 'type' => 'text', - 'class' => array( - 'my-field-class form-row-wide' - ) , - ) , $checkout->get_value('parcelshop_country')); +function add_parcelshop_id_field($checkout) +{ + woocommerce_form_field('parcelshop_id', array( + 'type' => 'text', + 'class' => array( + 'my-field-class form-row-wide' + ), + ), $checkout->get_value('parcelshop_id')); + + woocommerce_form_field('parcelshop_country', array( + 'type' => 'text', + 'class' => array( + 'my-field-class form-row-wide' + ), + ), $checkout->get_value('parcelshop_country')); } // Save / Send the parcelshop id -add_action( 'woocommerce_checkout_update_order_meta', 'update_parcelshop_id' ); -function update_parcelshop_id( $order_id ) { - if (!empty($_POST['parcelshop_id'])){ - update_post_meta( $order_id, 'parcelshop_id', sanitize_text_field($_POST['parcelshop_id'])); +add_action('woocommerce_checkout_update_order_meta', 'update_parcelshop_id'); +function update_parcelshop_id($order_id) +{ + if (!empty($_POST['parcelshop_id'])) { + update_post_meta($order_id, 'parcelshop_id', sanitize_text_field($_POST['parcelshop_id'])); } } // Check to see if a parcelshop is selected when parcel method is selected && Check if shipping country == parcelshop country add_action('woocommerce_checkout_process', 'check_parcelshop_selection'); -function check_parcelshop_selection() { - if ($_POST['shipping_method'][0] === 'wuunder_parcelshop') { - if (!$_POST['parcelshop_id']) wc_add_notice(__('Kies eerst een parcelshop') , 'error'); +function check_parcelshop_selection() +{ + if ($_POST['shipping_method'][0] === 'wuunder_parcelshop') { + if (!$_POST['parcelshop_id']) wc_add_notice(__('Kies eerst een parcelshop'), 'error'); - if ($_POST['shipping_country'] != $_POST['parcelshop_country']) wc_add_notice(__('Het land van de verzendgegevens moet overeenkomen met het land van de parcelshop'), 'error'); - } + if ($_POST['shipping_country'] != $_POST['parcelshop_country']) wc_add_notice(__('Het land van de verzendgegevens moet overeenkomen met het land van de parcelshop'), 'error'); + } } ?> diff --git a/woocommerce-wuunder.php b/woocommerce-wuunder.php index 22912b9..899732b 100644 --- a/woocommerce-wuunder.php +++ b/woocommerce-wuunder.php @@ -63,9 +63,9 @@ public function __construct() add_action('admin_enqueue_scripts', array(&$this, 'add_admin_styles_scripts')); require_once(WW_PLUGIN_ADMIN_DIR . '/wcwuunder-admin.php'); - include_once('includes/checkout.php'); include_once('includes/parcelshop.php'); include_once('includes/wcwuunder-shipping-method.php'); + include_once('includes/checkout.php'); add_action('wp_ajax_parcelshoplocator', 'parcelShopLocator'); add_action('wp_ajax_nopriv_parcelshoplocator', 'parcelShopLocator'); @@ -109,27 +109,29 @@ public function webhook() $orderBookingToken = get_post_meta($orderId, '_wuunder_label_booking_token')[0]; if ($data['action'] === "shipment_booked") { - if ($bookingToken === $orderBookingToken) { - if (!empty($data['shipment']['id']) || !empty($data['shipment']['track_and_trace_url']) || !empty($data['shipment']['label_url'])) { - update_post_meta($orderId, '_wuunder_label_id', $data['shipment']['id']); - update_post_meta($orderId, '_wuunder_track_and_trace_url', $data['shipment']['track_and_trace_url']); - update_post_meta($orderId, '_wuunder_label_url', $data['shipment']['label_url']); - - $order = new WC_Order($orderId); - $order->update_status(get_option("wc_wuunder_post_booking_status")); - $errorRedirect = false; - } - } + if ($bookingToken === $orderBookingToken) { + if (!empty($data['shipment']['id']) || !empty($data['shipment']['track_and_trace_url']) || !empty($data['shipment']['label_url'])) { + update_post_meta($orderId, '_wuunder_label_id', $data['shipment']['id']); + update_post_meta($orderId, '_wuunder_track_and_trace_url', $data['shipment']['track_and_trace_url']); + update_post_meta($orderId, '_wuunder_label_url', $data['shipment']['label_url']); + + $order = new WC_Order($orderId); + $order->update_status(get_option("wc_wuunder_post_booking_status")); + $errorRedirect = false; + } + } } elseif ($data['action'] === "track_and_trace_updated") { - // This is the 2nd webhook - $order = wc_get_order($orderId); - $note = __("Het pakket is aangemeld bij: ". $data["carrier_name"] ."\n De track and trace code is: ".$data["track_and_trace_code"]); - $order->add_order_note($note); - $order->save(); - $errorRedirect = false; + // This is the 2nd webhook + $order = wc_get_order($orderId); + $note = __("Het pakket is aangemeld bij: " . $data["carrier_name"] . "\n De track and trace code is: " . $data["track_and_trace_code"]); + $order->add_order_note($note); + $order->save(); + $errorRedirect = false; } - if($errorRedirect){wp_redirect("", 500);} + if ($errorRedirect) { + wp_redirect("", 500); + } } }