/** * Collect and combine rates from vendor carriers * * @param Mage_Shipping_Model_Rate_Request $request * @return Mage_Shipping_Model_Rate_Result|boolean */ public function collectRates(Mage_Shipping_Model_Rate_Request $request) { if (!$this->getConfigFlag('active')) { return false; } $this->_rawRequest = $request; $hlp = Mage::helper('udropship'); $hlpd = Mage::helper('udropship/protected'); $carrierNames = Mage::getSingleton('udropship/source')->getCarriers(); // get available dropship shipping methods $shipping = $hlp->getShippingMethods(); foreach ($shipping as $s) { $s->setIsSkipped(false); } // prepare data $items = $request->getAllItems(); try { $hlpd->prepareQuoteItems($items); } catch (Exception $e) { return; } if (!$hlpd->getQuote()) { return; } $quote = $hlpd->getQuote(); $quoteWebsiteId = false; if ($quote->getStore() instanceof Varien_Object) { $quoteWebsiteId = $quote->getStore()->getWebsiteId(); } $address = $hlpd->getQuote()->getShippingAddress(); foreach ($items as $item) { if ($item->getAddress()) { $address = $item->getAddress(); } break; } Mage::dispatchEvent('udropship_carrier_collect_before', array('request' => $request, 'address' => $address)); $requests = $hlpd->getRequestsByVendor($items, $request); $evTransport = new Varien_Object(array('requests' => $requests, 'orig_request' => $request)); Mage::dispatchEvent('udropship_carrier_process_vendor_requests', array('transport' => $evTransport, 'address' => $address)); $requests = $evTransport->getRequests(); if ($quote->getUdropshipCarrierError()) { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $errorMsg = $this->getConfigData('specificerrmsg'); $error->setErrorMessage($quote->getUdropshipCarrierError() ? $quote->getUdropshipCarrierError() : $errorMsg); return $error; } $requestVendors = array_keys($requests); // build separate requests grouped by carrier and vendor $carriers = array(); $numMethodsPerVendor = array(); $firstVendorRequests = array(); foreach ($requests as $vId => $vRequests) { foreach ($vRequests as $cCode => $r) { if (empty($firstVendorRequests[$vId])) { $firstVendorRequests[$vId] = $r; } #$cCode = $r->getCarrierCode(); $carriers[$cCode][$vId]['request'] = $r; $methods = $hlp->getVendor($vId)->getShippingMethods(); foreach ($methods as $__m) { foreach ($__m as $m) { if ($s = $shipping->getItemById($m['shipping_id'])) { $s->useProfile($hlp->getVendor($vId)); $carriers[$cCode][$vId]['methods'][$s->getShippingCode()] = $s->getSystemMethods($cCode); $s->resetProfile(); } } } } // skip methods that are not shared by ALL vendors $vendorMethods = $hlp->getVendor($vId)->getShippingMethods(); foreach ($shipping as $s) { if (empty($vendorMethods[$s->getId()])) { $s->setIsSkipped(true); } else { $_isSkippedShipping = new Varien_Object(array('result' => false)); Mage::dispatchEvent('udropship_vendor_shipping_check_skipped', array('shipping' => $s, 'address' => $address, 'vendor' => $hlp->getVendor($vId), 'request' => $firstVendorRequests[$vId], 'result' => $_isSkippedShipping)); $s->setIsSkipped($_isSkippedShipping->getResult()); } $sWebsites = $s->getWebsiteIds(); if (!is_array($sWebsites)) { $sWebsites = array($sWebsites); } $sWebsites = array_filter($sWebsites); if (!empty($sWebsites) && false !== $quoteWebsiteId && !in_array($quoteWebsiteId, $sWebsites)) { $s->setIsSkipped(true); } } } // quote.udropship_shipping_details $details = array('version' => $hlp->getVersion()); // vendors participating in the estimate $vendors = array(); $errorAction = $hlpd->getStore()->getConfig('udropship/customer/estimate_error_action'); // send actual requests and collect results foreach ($carriers as $cCode => $requests) { $keys = array_keys($requests); foreach ($keys as $k) { $vendor = $hlp->getVendor($k); $systemMethods = $hlp->getMultiSystemShippingMethodsByProfile($vendor); $vMethods = $vendor->getShippingMethods(); $result = $hlpd->collectVendorCarrierRates($requests[$k]['request']); if ($result === false) { if ($errorAction == 'fail') { //return $hlpd->errorResult(); continue; } elseif ($errorAction == 'skip') { continue; } } $rates = $result->getAllRates(); $keys1 = array_keys($rates); foreach ($keys1 as $k1) { if ($rates[$k1] instanceof Mage_Shipping_Model_Rate_Result_Error) { if ($errorAction == 'fail') { //return $hlpd->errorResult('udropship', $rates[$k1]->getErrorMessage()); continue 2; } elseif ($errorAction == 'skip') { continue 2; } } $wildcardUsed = false; if (empty($systemMethods[$rates[$k1]->getCarrier()][$rates[$k1]->getMethod()])) { if (!empty($systemMethods[$rates[$k1]->getCarrier()]['*'])) { $wildcardUsed = true; } else { continue; } } if ($wildcardUsed) { $smArray = $systemMethods[$rates[$k1]->getCarrier()]['*']; } else { $smArray = $systemMethods[$rates[$k1]->getCarrier()][$rates[$k1]->getMethod()]; } foreach ($smArray as $s) { $s->useProfile($vendor); if ($s->getIsSkipped() || !isset($vMethods[$s->getId()])) { continue; } foreach ($vMethods[$s->getId()] as $vMethod) { $vendorCode = $vendor->getCarrierCode(); if ($requests[$k]['request']->getForcedCarrierFlag()) { $ecCode = $ocCode = $rates[$k1]->getCarrier(); } else { $ecCode = !empty($vMethod['est_carrier_code']) ? $vMethod['est_carrier_code'] : (!empty($vMethod['carrier_code']) ? $vMethod['carrier_code'] : $vendorCode); $ocCode = !empty($vMethod['carrier_code']) ? $vMethod['carrier_code'] : $vendorCode; } $oldEstCode = null; if (!empty($details['methods'][$s->getShippingCode()]['vendors'][$k]['est_code'])) { list($oldEstCode, ) = explode('_', $details['methods'][$s->getShippingCode()]['vendors'][$k]['est_code'], 2); } if ($ecCode != $rates[$k1]->getCarrier()) { if ($vendor->getUseRatesFallback() && !Mage::helper('udropship')->isUdsprofileActive()) { if ($oldEstCode == $ecCode) { continue; } elseif ($oldEstCode != $ecCode && $ecCode == $rates[$k1]->getCarrier()) { } elseif ($oldEstCode != $ocCode && $ocCode == $rates[$k1]->getCarrier()) { $ecCode = $ocCode; } elseif (!$oldEstCode && $vendorCode == $rates[$k1]->getCarrier()) { $ecCode = $vendorCode; } else { continue; } } else { continue; } } if ('**estimate**' == $ocCode) { $ocCode = $ecCode; } if ($wildcardUsed && $ecCode != $ocCode) { continue; } if (Mage::helper('udropship')->isUdsprofileActive()) { $codeToCompare = $vMethod['carrier_code'] . '_' . $vMethod['method_code']; if (!empty($vMethod['est_use_custom'])) { $codeToCompare = $vMethod['est_carrier_code'] . '_' . $vMethod['est_method_code']; } if ($codeToCompare != $rates[$k1]->getCarrier() . '_' . $rates[$k1]->getMethod()) { continue; } } $rates[$k1]->setUdsIsSkip(false); Mage::dispatchEvent('udropship_process_vendor_carrier_single_rate_result', array('vendor_method' => $vMethod, 'udmethod' => $s, 'address' => $address, 'vendor' => $vendor, 'request' => $requests[$k]['request'], 'rate' => $rates[$k1])); if ($rates[$k1]->getUdsIsSkip()) { continue; } $shipPrice = $this->getUdRatePrice($rates[$k1], $requests[$k]['request'], $s); $detail = array('cost' => sprintf('%.4f', $rates[$k1]->getCost()), 'price' => sprintf('%.4f', $shipPrice), 'price_excl' => sprintf('%.4f', $this->getShippingPrice($shipPrice, $vendor, $address, 'base')), 'price_incl' => sprintf('%.4f', $this->getShippingPrice($shipPrice, $vendor, $address, 'incl')), 'tax' => sprintf('%.4f', $this->getShippingPrice($shipPrice, $vendor, $address, 'tax')), 'est_code' => $rates[$k1]->getCarrier() . '_' . $rates[$k1]->getMethod(), 'est_carrier_title' => $rates[$k1]->getCarrierTitle(), 'est_method_title' => $rates[$k1]->getMethodTitle(), 'package_weight' => $requests[$k]['request']->getPackageWeight(), 'package_value' => $requests[$k]['request']->getPackageValue(), 'days_in_transit' => $s->getDaysInTransit()); if (Mage::helper('udropship')->isUdsprofileActive()) { $detail['sort_order'] = $vMethod['sort_order']; } $detail['is_free_shipping'] = (int) $this->isFwFreeShipping($rates[$k1], $requests[$k]['request'], $s); if ($ecCode == $ocCode) { $detail['code'] = $detail['est_code']; $detail['carrier_title'] = $detail['est_carrier_title']; $detail['method_title'] = $detail['est_method_title']; } else { $ocMethod = $s->getSystemMethods($ocCode); if (Mage::helper('udropship')->isUdsprofileActive()) { $ocMethod = $vMethod['method_code']; } if (empty($ocMethod)) { continue; } $methodNames = $hlp->getCarrierMethods($ocCode); $detail['code'] = $ocCode . '_' . $ocMethod; $detail['carrier_title'] = $carrierNames[$ocCode]; $detail['method_title'] = $methodNames[$ocMethod]; } $vendors[$k] = 1; $scKey = !$wildcardUsed ? $s->getShippingCode() : $s->getShippingCode() . '___' . $detail['code']; $details['methods'][$scKey]['id'] = $s->getShippingId(); if (Mage::helper('udropship')->isUdsprofileActive() && !empty($details['methods'][$scKey]['vendors'][$k]) && $details['methods'][$scKey]['vendors'][$k]['sort_order'] < $detail['sort_order']) { continue; } if (($curUdpoSeqNumber = $requests[$k]['request']->getUdpoSeqNumber()) && !empty($details['methods'][$scKey]['vendors'][$k])) { $snByVendor = $address->getSeqNumbersByVendor(); $snByVendor[$k][$curUdpoSeqNumber][$scKey] = true; $address->setSeqNumbersByVendor($snByVendor); $ratesBySeqNumber = @$details['methods'][$scKey]['vendors'][$k]['rates_by_seq_number']; if (!is_array($ratesBySeqNumber)) { $ratesBySeqNumber = array(); } if ($oldRateBySeqNumber = @$ratesBySeqNumber[$curUdpoSeqNumber]) { $details['methods'][$scKey]['vendors'][$k]['cost'] -= $oldRateBySeqNumber['cost']; $details['methods'][$scKey]['vendors'][$k]['price'] -= $oldRateBySeqNumber['price']; $details['methods'][$scKey]['vendors'][$k]['price_excl'] -= $oldRateBySeqNumber['price_excl']; $details['methods'][$scKey]['vendors'][$k]['price_incl'] -= $oldRateBySeqNumber['price_incl']; $details['methods'][$scKey]['vendors'][$k]['tax'] -= $oldRateBySeqNumber['tax']; } $ratesBySeqNumber[$curUdpoSeqNumber] = $detail; //if (is_array($snByVendor) && $curUdpoSeqNumber>=max(array_keys($snByVendor))) { $_resCost = $detail['cost'] + $details['methods'][$scKey]['vendors'][$k]['cost']; $_resPrice = $detail['price'] + $details['methods'][$scKey]['vendors'][$k]['price']; $_resPriceExcl = $detail['price_excl'] + $details['methods'][$scKey]['vendors'][$k]['price_excl']; $_resPriceIncl = $detail['price_incl'] + $details['methods'][$scKey]['vendors'][$k]['price_incl']; $_resTax = $detail['tax'] + $details['methods'][$scKey]['vendors'][$k]['tax']; $details['methods'][$scKey]['vendors'][$k] = $detail; $details['methods'][$scKey]['vendors'][$k]['cost'] = $_resCost; $details['methods'][$scKey]['vendors'][$k]['price'] = $_resPrice; $details['methods'][$scKey]['vendors'][$k]['price_excl'] = $_resPriceExcl; $details['methods'][$scKey]['vendors'][$k]['price_incl'] = $_resPriceIncl; $details['methods'][$scKey]['vendors'][$k]['tax'] = $_resTax; //} $details['methods'][$scKey]['vendors'][$k]['is_free_shipping'] = $detail['is_free_shipping']; $details['methods'][$scKey]['vendors'][$k]['rates_by_seq_number'] = $ratesBySeqNumber; } else { $details['methods'][$scKey]['vendors'][$k] = $detail; if (!empty($curUdpoSeqNumber)) { $snByVendor = $address->getSeqNumbersByVendor(); $snByVendor[$k][$curUdpoSeqNumber][$scKey] = true; $address->setSeqNumbersByVendor($snByVendor); $details['methods'][$scKey]['vendors'][$k]['rates_by_seq_number'] = array($curUdpoSeqNumber => $detail); } } if ($wildcardUsed) { $details['methods'][$scKey]['wildcard_code'] = $detail['code']; $details['methods'][$scKey]['wildcard_carrier_title'] = $detail['carrier_title']; $details['methods'][$scKey]['wildcard_method_title'] = $detail['method_title']; } } } foreach ($smArray as $s) { $s->resetProfile(); } } } } $snByVendor = $address->getSeqNumbersByVendor(); if (!empty($snByVendor) && is_array($snByVendor)) { $totalSnUsed = array(); $methodsUsedBySn = array(); foreach ($snByVendor as $vId => $snData) { foreach ($snData as $__sn => $snMethods) { $totalSnUsed[$vId . '-' . $__sn] = 1; foreach ($snMethods as $_snMethod => $_dummy) { $methodsUsedBySn[$_snMethod][$vId . '-' . $__sn] = 1; } } } $totalSnUsed = array_sum($totalSnUsed); foreach ($methodsUsedBySn as $_snMethod => $_snMethodTotals) { if (array_sum($_snMethodTotals) < $totalSnUsed) { unset($details['methods'][$_snMethod]); } } } $address->setUdropshipShippingDetails(Zend_Json::encode($details)); #Mage::log($hlpd->getQuote()->getUdropshipShippingDetails()); #exit; // googlecheckout merchant calculations don't save address if (Mage::app()->getRequest()->getRouteName() == 'googlecheckout') { $transaction = Mage::getModel('core/resource_transaction'); $transaction->addObject($address); foreach ($request->getAllItems() as $item) { $transaction->addObject($item); } $transaction->save(); } if (empty($vendors) || $errorAction == 'fail' && count($vendors) < count($requestVendors)) { return $hlpd->errorResult('udropship'); } $totalMethod = Mage::getStoreConfig('udropship/customer/estimate_total_method'); // collect prices from details $totals = array(); $numVendors = sizeof($vendors); $processedMethods = array(); foreach ($details['methods'] as $mCode => $method) { $sId = $method['id']; unset($method['id']); if (in_array($method, $processedMethods)) { continue; } $processedMethods[] = $method; $method['id'] = $sId; $s = $shipping->getItemById($method['id']); // skip not common methods if ($s->getIsSkipped() || sizeof($method['vendors']) < $numVendors) { continue; } if (empty($totals[$mCode])) { $totals[$mCode] = array('is_free_shipping' => 1, 'cost' => 0, 'price' => 0, 'price_excl' => 0, 'price_incl' => 0, 'tax' => 0, 'title' => $s->getStoreTitle($quote->getStoreId())); if (!empty($method['wildcard_code'])) { $totals[$mCode]['title'] .= sprintf(' [%s - %s]', $method['wildcard_carrier_title'], $method['wildcard_method_title']); } } foreach ($method['vendors'] as $vId => $rate) { $totals[$mCode]['is_free_shipping'] = @$totals[$mCode]['is_free_shipping'] && @$rate['is_free_shipping']; $totals[$mCode]['cost'] = $hlp->applyEstimateTotalCostMethod($totals[$mCode]['cost'], $rate['cost']); $totals[$mCode]['price'] = $hlp->applyEstimateTotalPriceMethod($totals[$mCode]['price'], $rate['price']); $totals[$mCode]['price_excl'] = $hlp->applyEstimateTotalPriceMethod($totals[$mCode]['price_excl'], $rate['price_excl']); $totals[$mCode]['price_incl'] = $hlp->applyEstimateTotalPriceMethod($totals[$mCode]['price_incl'], $rate['price_incl']); $totals[$mCode]['tax'] = $hlp->applyEstimateTotalPriceMethod($totals[$mCode]['tax'], $rate['tax']); } } #Mage::log($totals); // return Magento formated shipping carrier result $result = Mage::getModel('shipping/rate_result'); // flat rate customization /* $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('udropship'); $method->setCarrierTitle($dropshipCarrier->getConfigData('title')); $method->setMethod('flatrate'); $method->setMethodTitle('Flat Rate'); $method->setCost(7.5); $method->setPrice(7.5); $result->append($method); */ if (!empty($totals)) { foreach ($totals as $mCode => $total) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('udropship'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod($mCode); $method->setMethodTitle($total['title']); $method->setCost($total['cost']); if (!empty($total['is_free_shipping'])) { $method->setPrice(0); } else { $method->setPrice($this->getMethodPrice($total['price'], $mCode)); } $result->append($method); } } else { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $errorMsg = $this->getConfigData('specificerrmsg'); $error->setErrorMessage($errorMsg); return $error; } Mage::dispatchEvent('udropship_carrier_collect_after', array('request' => $request, 'result' => $result, 'address' => $address, 'details' => $details)); return $result; }
public function collectRates(Mage_Shipping_Model_Rate_Request $request) { #return Mage::helper('udsplit/protected')->collectRates($this, $request); if (!$this->getConfigFlag('active')) { return false; } $hl = Mage::helper('udropship'); $hlp = Mage::helper('udropship/protected'); $carrierNames = Mage::getSingleton('udropship/source')->getCarriers(); // prepare data $items = $request->getAllItems(); if (empty($items)) { return; } try { $hlp->prepareQuoteItems($items); } catch (Exception $e) { Mage::helper('udropship')->addMessageOnce($e->getMessage()); return; } foreach ($items as $item) { $quote = $item->getQuote(); break; } $address = $quote->getShippingAddress(); foreach ($items as $item) { if ($item->getAddress()) { $address = $item->getAddress(); } break; } Mage::dispatchEvent('udsplit_carrier_collect_before', array('request' => $request, 'address' => $address)); $requests = $hlp->getRequestsByVendor($items, $request); $evTransport = new Varien_Object(array('requests' => $requests)); Mage::dispatchEvent('udsplit_carrier_process_vendor_requests', array('transport' => $evTransport, 'address' => $address)); $requests = $evTransport->getRequests(); #foreach ($requests as $r) var_dump($r); exit; // get available dropship shipping methods $shipping = $hl->getShippingMethods(); $freeMethods = explode(',', Mage::getStoreConfig('carriers/udropship/free_method', $hlp->getStore())); if ($freeMethods) { $_freeMethods = array(); foreach ($freeMethods as $freeMethod) { if (is_numeric($freeMethod)) { if ($shipping->getItemById($freeMethod)) { $_freeMethods[] = $freeMethod; } } else { if ($shipping->getItemByColumnValue('shipping_code', $freeMethod)) { $_freeMethods[] = $freeMethod; } } $_freeMethods[] = $freeMethod; } $freeMethods = $_freeMethods; } $result = Mage::getModel('udropship/rateResult'); foreach ($requests as $vId => $vRequests) { $vendor = $hl->getVendor($vId); $systemMethods = $hl->getMultiSystemShippingMethodsByProfile($vendor); $vMethods = $vendor->getShippingMethods(); foreach ($vRequests as $cCode => $req) { $vResult = $hlp->collectVendorCarrierRates($req); $vRates = $vResult->getAllRates(); foreach ($vRates as $rate) { $wildcardUsed = false; $hasVendorMethod = false; $smArray = @$systemMethods[$rate->getCarrier()][$rate->getMethod()]; if (!empty($smArray)) { foreach ($smArray as $udMethod) { $hasVendorMethod = $hasVendorMethod || !empty($vMethods[$udMethod->getShippingId()]); } } if (!$hasVendorMethod) { if (!empty($systemMethods[$rate->getCarrier()]['*'])) { $wildcardUsed = true; } else { continue; } } if ($wildcardUsed) { $smArray = $systemMethods[$rate->getCarrier()]['*']; } foreach ($smArray as $udMethod) { $udMethod->useProfile($vendor); if (empty($vMethods[$udMethod->getShippingId()])) { continue; } foreach ($vMethods[$udMethod->getId()] as $vMethod) { $_isSkippedShipping = new Varien_Object(array('result' => false)); Mage::dispatchEvent('udropship_vendor_shipping_check_skipped', array('shipping' => $udMethod, 'address' => $address, 'vendor' => $vendor, 'request' => $req, 'result' => $_isSkippedShipping)); if ($_isSkippedShipping->getResult()) { continue; } if ($freeMethods && Mage::getStoreConfigFlag('carriers/udropship/free_shipping_allowed', $request->getStoreId()) && Mage::getStoreConfigFlag('carriers/udropship/freeweight_allowed', $request->getStoreId()) && $this->isRuleFreeshipping($req) && in_array($udMethod->getShippingCode(), $freeMethods)) { $rate->setPrice(0); $rate->setIsFwFreeShipping(true); } $rate->setPrice($this->getMyMethodPrice($rate->getPrice(), $req, $udMethod->getShippingCode())); $rate->setUdsIsSkip(false); Mage::dispatchEvent('udropship_process_vendor_carrier_single_rate_result', array('vendor_method' => $vMethod, 'udmethod' => $udMethod, 'address' => $address, 'vendor' => $vendor, 'request' => $req, 'rate' => $rate)); if ($rate->getUdsIsSkip()) { continue; } $vendorCode = $vendor->getCarrierCode(); if ($req->getForcedCarrierFlag()) { $ecCode = $ocCode = $rate->getCarrier(); } else { $ecCode = !empty($vMethod['est_carrier_code']) ? $vMethod['est_carrier_code'] : (!empty($vMethod['carrier_code']) ? $vMethod['carrier_code'] : $vendorCode); $ocCode = !empty($vMethod['carrier_code']) ? $vMethod['carrier_code'] : $vendorCode; } $oldEstCode = null; $resultKey = sprintf('%s-%s', $vId, $udMethod->getShippingCode()); if (!empty($resultRates[$resultKey])) { $oldEstCode = $resultRates[$resultKey]->getUdEstCarrier(); if (Mage::helper('udropship')->isUdsprofileActive() && $resultRates[$resultKey]->getUdsprofileSortOrder() < $vMethod['sort_order']) { continue; } } if ($ecCode != $rate->getCarrier()) { if (!$wildcardUsed && $vendor->getUseRatesFallback() && !Mage::helper('udropship')->isUdsprofileActive()) { if ($oldEstCode == $ecCode) { continue; } elseif ($oldEstCode != $ocCode && $ocCode == $rate->getCarrier()) { $ecCode = $ocCode; } elseif (!$oldEstCode && $vendorCode == $rate->getCarrier()) { $ecCode = $vendorCode; } else { continue; } } else { continue; } } if ('**estimate**' == $ocCode) { $ocCode = $ecCode; } if ($wildcardUsed && $ecCode != $ocCode) { continue; } if ($ecCode != $rate->getCarrier()) { continue; } if (Mage::helper('udropship')->isUdsprofileActive()) { $codeToCompare = $vMethod['carrier_code'] . '_' . $vMethod['method_code']; if (!empty($vMethod['est_use_custom'])) { $codeToCompare = $vMethod['est_carrier_code'] . '_' . $vMethod['est_method_code']; } if ($codeToCompare != $rate->getCarrier() . '_' . $rate->getMethod()) { continue; } } if ($ocCode != $ecCode) { $ocMethod = $udMethod->getSystemMethods($ocCode); if (Mage::helper('udropship')->isUdsprofileActive()) { $ocMethod = $vMethod['method_code']; } if (empty($ocMethod)) { continue; } $methodNames = $hl->getCarrierMethods($ocCode); $rate->setCarrier($ocCode)->setMethod($ocMethod)->setCarrierTitle($carrierNames[$ocCode])->setMethodTitle($methodNames[$ocMethod]); } $rate->setPriority(@$vMethod['priority'])->setUdEstCarrier($ecCode)->setUdVendorMethod($vMethod)->setUdVid($vId)->setUdropshipShippingId($udMethod->getShippingId()); if (Mage::helper('udropship')->isUdsprofileActive()) { $rate->setUdsprofileSortOrder($vMethod['sort_order']); } if ($wildcardUsed) { $resultKey .= $rate->getCarrier() . '_' . $rate->getMethod(); } $resultRates[$resultKey] = $rate; break; } } foreach ($smArray as $udMethod) { $udMethod->resetProfile(); } } } } if (!empty($resultRates)) { foreach ($resultRates as $resultRate) { $result->append($resultRate); if ($exRate = $hl->getExtraChargeRate($req, $resultRate, $resultRate->getUdVid(), $resultRate->getUdVendorMethod())) { $result->append($exRate); } } } foreach ($items as $item) { $quote = $item->getQuote(); break; } if (empty($quote)) { $result->append($hlp->errorResult('udsplit')); return $result; } $address = $quote->getShippingAddress(); foreach ($items as $item) { if ($item->getAddress()) { $address = $item->getAddress(); } break; } $cost = 0; $price = 0; $details = $address->getUdropshipShippingDetails(); $methodCodes = array(); if ($details && ($details = Zend_Json::decode($details)) && !empty($details['methods'])) { foreach ($details['methods'] as $vId => $rate) { if (!empty($rate['code'])) { $methodCodes[$vId] = $rate['code']; } } } $totalMethod = Mage::getStoreConfig('udropship/customer/estimate_total_method'); $details = array('version' => Mage::helper('udropship')->getVersion()); $result->sortRatesByPriority(); $rates = $result->getAllRates(); $hasDefPatternMatch = array(); foreach ($rates as $rate) { if ($rate->getErrorMessage()) { continue; } $vId = $rate->getUdropshipVendor(); $vendor = $hl->getVendor($vId); if (!$vId) { continue; } $pattern = $vendor->getDefaultShippingMethodPattern(); if (!isset($hasDefPatternMatch[$vId])) { $hasDefPatternMatch[$vId] = false; } $hasDefPatternMatch[$vId] = $hasDefPatternMatch[$vId] || ($pattern ? preg_match('#' . preg_quote($pattern) . '#i', $rate->getMethodTitle()) : true); } $matchRank = array(); $costsByVid = $pricesByVid = array(); foreach ($rates as $rate) { if ($rate->getErrorMessage()) { continue; } $vId = $rate->getUdropshipVendor(); $vendor = $hl->getVendor($vId); if (!$vId) { continue; } $code = $rate->getCarrier() . '_' . $rate->getMethod(); $data = array('code' => $code, 'cost' => (double) $rate->getCost(), 'price' => (double) $rate->getPrice(), 'price_excl' => (double) $this->getShippingPrice($rate->getPrice(), $vendor, $address, 'base'), 'price_incl' => (double) $this->getShippingPrice($rate->getPrice(), $vendor, $address, 'incl'), 'tax' => (double) $this->getShippingPrice($rate->getPrice(), $vendor, $address, 'tax'), 'carrier_title' => $rate->getCarrierTitle(), 'method_title' => $rate->getMethodTitle(), 'is_free_shipping' => (int) $rate->getIsFwFreeShipping()); if (!isset($matchRank[$vId])) { $matchRank[$vId] = 0; } $pattern = $vendor->getDefaultShippingMethodPattern(); $defPatternMatch = $pattern ? preg_match('#' . preg_quote($pattern) . '#i', $rate->getMethodTitle()) : true; $isCurMatch = false; $curMatchRank = 0; if (!empty($methodCodes[$vId]) && $code == $methodCodes[$vId]) { $curMatchRank = 1000; $isCurMatch = true; } elseif ($vendor->getDefaultShippingId() && $vendor->getDefaultShippingId() == $rate->getUdropshipShippingId() && (!$rate->getHasExtraCharge() || (bool) $vendor->getIsExtraChargeShippingDefault() == (bool) $rate->getIsExtraCharge())) { $curMatchRank = 100; $isCurMatch = true; } elseif ($defPatternMatch) { $curMatchRank = 10; $isCurMatch = true; } elseif (empty($details['methods'][$vId])) { $curMatchRank = 1; $isCurMatch = true; } if ($isCurMatch && $curMatchRank > $matchRank[$vId]) { $matchRank[$vId] = $curMatchRank; // updating already chosen vendor shipping method price $details['methods'][$vId] = $data; $costsByVid[$vId] = $data['cost']; $pricesByVid[$vId] = $data['price']; } } $price = $cost = 0; foreach ($pricesByVid as $_vId => $_price) { $price = $hl->applyEstimateTotalPriceMethod($price, $pricesByVid[$_vId]); $cost = $hl->applyEstimateTotalCostMethod($cost, $costsByVid[$_vId]); } if ($rates) { $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier('udsplit'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('total'); $method->setMethodTitle('Total'); $method->setCost($price); //$method->setPrice($this->getMethodPrice($price, 'total')); $method->setPrice($price); $result->append($method); } else { $result->append($hlp->errorResult('udsplit')); } $address->setUdropshipShippingDetails(Zend_Json::encode($details)); $address->setShippingMethod('udsplit_total'); $address->setShippingDescription($this->getConfigData('title')); $address->setShippingAmount($price); Mage::dispatchEvent('udsplit_carrier_collect_after', array('request' => $request, 'result' => $result, 'address' => $address, 'details' => $details)); Mage::dispatchEvent('udropship_carrier_collect_after', array('request' => $request, 'result' => $result, 'address' => $address, 'details' => $details)); return $result; }