From 95a749de8bc38b45a92db2f118d8cf57296da1e2 Mon Sep 17 00:00:00 2001 From: mDeflotte Date: Tue, 22 Nov 2011 14:09:52 +0000 Subject: [PATCH] // Autoset id_carrier on the cart constructor + Replace a hookExec() --- classes/Cart.php | 28 +++++++++++++++++---- controllers/front/AuthController.php | 2 +- controllers/front/CartController.php | 4 +-- controllers/front/ParentOrderController.php | 12 ++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 6f07ccc82..d9cda702a 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -167,6 +167,18 @@ class CartCore extends ObjectModel } else $this->_taxCalculationMethod = Group::getDefaultPriceDisplayMethod(); + + // id_carrier is not setted since 1.5.0 + // Need to set it virtualy by using the delivery_option_list + if (!empty($this->delivery_option)) + { + $delivery_option = unserialize($this->delivery_option); + if (count($delivery_option) == 1) + { + $delivery_option_list = $this->getDeliveryOptionList(); + $this->id_carrier = $this->getIdCarrierFromDeliveryOption($delivery_option); + } + } } public function add($autodate = true, $nullValues = false) @@ -1509,15 +1521,21 @@ class CartCore extends ObjectModel if (count($delivery_option) == 1) { - $delivery_option_list = $this->getDeliveryOptionList(); - foreach ($delivery_option as $key => $value) - if (isset($delivery_option_list[$key]) && isset($delivery_option_list[$key][$value])) - if (count($delivery_option_list[$key][$value]['carrier_list']) == 1) - $this->id_carrier = $delivery_option_list[$key][$value]['carrier_list'][0]; + $this->id_carrier = $this->getIdCarrierFromDeliveryOption($delivery_option); $this->delivery_option = serialize($delivery_option); } } + private function getIdCarrierFromDeliveryOption($delivery_option) + { + $delivery_option_list = $this->getDeliveryOptionList(); + foreach ($delivery_option as $key => $value) + if (isset($delivery_option_list[$key]) && isset($delivery_option_list[$key][$value])) + if (count($delivery_option_list[$key][$value]['carrier_list']) == 1) + return $delivery_option_list[$key][$value]['carrier_list'][0]; + return 0; + } + /** * Get the delivery option seleted, or if no delivery option was selected, the cheapest option for each address * @return array delivery option diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 7c03f2b63..27a15f2e9 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -274,7 +274,7 @@ class AuthControllerCore extends FrontController if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0)) $this->context->cookie->id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id); // Update cart address - $this->context->cart->delivery_option = ''; + $this->context->cart->setDeliveryOption(null); $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id)); $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id)); $this->context->cart->update(); diff --git a/controllers/front/CartController.php b/controllers/front/CartController.php index 4273e309a..638882a21 100644 --- a/controllers/front/CartController.php +++ b/controllers/front/CartController.php @@ -104,7 +104,7 @@ class CartControllerCore extends FrontController { if (!Cart::getNbProducts((int)($this->context->cart->id))) { - $this->context->cart->delivery_option = ''; + $this->context->cart->setDeliveryOption(null); $this->context->cart->gift = 0; $this->context->cart->gift_message = ''; $this->context->cart->update(); @@ -249,7 +249,7 @@ class CartControllerCore extends FrontController $id_country = (isset($deliveryAddress) && $deliveryAddress->id) ? $deliveryAddress->id_country : Configuration::get('PS_COUNTRY_DEFAULT'); $result['carriers'] = Carrier::getCarriersForOrder(Country::getIdZone($id_country), $groups); //$result['checked'] = Carrier::getDefaultCarrierSelection($result['carriers'], (int)$this->cart->id_carrier); - $result['HOOK_EXTRACARRIER'] = Module::hookExec('extraCarrier', array('address' => (isset($deliveryAddress) && (int)$deliveryAddress->id) ? $deliveryAddress : null)); + $result['HOOK_EXTRACARRIER'] = Hook::exec('extraCarrier', array('address' => (isset($deliveryAddress) && (int)$deliveryAddress->id) ? $deliveryAddress : null)); } $result['summary'] = $this->context->cart->getSummaryDetails(); $result['customizedDatas'] = Product::getAllCustomizedDatas($this->context->cart->id, null, true); diff --git a/controllers/front/ParentOrderController.php b/controllers/front/ParentOrderController.php index b0054171e..595c2ff72 100644 --- a/controllers/front/ParentOrderController.php +++ b/controllers/front/ParentOrderController.php @@ -218,7 +218,7 @@ class ParentOrderControllerCore extends FrontController $id_zone = Country::getIdZone((int)Configuration::get('PS_COUNTRY_DEFAULT')); if (Tools::getIsset('delivery_option') && $this->validateDeliveryOption(Tools::getValue('delivery_option'))) - $this->context->cart->delivery_option = serialize(Tools::getValue('delivery_option')); + $this->context->cart->setDeliveryOption(Tools::getValue('delivery_option')); Hook::exec('processCarrier', array('cart' => $this->context->cart)); @@ -435,7 +435,7 @@ class ParentOrderControllerCore extends FrontController */ protected function setNoCarrier() { - $this->context->cart->delivery_option = 0; + $this->context->cart->setDeliveryOption(null); $this->context->cart->update(); } @@ -445,6 +445,9 @@ class ParentOrderControllerCore extends FrontController * @todo this function must be modified - id_carrier is now delivery_option * * @param array $carriers + * + * @deprecated since 1.5.0 + * * @return number the id of the default carrier */ protected function setDefaultCarrierSelection($carriers) @@ -467,7 +470,7 @@ class ParentOrderControllerCore extends FrontController $this->context->cart->id_carrier = (int)$carriers[0]['id_carrier']; } else - $this->context->cart->id_carrier = 0; + $this->context->cart->setDeliveryOption(null); if ($this->context->cart->update()) return $this->context->cart->id_carrier; return 0; @@ -477,6 +480,9 @@ class ParentOrderControllerCore extends FrontController * Decides what the default carrier is and update the cart with it * * @param array $carriers + * + * @deprecated since 1.5.0 + * * @return number the id of the default carrier */ protected function _setDefaultCarrierSelection($carriers)