Skip to content
Snippets Groups Projects
Commit e15a9a47 authored by Christel Loftus's avatar Christel Loftus
Browse files

collectRates

parent a1543ef9
Branches
Tags 1.0.5
1 merge request!6Rates at checkout
Pipeline #64940 passed
...@@ -77,4 +77,47 @@ class AdditionalInfo ...@@ -77,4 +77,47 @@ class AdditionalInfo
} }
return $countryName; 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];
// }
// }
} }
...@@ -30,6 +30,11 @@ use Magento\Shipping\Model\Tracking\Result\StatusFactory; ...@@ -30,6 +30,11 @@ use Magento\Shipping\Model\Tracking\Result\StatusFactory;
use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface; use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface; 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 * Bob Go shipping implementation
...@@ -41,7 +46,8 @@ use Psr\Log\LoggerInterface; ...@@ -41,7 +46,8 @@ use Psr\Log\LoggerInterface;
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields) * @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 * Code of the carrier
...@@ -288,84 +294,160 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -288,84 +294,160 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
* @param RateRequest $request * @param RateRequest $request
* @return Result|bool|null * @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->getConfigFlag('active')) {
if (!$this->isActive()) { Mage::log('BobGo is not active', null, 'shipping.log');
return false; 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 */ $result = Mage::getModel('shipping/rate_result');
[$destStreet1, $destStreet2, $destStreet3] = $this->destStreet($destStreet);
// Prepare request data for API call
/** Origin Information */ $params = [
[$originStreet, $originRegion, $originCountryCode, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit, $originPhone] = $this->storeInformation(); 'dest_country_id' => $request->getDestCountryId(),
'dest_region_id' => $request->getDestRegionId(),
/** Get all items in cart */ 'dest_postcode' => $request->getDestPostcode(),
$items = $request->getAllItems(); 'package_weight' => $request->getPackageWeight(),
$itemsArray = []; 'package_value' => $request->getPackageValue(),
$itemsArray = $this->getStoreItems($items, $weightUnit, $itemsArray); 'package_qty' => $request->getPackageQty(),
$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); 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; 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 * @return array
*/ */
...@@ -636,13 +718,20 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -636,13 +718,20 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
*/ */
public function getAllowedMethods() public function getAllowedMethods()
{ {
$allowed = explode(',', $this->getConfigData('allowed_methods')); return ['bobgo' => $this->getConfigData('name')];
$arr = []; // $allowed = explode(',', $this->getConfigData('allowed_methods'));
foreach ($allowed as $k) { // $arr = [];
$arr[$k] = $this->getCode('method', $k); // 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;
} }
......
...@@ -12,12 +12,7 @@ class uSubs ...@@ -12,12 +12,7 @@ class uSubs
public function getDestComp(): mixed public function getDestComp(): mixed
{ {
$data = json_decode(file_get_contents('php://input'), true); $data = json_decode(file_get_contents('php://input'), true);
return isset($data['address']['company']) ? $data['address']['company'] : '';
if (isset($data['address']['company'])) {
$destComp = $data['address']['company'];
} else {
$destComp = '';
}
return $destComp;
} }
} }
...@@ -9,17 +9,14 @@ ...@@ -9,17 +9,14 @@
<carriers> <carriers>
<bobgo> <bobgo>
<active>0</active> <active>1</active>
<!-- <sallowspecific>1</sallowspecific>
<specificcountry>ZA</specificcountry>-->
<price>0.00</price> <price>0.00</price>
<model>BobGroup\BobGo\Model\Carrier\BobGo</model> <model>BobGroup\BobGo\Model\Carrier\BobGo</model>
<name>Fixed</name> <name>BobGo</name>
<max_package_weight>300</max_package_weight> <max_package_weight>300</max_package_weight>
<title>Bob Go</title> <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> <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> </bobgo>
</carriers> </carriers>
</default> </default>
</config> </config>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment