Skip to content

Commit

Permalink
bugfix checkout script
Browse files Browse the repository at this point in the history
  • Loading branch information
timoj committed Jun 8, 2018
1 parent 1d99975 commit 80e47a8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
75 changes: 41 additions & 34 deletions includes/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 <strong>parcelshop</strong>') , 'error');
function check_parcelshop_selection()
{
if ($_POST['shipping_method'][0] === 'wuunder_parcelshop') {
if (!$_POST['parcelshop_id']) wc_add_notice(__('Kies eerst een <strong>parcelshop</strong>'), 'error');

if ($_POST['shipping_country'] != $_POST['parcelshop_country']) wc_add_notice(__('Het <strong>land van de verzendgegevens</strong> moet overeenkomen met het <strong>land van de parcelshop</strong>'), 'error');
}
if ($_POST['shipping_country'] != $_POST['parcelshop_country']) wc_add_notice(__('Het <strong>land van de verzendgegevens</strong> moet overeenkomen met het <strong>land van de parcelshop</strong>'), 'error');
}
}

?>
40 changes: 21 additions & 19 deletions woocommerce-wuunder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}

}
Expand Down

0 comments on commit 80e47a8

Please sign in to comment.