Skip to content

Commit

Permalink
Merge pull request #153 from mateus007/request_items
Browse files Browse the repository at this point in the history
Correção para referência de items para cálculo do request
  • Loading branch information
pedro-teixeira committed May 19, 2016
2 parents c156be0 + fc21eba commit 8805110
Showing 1 changed file with 59 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -107,47 +107,66 @@ 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 Mage 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 Mage 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;
}

Expand Down Expand Up @@ -224,7 +243,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;
}
Expand Down Expand Up @@ -429,19 +448,16 @@ protected function _throwError($message, $log = null, $line = 'NO LINE', $custom
/**
* Generate Volume weight
*
* @param Mage_Shipping_Model_Rate_Request $request Mage 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();

Expand Down Expand Up @@ -720,7 +736,7 @@ protected function _removeInvalidServices()
$tmpMethods = $this->_postMethodsExplode;
$tmpMethods = $this->_filterMethodByConfigRestriction($tmpMethods);
$isDivisible = (count($tmpMethods) == 0);

if ($isDivisible) {
return $this->_splitPack();
}
Expand Down Expand Up @@ -810,18 +826,15 @@ protected function _addPostMethods($cServico)
* ...
* value 99: 81019
*
* @param Mage_Shipping_Model_Rate_Request $request Mage 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());
Expand Down Expand Up @@ -890,9 +903,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)
Expand All @@ -916,9 +929,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)
Expand All @@ -939,9 +952,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)
Expand Down Expand Up @@ -990,7 +1003,7 @@ protected function _getSplitUpMsg()

/**
* Returns a short warning message.
*
*
* @param string $error Error Id
*
* @return string
Expand All @@ -1010,9 +1023,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)
Expand All @@ -1026,9 +1039,9 @@ protected function _getFormatPrice($price)

/**
* Filter visible and bundle children products.
*
*
* @param array $items Product Items
*
*
* @return array
*/
protected function _loadBundleChildren($items)
Expand Down

0 comments on commit 8805110

Please sign in to comment.