Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgo-magento-extension
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgo-magento-extension
Commits
e15a9a47
Commit
e15a9a47
authored
1 year ago
by
Christel Loftus
Browse files
Options
Downloads
Patches
Plain Diff
collectRates
parent
a1543ef9
No related branches found
No related tags found
1 merge request
!6
Rates at checkout
Pipeline
#64940
passed
1 year ago
Stage: tagged_release
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Model/Carrier/AdditionalInfo.php
+43
-0
43 additions, 0 deletions
Model/Carrier/AdditionalInfo.php
Model/Carrier/BobGo.php
+165
-76
165 additions, 76 deletions
Model/Carrier/BobGo.php
Model/Carrier/uSubs.php
+2
-7
2 additions, 7 deletions
Model/Carrier/uSubs.php
etc/config.xml
+2
-5
2 additions, 5 deletions
etc/config.xml
with
212 additions
and
88 deletions
Model/Carrier/AdditionalInfo.php
+
43
−
0
View file @
e15a9a47
...
...
@@ -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];
// }
// }
}
This diff is collapsed.
Click to expand it.
Model/Carrier/BobGo.php
+
165
−
76
View file @
e15a9a47
...
...
@@ -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
);
$result
=
Mage
::
getModel
(
'shipping/rate_result'
);
/** 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
,
]
// 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
$arr
;
return
[
'bobgo'
=>
$this
->
getConfigData
(
'name'
)];
// $allowed = explode(',', $this->getConfigData('allowed_methods'));
// $arr = [];
// foreach ($allowed as $k) {
// $arr[$k] = $this->getCode('method', $k);
// }
//
// return $arr;
}
// Method to check if tracking is available
public
function
isTrackingAvailable
()
{
return
true
;
}
...
...
This diff is collapsed.
Click to expand it.
Model/Carrier/uSubs.php
+
2
−
7
View file @
e15a9a47
...
...
@@ -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'
]
:
''
;
}
}
This diff is collapsed.
Click to expand it.
etc/config.xml
+
2
−
5
View file @
e15a9a47
...
...
@@ -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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment