From e15a9a47447e2f27c523a62f31839fec11d9d25c Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Tue, 30 Jul 2024 13:49:35 +0200 Subject: [PATCH] collectRates --- Model/Carrier/AdditionalInfo.php | 43 ++++++ Model/Carrier/BobGo.php | 241 +++++++++++++++++++++---------- Model/Carrier/uSubs.php | 9 +- etc/config.xml | 7 +- 4 files changed, 212 insertions(+), 88 deletions(-) diff --git a/Model/Carrier/AdditionalInfo.php b/Model/Carrier/AdditionalInfo.php index 0b002a0..fff3c92 100644 --- a/Model/Carrier/AdditionalInfo.php +++ b/Model/Carrier/AdditionalInfo.php @@ -77,4 +77,47 @@ class AdditionalInfo } return $countryName; } + +// // Method to get the per-package price +// protected function _getPerpackagePrice($cost, $handlingType, $handlingFee) +// { +// if ($handlingType == AbstractCarrier::HANDLING_TYPE_PERCENT) { +// return $cost + $cost * $this->_numBoxes * $handlingFee / self::UNITS; +// } +// return $cost + $this->_numBoxes * $handlingFee; +// } +// +// // Method to get the per-order price +// protected function _getPerorderPrice($cost, $handlingType, $handlingFee) +// { +// if ($handlingType == self::HANDLING_TYPE_PERCENT) { +// return $cost + $cost * $handlingFee / self::UNITS; +// } +// return $cost + $handlingFee; +// } +// +// // Method to get configuration data of the carrier +// public function getCode($type, $code = '') +// { +// $codes = [ +// 'method' => [ +// 'bobGo' => __('BobGo'), +// ], +// 'unit_of_measure' => [ +// 'KGS' => __('Kilograms'), +// 'LBS' => __('Pounds'), +// ], +// ]; +// if (!isset($codes[$type])) { +// return false; +// } elseif ('' === $code) { +// return $codes[$type]; +// } +// if (!isset($codes[$type][$code])) { +// return false; +// } else { +// return $codes[$type][$code]; +// } +// } + } diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php index 2c55674..6dcd649 100644 --- a/Model/Carrier/BobGo.php +++ b/Model/Carrier/BobGo.php @@ -30,6 +30,11 @@ use Magento\Shipping\Model\Tracking\Result\StatusFactory; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface; +use Mage; +use Mage_Shipping_Model_Carrier_Abstract; +use Mage_Shipping_Model_Carrier_Interface; +use Varien_Object; + /** * Bob Go shipping implementation @@ -41,7 +46,8 @@ use Psr\Log\LoggerInterface; * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) */ -class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Carrier\CarrierInterface + +class BobGo extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface { /** * Code of the carrier @@ -288,84 +294,160 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car * @param RateRequest $request * @return Result|bool|null */ - public function collectRates(RateRequest $request) +// public function collectRates(RateRequest $request) +// { +// if (!$this->getConfigFlag('active')) { +// Mage::log('BobGo is not active', null, 'shipping.log'); +// return false; +// } +// +// $result = Mage::getModel('shipping/rate_result'); +// $rate = Mage::getModel('shipping/rate_result_method'); +// $rate->setCarrier('bobgo'); +// $rate->setCarrierTitle($this->getConfigData('title')); +// +// $result->append($rate); +// +// Mage::log('BobGo rates collected', null, 'shipping.log'); +// +// +// /** +// * Gets the destination company name from AdditionalInfo Name field in the checkout page +// * This method is used is the last resort to get the company name since the company name is not available in _rateFactory +// */ +// $destComp = $this->getDestComp(); +// $destSuburb = $this->getDestSuburb(); +// $destPhone = $this->getDestTelephone(); +// +// /** @var \Magento\Shipping\Model\Rate\Result $result */ +// +// $result = $this->_rateFactory->create(); +// +// $destination = $request->getDestPostcode(); +// $destCountryCode = $request->getDestCountryId(); +// $destRegion = $request->getDestRegionCode(); +// $destCity = $request->getDestCity(); +// $destStreet = $request->getDestStreet(); +// +// $destCountry = $this->getCountryName($destCountryCode); +// +// /** Destination Information */ +// [$destStreet1, $destStreet2, $destStreet3] = $this->destStreet($destStreet); +// +// +// /** Origin Information */ +// [$originStreet, $originRegion, $originCountryCode, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit, $originPhone] = $this->storeInformation(); +// +// /** Get all items in cart */ +// $items = $request->getAllItems(); +// $itemsArray = []; +// $itemsArray = $this->getStoreItems($items, $weightUnit, $itemsArray); +// +// $originCountry = $this->getCountryName($originCountryCode); +// +// +// $payload = [ +// 'identifier' => $baseIdentifier, +// 'rate' => [ +// 'origin' => [ +// 'company' => $storeName, +// 'address1' => $originStreet1, +// 'address2' => $originStreet2, +// 'city' => $originCity, +// 'suburb' => $originSuburb, +// 'province' => $originRegion, +// 'country_code' => $originCountryCode, +// 'postal_code' => $originStreet, +// 'telephone' => $originPhone, +// 'country' => $originCountry, +// +// ], +// 'destination' => [ +// 'company' => $destComp, +// 'address1' => $destStreet1, +// 'address2' => $destStreet2, +// 'suburb' => $destSuburb, +// 'city' => $destCity, +// 'province' => $destRegion, +// 'country_code' => $destCountryCode, +// 'postal_code' => $destination, +// 'telephone' => $destPhone, +// 'country' => $destCountry +// ], +// 'items' => $itemsArray, +// ] +// ]; +// +// $this->_getRates($payload, $result); +// +// return $result; +// } + + public function collectRates(Mage_Shipping_Model_Rate_Request $request) { - /*** Make sure that Shipping method is enabled*/ - if (!$this->isActive()) { + if (!$this->getConfigFlag('active')) { + Mage::log('BobGo is not active', null, 'shipping.log'); return false; } - /** - * Gets the destination company name from AdditionalInfo Name field in the checkout page - * This method is used is the last resort to get the company name since the company name is not available in _rateFactory - */ - $destComp = $this->getDestComp(); - $destSuburb = $this->getDestSuburb(); - $destPhone = $this->getDestTelephone(); - - /** @var \Magento\Shipping\Model\Rate\Result $result */ - - $result = $this->_rateFactory->create(); - - $destination = $request->getDestPostcode(); - $destCountryCode = $request->getDestCountryId(); - $destRegion = $request->getDestRegionCode(); - $destCity = $request->getDestCity(); - $destStreet = $request->getDestStreet(); - - $destCountry = $this->getCountryName($destCountryCode); - - /** Destination Information */ - [$destStreet1, $destStreet2, $destStreet3] = $this->destStreet($destStreet); - - - /** Origin Information */ - [$originStreet, $originRegion, $originCountryCode, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit, $originPhone] = $this->storeInformation(); - - /** Get all items in cart */ - $items = $request->getAllItems(); - $itemsArray = []; - $itemsArray = $this->getStoreItems($items, $weightUnit, $itemsArray); - - $originCountry = $this->getCountryName($originCountryCode); - - - $payload = [ - 'identifier' => $baseIdentifier, - 'rate' => [ - 'origin' => [ - 'company' => $storeName, - 'address1' => $originStreet1, - 'address2' => $originStreet2, - 'city' => $originCity, - 'suburb' => $originSuburb, - 'province' => $originRegion, - 'country_code' => $originCountryCode, - 'postal_code' => $originStreet, - 'telephone' => $originPhone, - 'country' => $originCountry, - - ], - 'destination' => [ - 'company' => $destComp, - 'address1' => $destStreet1, - 'address2' => $destStreet2, - 'suburb' => $destSuburb, - 'city' => $destCity, - 'province' => $destRegion, - 'country_code' => $destCountryCode, - 'postal_code' => $destination, - 'telephone' => $destPhone, - 'country' => $destCountry - ], - 'items' => $itemsArray, - ] + + $result = Mage::getModel('shipping/rate_result'); + + // Prepare request data for API call + $params = [ + 'dest_country_id' => $request->getDestCountryId(), + 'dest_region_id' => $request->getDestRegionId(), + 'dest_postcode' => $request->getDestPostcode(), + 'package_weight' => $request->getPackageWeight(), + 'package_value' => $request->getPackageValue(), + 'package_qty' => $request->getPackageQty(), ]; - $this->_getRates($payload, $result); + try { + $apiResponse = $this->_fetchRatesFromApi($params); + if ($apiResponse && isset($apiResponse['rates'])) { + foreach ($apiResponse['rates'] as $rateData) { + $rate = Mage::getModel('shipping/rate_result_method'); + $rate->setCarrier($this->_code); + $rate->setCarrierTitle($this->getConfigData('title')); + $rate->setMethod($rateData['method']); + $rate->setMethodTitle($rateData['method_title']); + $rate->setPrice($rateData['price']); + $rate->setCost($rateData['cost']); + $result->append($rate); + } + } else { + Mage::log('No rates returned from API', null, 'shipping.log'); + } + } catch (Exception $e) { + Mage::logException($e); + Mage::log('Error fetching rates from API: ' . $e->getMessage(), null, 'shipping.log'); + } return $result; } + protected function _fetchRatesFromApi($params) + { + $url = \BobGroup\BobGo\Model\Carrier\uData::RATES_ENDPOINT; + $client = new Varien_Http_Client($url); + $client->setMethod(Varien_Http_Client::POST); + $client->setRawData(json_encode($params), 'application/json'); + + try { + $response = $client->request(); + if ($response->isSuccessful()) { + return json_decode($response->getBody(), true); + } else { + Mage::log('API request failed with status: ' . $response->getStatus(), null, 'shipping.log'); + } + } catch (Exception $e) { + Mage::logException($e); + Mage::log('Exception during API request: ' . $e->getMessage(), null, 'shipping.log'); + } + + return false; + } + /** * @return array */ @@ -636,13 +718,20 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car */ public function getAllowedMethods() { - $allowed = explode(',', $this->getConfigData('allowed_methods')); - $arr = []; - foreach ($allowed as $k) { - $arr[$k] = $this->getCode('method', $k); - } + return ['bobgo' => $this->getConfigData('name')]; +// $allowed = explode(',', $this->getConfigData('allowed_methods')); +// $arr = []; +// foreach ($allowed as $k) { +// $arr[$k] = $this->getCode('method', $k); +// } +// +// return $arr; + } - return $arr; + // Method to check if tracking is available + public function isTrackingAvailable() + { + return true; } diff --git a/Model/Carrier/uSubs.php b/Model/Carrier/uSubs.php index 5f7dcd8..b7438d2 100644 --- a/Model/Carrier/uSubs.php +++ b/Model/Carrier/uSubs.php @@ -12,12 +12,7 @@ class uSubs public function getDestComp(): mixed { $data = json_decode(file_get_contents('php://input'), true); - - if (isset($data['address']['company'])) { - $destComp = $data['address']['company']; - } else { - $destComp = ''; - } - return $destComp; + return isset($data['address']['company']) ? $data['address']['company'] : ''; } } + diff --git a/etc/config.xml b/etc/config.xml index eb34051..8159723 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -9,17 +9,14 @@ <carriers> <bobgo> - <active>0</active> -<!-- <sallowspecific>1</sallowspecific> - <specificcountry>ZA</specificcountry>--> + <active>1</active> <price>0.00</price> <model>BobGroup\BobGo\Model\Carrier\BobGo</model> - <name>Fixed</name> + <name>BobGo</name> <max_package_weight>300</max_package_weight> <title>Bob Go</title> <specificerrmsg>We could not find any shipping rates for your delivery address. Please make sure your address is correct. If you are experiencing problems, please contact us.</specificerrmsg> </bobgo> </carriers> - </default> </config> -- GitLab