From 149aa382abd5921fb1dd4f6e28440f0c1fb586fd Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Tue, 17 May 2016 12:08:15 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Corre=C3=A7=C3=A3o=20para=20refer=C3=AAncia?= =?UTF-8?q?=20de=20items=20para=20c=C3=A1lculo=20do=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Correios/Model/Carrier/CorreiosMethod.php | 103 ++++++++++-------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php index d200f1d..e4b4685 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php @@ -89,7 +89,7 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request) } if ($this->_packageWeight == 0) { - $this->_packageWeight = $this->_getNominalWeight(); + $this->_packageWeight = $this->_getNominalWeight($request); } if ($this->getConfigData('weight_type') == PedroTeixeira_Correios_Model_Source_WeightType::WEIGHT_GR) { @@ -107,47 +107,64 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request) $this->_postMethodsExplode = explode(',', $this->getConfigData('postmethods')); // Generate Volume Weight - if ($this->_generateVolumeWeight() === false || $this->_removeInvalidServices() === false) { + if ($this->_generateVolumeWeight($request) === false || $this->_removeInvalidServices() === false) { $this->_throwError('dimensionerror', 'Dimension error', __LINE__); return $this->_result; } - $this->_filterMethodByItemRestriction(); + $this->_filterMethodByItemRestriction($request); if (empty($this->_postMethods)) { return false; } //Show Quotes $this->_getQuotes(); - + // Use descont codes $this->_updateFreeMethodQuote($request); return $this->_result; } - + + /** + * Retrieve all visible items from request + * + * @param Mage_Shipping_Model_Rate_Request $request + * @return array + */ + protected function _getRequestItems($request) + { + + $allItems = $request->getAllItems(); + $items = array(); + + + foreach( $allItems as $item ){ + if( !$item->getParentItemId() ){ + $items[] = $item; + } + } + + $items = $this->_loadBundleChildren($items); + + return $items; + } + /** * Gets Nominal Weight - * + * @param Mage_Shipping_Model_Rate_Request $request * @return number */ - protected function _getNominalWeight() + protected function _getNominalWeight($request) { $weight = 0; - $quote = Mage::getSingleton('checkout/cart')->getQuote(); - if (count($quote->getAllVisibleItems()) == 0) { - $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); - } - if ($quote->isNominal()) { - foreach ($quote->getAllVisibleItems() as $item) { - if ($item->getProduct()->getWeight()) { - $weight += $item->getProduct()->getWeight(); - } else { - $product = Mage::getModel('catalog/product')->load($item->getProductId()); - $weight += $product->getWeight(); - } - } + $items = $this->_getRequestItems($request); + + foreach ($items as $item) { + $product = Mage::getModel('catalog/product')->load($item->getProductId()); + $weight += $product->getWeight(); } + return $weight; } @@ -224,7 +241,7 @@ protected function _inicialCheck(Mage_Shipping_Model_Rate_Request $request) Mage::log('pedroteixeira_correios: From ZIP Code Error'); return false; } - + if (!trim($this->_toZip)) { return false; } @@ -429,19 +446,15 @@ protected function _throwError($message, $log = null, $line = 'NO LINE', $custom /** * Generate Volume weight * + * @param Mage_Shipping_Model_Rate_Request $request * @return bool */ - protected function _generateVolumeWeight() + protected function _generateVolumeWeight($request) { $pesoCubicoTotal = 0; - $items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems(); - - if (count($items) == 0) { - $items = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getAllVisibleItems(); - } + $items = $this->_getRequestItems($request); - $items = $this->_loadBundleChildren($items); foreach ($items as $item) { $_product = $item->getProduct(); @@ -720,7 +733,7 @@ protected function _removeInvalidServices() $tmpMethods = $this->_postMethodsExplode; $tmpMethods = $this->_filterMethodByConfigRestriction($tmpMethods); $isDivisible = (count($tmpMethods) == 0); - + if ($isDivisible) { return $this->_splitPack(); } @@ -810,18 +823,14 @@ protected function _addPostMethods($cServico) * ... * value 99: 81019 * + * @param Mage_Shipping_Model_Rate_Request $request * @return PedroTeixeira_Correios_Model_Carrier_CorreiosMethod */ - protected function _filterMethodByItemRestriction() + protected function _filterMethodByItemRestriction($request) { if ($this->getConfigFlag('filter_by_item')) { - $items = Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems(); - if (count($items) == 0) { - $items = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getAllVisibleItems(); - } - - $items = $this->_loadBundleChildren($items); + $items = $this->_getRequestItems($request); $intersection = $this->_postMethodsExplode; foreach ($items as $item) { $product = Mage::getModel('catalog/product')->load($item->getProductId()); @@ -890,9 +899,9 @@ protected function _splitPack() /** * Receive a list of methods, and validate one-by-one using the config settings. * Returns a list of valid methods or empty. - * + * * @param array $postmethods Services List - * + * * @return array */ protected function _filterMethodByConfigRestriction($postmethods) @@ -916,9 +925,9 @@ protected function _filterMethodByConfigRestriction($postmethods) /** * Loads the zip range list. * Returns TRUE only if zip target is included in the range. - * + * * @param array $method Current Post Method - * + * * @return boolean */ protected function _validateZipRestriction($method) @@ -939,9 +948,9 @@ protected function _validateZipRestriction($method) /** * Some special errors must be sent to users. * If not applicable, the default error will be sent. - * + * * @param array $errorList Error List - * + * * @return boolean */ protected function _appendShippingErrors($errorList) @@ -990,7 +999,7 @@ protected function _getSplitUpMsg() /** * Returns a short warning message. - * + * * @param string $error Error Id * * @return string @@ -1010,9 +1019,9 @@ protected function _getSoftErrorMsg($error) /** * Returns the price as float, and fixed by pack division. - * + * * @param string $price Price String - * + * * @return float */ protected function _getFormatPrice($price) @@ -1026,9 +1035,9 @@ protected function _getFormatPrice($price) /** * Filter visible and bundle children products. - * + * * @param array $items Product Items - * + * * @return array */ protected function _loadBundleChildren($items) From fc21ebaafa63ee8cee0f92fea346cb8ecee0c6d4 Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Tue, 17 May 2016 12:39:22 -0300 Subject: [PATCH 2/2] Formating fixes --- .../Correios/Model/Carrier/CorreiosMethod.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php index e4b4685..9d0cdce 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php @@ -129,7 +129,8 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request) /** * Retrieve all visible items from request * - * @param Mage_Shipping_Model_Rate_Request $request + * @param Mage_Shipping_Model_Rate_Request $request Mage request + * * @return array */ protected function _getRequestItems($request) @@ -138,9 +139,8 @@ protected function _getRequestItems($request) $allItems = $request->getAllItems(); $items = array(); - - foreach( $allItems as $item ){ - if( !$item->getParentItemId() ){ + foreach ( $allItems as $item ) { + if ( !$item->getParentItemId() ) { $items[] = $item; } } @@ -152,7 +152,9 @@ protected function _getRequestItems($request) /** * Gets Nominal Weight - * @param Mage_Shipping_Model_Rate_Request $request + * + * @param Mage_Shipping_Model_Rate_Request $request Mage request + * * @return number */ protected function _getNominalWeight($request) @@ -446,7 +448,8 @@ protected function _throwError($message, $log = null, $line = 'NO LINE', $custom /** * Generate Volume weight * - * @param Mage_Shipping_Model_Rate_Request $request + * @param Mage_Shipping_Model_Rate_Request $request Mage request + * * @return bool */ protected function _generateVolumeWeight($request) @@ -823,7 +826,8 @@ protected function _addPostMethods($cServico) * ... * value 99: 81019 * - * @param Mage_Shipping_Model_Rate_Request $request + * @param Mage_Shipping_Model_Rate_Request $request Mage request + * * @return PedroTeixeira_Correios_Model_Carrier_CorreiosMethod */ protected function _filterMethodByItemRestriction($request)