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

#4 - additions to rates at checkout magento

parent 75f0f6c6
No related branches found
No related tags found
2 merge requests!5Update RAC,!4Resolve "Magento extension :: Rates at checkout"
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
namespace BobGroup\BobGo\Model\Carrier; namespace BobGroup\BobGo\Model\Carrier;
/** /**
* Get the Company information from the request body and return it * Get the AdditionalInfo information from the request body and return it
*/ */
class Company class AdditionalInfo
{ {
/** /**
...@@ -36,4 +36,45 @@ class Company ...@@ -36,4 +36,45 @@ class Company
} }
return $destSub; return $destSub;
} }
/**
* @return mixed|string
*/
public function getDestTelephone(): mixed
{
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['address']['telephone'])) {
$destTelephone = $data['address']['telephone'];
} else {
$destTelephone = '';
}
return $destTelephone;
}
/**
* @var \BobGroup\BobGo\Model\Carrier\AdditionalInfo
*/
public $countryFactory;
public function __construct($countryFactory)
{
$this->countryFactory = $countryFactory;
}
/**
* country full name
*
* @return string
*/
public function getCountryName($countryId): string
{
$countryName = '';
$country = $this->countryFactory->create()->loadByCode($countryId);
if ($country) {
$countryName = $country->getName();
}
return $countryName;
}
} }
...@@ -107,7 +107,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -107,7 +107,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
*/ */
protected JsonFactory $jsonFactory; protected JsonFactory $jsonFactory;
private $cartRepository; private $cartRepository;
public Company $company; public AdditionalInfo $additionalInfo;
/** /**
...@@ -156,8 +156,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -156,8 +156,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
JsonFactory $jsonFactory, JsonFactory $jsonFactory,
CurlFactory $curlFactory, CurlFactory $curlFactory,
array $data = [] array $data = []
)
) { {
$this->_storeManager = $storeManager; $this->_storeManager = $storeManager;
$this->_productCollectionFactory = $productCollectionFactory; $this->_productCollectionFactory = $productCollectionFactory;
...@@ -181,7 +181,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -181,7 +181,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
); );
$this->jsonFactory = $jsonFactory; $this->jsonFactory = $jsonFactory;
$this->curl = $curlFactory->create(); $this->curl = $curlFactory->create();
$this->company = new Company(); $this->additionalInfo = new AdditionalInfo($countryFactory);
} }
...@@ -295,35 +295,40 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -295,35 +295,40 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
return false; return false;
} }
/** /**
* Gets the destination company name from Company Name field in the checkout page * 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 * 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(); $destComp = $this->getDestComp();
$destSuburb = $this->getDestSuburb(); $destSuburb = $this->getDestSuburb();
$destPhone = $this->getDestTelephone();
/** @var \Magento\Shipping\Model\Rate\Result $result */ /** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateFactory->create(); $result = $this->_rateFactory->create();
$destination = $request->getDestPostcode(); $destination = $request->getDestPostcode();
$destCountry = $request->getDestCountryId(); $destCountryCode = $request->getDestCountryId();
$destRegion = $request->getDestRegionCode(); $destRegion = $request->getDestRegionCode();
$destCity = $request->getDestCity(); $destCity = $request->getDestCity();
$destStreet = $request->getDestStreet(); $destStreet = $request->getDestStreet();
$destCountry = $this->getCountryName($destCountryCode);
/** Destination Information */ /** Destination Information */
[$destStreet1, $destStreet2, $destStreet3] = $this->destStreet($destStreet); [$destStreet1, $destStreet2, $destStreet3] = $this->destStreet($destStreet);
/** Origin Information */ /** Origin Information */
[$originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb,$weightUnit] = $this->storeInformation(); [$originStreet, $originRegion, $originCountryCode, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit, $originPhone] = $this->storeInformation();
/** Get all items in cart */ /** Get all items in cart */
$items = $request->getAllItems(); $items = $request->getAllItems();
$itemsArray = []; $itemsArray = [];
$itemsArray = $this->getStoreItems($items, $weightUnit, $itemsArray); $itemsArray = $this->getStoreItems($items, $weightUnit, $itemsArray);
$originCountry = $this->getCountryName($originCountryCode);
$payload = [ $payload = [
'identifier' => $baseIdentifier, 'identifier' => $baseIdentifier,
'rate' => [ 'rate' => [
...@@ -334,8 +339,10 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -334,8 +339,10 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
'city' => $originCity, 'city' => $originCity,
'suburb' => $originSuburb, 'suburb' => $originSuburb,
'province' => $originRegion, 'province' => $originRegion,
'country_code' => $originCountry, 'country_code' => $originCountryCode,
'postal_code' => $originStreet, 'postal_code' => $originStreet,
'telephone' => $originPhone,
'country' => $originCountry,
], ],
'destination' => [ 'destination' => [
...@@ -345,8 +352,10 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -345,8 +352,10 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
'suburb' => $destSuburb, 'suburb' => $destSuburb,
'city' => $destCity, 'city' => $destCity,
'province' => $destRegion, 'province' => $destRegion,
'country_code' => $destCountry, 'country_code' => $destCountryCode,
'postal_code' => $destination, 'postal_code' => $destination,
'telephone' => $destPhone,
'country' => $destCountry
], ],
'items' => $itemsArray, 'items' => $itemsArray,
] ]
...@@ -363,7 +372,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -363,7 +372,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
public function storeInformation(): array public function storeInformation(): array
{ {
/** Store Origin details */ /** Store Origin details */
$originCountry = $this->_scopeConfig->getValue( $originCountryCode = $this->_scopeConfig->getValue(
'general/store_information/country_id', 'general/store_information/country_id',
ScopeInterface::SCOPE_STORE ScopeInterface::SCOPE_STORE
); );
...@@ -400,14 +409,20 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -400,14 +409,20 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
'general/store_information/suburb', 'general/store_information/suburb',
ScopeInterface::SCOPE_STORE ScopeInterface::SCOPE_STORE
); );
$weightUnit = $this->_scopeConfig->getValue( $weightUnit = $this->_scopeConfig->getValue(
'general/locale/weight_unit', 'general/locale/weight_unit',
ScopeInterface::SCOPE_STORE ScopeInterface::SCOPE_STORE
); );
$originTelephone = $this->_scopeConfig->getValue(
'general/store_information/phone',
ScopeInterface::SCOPE_STORE
);
$baseIdentifier = $this->getBaseUrl(); $baseIdentifier = $this->getBaseUrl();
return array($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit); return array($originStreet, $originRegion, $originCountryCode, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb, $weightUnit, $originTelephone);
} }
...@@ -502,7 +517,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -502,7 +517,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
*/ */
public function getTracking($trackings) public function getTracking($trackings)
{ {
$this->setTrackingReqeust(); $this->setTrackingRequest();
if (!is_array($trackings)) { if (!is_array($trackings)) {
$trackings = [$trackings]; $trackings = [$trackings];
...@@ -520,7 +535,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -520,7 +535,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
* *
* @return void * @return void
*/ */
protected function setTrackingReqeust() protected function setTrackingRequest()
{ {
$r = new \Magento\Framework\DataObject(); $r = new \Magento\Framework\DataObject();
...@@ -814,7 +829,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -814,7 +829,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
*/ */
protected function _formatRates(mixed $rates, Result $result): void protected function _formatRates(mixed $rates, Result $result): void
{ {
if (empty($rates)) { if (!isset($rates['rates'])) {
$error = $this->_rateErrorFactory->create(); $error = $this->_rateErrorFactory->create();
$error->setCarrierTitle($this->getConfigData('title')); $error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage($this->getConfigData('specificerrmsg')); $error->setErrorMessage($this->getConfigData('specificerrmsg'));
...@@ -973,14 +988,31 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -973,14 +988,31 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
*/ */
public function getDestComp(): mixed public function getDestComp(): mixed
{ {
return $this->company->getDestComp(); return $this->additionalInfo->getDestComp();
} }
/** /**
* @return mixed|string * @return mixed|string
*/ */
public function getDestSuburb(): mixed public function getDestSuburb(): mixed
{ {
return $this->company->getSuburb(); return $this->additionalInfo->getSuburb();
}
/**
* @return mixed|string
*/
public function getDestTelephone(): mixed
{
return $this->additionalInfo->getDestTelephone();
}
/**
* @return mixed|string
*/
public function getCountryName(mixed $country_id): mixed
{
return $this->additionalInfo->getCountryName($country_id);
} }
/** /**
...@@ -1009,6 +1041,17 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -1009,6 +1041,17 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
{ {
foreach ($items as $item) { foreach ($items as $item) {
$productID = $item->getProductId();
$product = $item->getProduct(); // Product Object
$description = "";
if ($product) {
$productFull = $product->load($productID);
// Remove meta tags from the description
$description = preg_replace('#<[^>]+>#', ' ', $productFull->getDescription());
$description = preg_replace('!\s+!', ' ', $description);
}
$mass = $this->getItemWeight($weightUnit, $item); $mass = $this->getItemWeight($weightUnit, $item);
$itemsArray[] = [ $itemsArray[] = [
...@@ -1016,6 +1059,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car ...@@ -1016,6 +1059,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
'quantity' => $item->getQty(), 'quantity' => $item->getQty(),
'price' => $item->getPrice(), 'price' => $item->getPrice(),
'weight' => round($mass), 'weight' => round($mass),
'name' => $item->getName(),
'description' => $description,
]; ];
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace BobGroup\BobGo\Model\Carrier; namespace BobGroup\BobGo\Model\Carrier;
/** Get Company information if available from the Estimate Shipping Methods Request Body */ /** Get AdditionalInfo information if available from the Estimate Shipping Methods Request Body */
class uSubs class uSubs
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment