From 4ee5b792fcc40bb3d626cfea14e29027df4fe375 Mon Sep 17 00:00:00 2001 From: Mois Moshev Date: Sun, 11 Jan 2015 22:37:01 +0100 Subject: [PATCH] * Use selected shipping method instead of the first one available * Add an info object on which shipping module can set extra data about the order * Add a selected_module property to shipping class * Add an after_process method to shipping - shipping modules may need to do additional processing --- catalog/checkout_confirmation.php | 1 + catalog/checkout_process.php | 1 + catalog/checkout_shipping.php | 17 ++++++++++++++--- catalog/includes/classes/shipping.php | 20 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/catalog/checkout_confirmation.php b/catalog/checkout_confirmation.php index 8276e2462..41d759cd1 100644 --- a/catalog/checkout_confirmation.php +++ b/catalog/checkout_confirmation.php @@ -62,6 +62,7 @@ // load the selected shipping module require(DIR_WS_CLASSES . 'shipping.php'); + $shipping_modules = new shipping($shipping); require(DIR_WS_CLASSES . 'order_total.php'); diff --git a/catalog/checkout_process.php b/catalog/checkout_process.php index 1ff460be8..98766ad80 100644 --- a/catalog/checkout_process.php +++ b/catalog/checkout_process.php @@ -284,6 +284,7 @@ // load the after_process function from the payment modules $payment_modules->after_process(); + $shipping_modules->after_process(); $_SESSION['cart']->reset(true); diff --git a/catalog/checkout_shipping.php b/catalog/checkout_shipping.php index e28ee9422..b3e79ded2 100644 --- a/catalog/checkout_shipping.php +++ b/catalog/checkout_shipping.php @@ -123,10 +123,21 @@ if (isset($quote['error'])) { unset($_SESSION['shipping']); } else { - if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) { + $methods = $quote[0]['methods']; + $selectedMethod = null; + + // find selected method + foreach ($methods as $key => $methodObj) { + if ($methodObj['id'] == $method) { + $selectedMethod = $methodObj; + break; + } + } + if ( (isset($selectedMethod['title'])) && (isset($selectedMethod['cost'])) ) { $shipping = array('id' => $shipping, - 'title' => (($free_shipping == true) ? $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'), - 'cost' => $quote[0]['methods'][0]['cost']); + 'title' => (($free_shipping == true) ? $selectedMethod['title'] : $quote[0]['module'] . ' (' . $selectedMethod['title'] . ')'), + 'cost' => $selectedMethod['cost'], + 'info' => $selectedMethod['info']); tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL')); } diff --git a/catalog/includes/classes/shipping.php b/catalog/includes/classes/shipping.php index 939d5bbe3..c18deede4 100644 --- a/catalog/includes/classes/shipping.php +++ b/catalog/includes/classes/shipping.php @@ -24,6 +24,7 @@ function shipping($module = '') { if ( (tep_not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) { $include_modules[] = array('class' => substr($module['id'], 0, strpos($module['id'], '_')), 'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1))); + $this->selected_module = $module; } else { foreach ($this->modules as $value) { $class = substr($value, 0, strrpos($value, '.')); @@ -84,6 +85,25 @@ function quote($method = '', $module = '') { return $quotes_array; } + function after_process() + { + global $order; + + if(empty($this->selected_module)) { + // TODO: handle error here + return; + } + + // obtain selected module name and method id + $id_arr = explode('_', $this->selected_module['id']); + $module = $GLOBALS[$id_arr[0]]; + + // check is for backwards compatibility + if (!empty($module) && method_exists($module, 'after_process')) { + $module->after_process(); + } + } + function cheapest() { if (is_array($this->modules)) { $rates = array();