From b2ffd402683a03672bea3fcb3ec4a63a5a1c672d Mon Sep 17 00:00:00 2001 From: Gundo Sifhufhi <sifhufhisg@gmail.com> Date: Fri, 3 Feb 2023 09:41:23 +0200 Subject: [PATCH] #2 - Added comments, Removed Unneccessary code. --- Model/Carrier/CustomShipping.php | 93 ++++++++++--------- Plugin/Block/Tracking/PopUpDeliveryDate.php | 3 +- .../model/shipping-rates-validation-rules.js | 3 - 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/Model/Carrier/CustomShipping.php b/Model/Carrier/CustomShipping.php index 679a0a4..d007495 100644 --- a/Model/Carrier/CustomShipping.php +++ b/Model/Carrier/CustomShipping.php @@ -22,6 +22,7 @@ use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory; use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Magento\Shipping\Model\Carrier\AbstractCarrier; use Magento\Shipping\Model\Carrier\AbstractCarrierOnline; +use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\Result; use Magento\Shipping\Model\Rate\ResultFactory; use Magento\Shipping\Model\Simplexml\ElementFactory; @@ -40,7 +41,7 @@ use Psr\Log\LoggerInterface; * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) */ -class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\Model\Carrier\CarrierInterface +class CustomShipping extends AbstractCarrierOnline implements CarrierInterface { /** * Code of the carrier @@ -179,8 +180,9 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } - /** - * Store Base Url + /* + * Gets the base url of the store by stripping the http:// or https:// and wwww. from the url + * leaving just "example.com" since bobgo API uses this format and not the full url as the Identifier * @var \Magento\Store\Model\StoreManagerInterface $this->_storeManager * @return string */ @@ -193,35 +195,36 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ return $storeBase; } - /** - * Make request to bobgo API to get shipping rates + * Make request to bobgo API to get shipping rates for the cart + * After all the required data is collected, it makes a request to the bobgo API to get the shipping rates * @param $payload * @return array */ public function getRates($payload): array { - $response = $this->uRates($payload); - - return $response; + return $this->uRates($payload); } /** - * Collect and get rates - * + * Collect and get rates for this shipping method based on information in $request + * This is a default function that is called by Magento to get the shipping rates for the cart * @param RateRequest $request * @return Result|bool|null */ - public function collectRates(RateRequest $request) + public function collectRates(RateRequest $request): Result|bool|null { /*** Make sure that Shipping method is enabled*/ if (!$this->isActive()) { return false; } - + /** + * Gets the destination company name from Company 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(); - /** @var \Magento\Shipping\Model\Rate\Result $result */ + /** @var Result $result */ $result = $this->_rateFactory->create(); @@ -231,13 +234,12 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ $destCity = $request->getDestCity(); $destStreet = $request->getDestStreet(); + /** Destination Information */ list($destStreet1, $destStreet2, $destStreet3) = $this->destStreet($destStreet); - - /** Origin Information */ list($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb) = $this->storeInformation(); - + /** Get all the items in the cart */ $items = $request->getAllItems(); $itemsArray = []; @@ -285,6 +287,10 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } /** + * Get The store information from the Magento store configuration and return it as an array + * In magento 2 there is origin information in the store information section of the configuration, so we get it from there + * since Store Information has the origin information including suburb field that we Injected upon Bob Go Extension installation + * which is not available in the origin section * @return array */ public function storeInformation(): array @@ -329,16 +335,16 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ ); $baseIdentifier = $this->getBaseUrl(); + return array($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb); } - /** * Get result of request * * @return Result|null */ - public function getResult() + public function getResult(): ?Result { if (!$this->_result) { $this->_result = $this->_trackFactory->create(); @@ -346,7 +352,6 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ return $this->_result; } - /** * Get final price for shipping method with handling fee per package * @@ -355,7 +360,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * @param float $handlingFee * @return float */ - protected function _getPerpackagePrice($cost, $handlingType, $handlingFee) + protected function _getPerpackagePrice($cost, $handlingType, $handlingFee): float { if ($handlingType == AbstractCarrier::HANDLING_TYPE_PERCENT) { return $cost + $cost * $this->_numBoxes * $handlingFee / self::UNITS; @@ -372,7 +377,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * @param float $handlingFee * @return float */ - protected function _getPerorderPrice($cost, $handlingType, $handlingFee) + protected function _getPerorderPrice($cost, $handlingType, $handlingFee): float { if ($handlingType == self::HANDLING_TYPE_PERCENT) { return $cost + $cost * $handlingFee / self::UNITS; @@ -381,7 +386,6 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ return $cost + $handlingFee; } - /** * Get configuration data of carrier * @@ -390,11 +394,11 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * @return array|false * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function getCode($type, $code = '') + public function getCode($type, $code = ''): bool|array { $codes = [ 'method' => [ - 'bobgo_SHIPPING' => __('bobgo Shipping'), + 'bobGo' => __('Bob Go'), ], 'delivery_confirmation_types' => [ 'NO_SIGNATURE_REQUIRED' => __('Not Required'), @@ -420,14 +424,14 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } } - /** - * Get tracking + * Get tracking info by tracking number or tracking request + * Without getTrackingInfo() method, Magento will not show tracking info on frontend * * @param string|string[] $trackings * @return Result|null */ - public function getTracking($trackings) + public function getTracking($trackings): ?Result { $this->setTrackingReqeust(); @@ -443,11 +447,11 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } /** - * Set tracking request + * Set tracking request data for request by getting context from Magento * * @return void */ - protected function setTrackingReqeust() + protected function setTrackingReqeust(): void { $r = new \Magento\Framework\DataObject(); @@ -458,23 +462,23 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } /** - * Send request for tracking + * Send request for the actual tracking info and process response * * @param string[] $tracking * @return void */ - protected function _getXMLTracking($tracking) + protected function _getXMLTracking($tracking): void { $this->_parseTrackingResponse($tracking); } /** - * Parse tracking response + * Parse tracking response and set tracking info * * @param string $trackingValue * @return void */ - protected function _parseTrackingResponse($trackingValue) + protected function _parseTrackingResponse($trackingValue): void { $result = $this->getResult(); $carrierTitle = $this->getConfigData('title'); @@ -508,7 +512,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * * @return string */ - public function getResponse() + public function getResponse(): string { $statuses = ''; if ($this->_result instanceof \Magento\Shipping\Model\Tracking\Result) { @@ -537,7 +541,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * * @return array */ - public function getAllowedMethods() + public function getAllowedMethods(): array { $allowed = explode(',', $this->getConfigData('allowed_methods')); $arr = []; @@ -548,14 +552,13 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ return $arr; } - /** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response - * + * Also another magic function that is required to be implemented by carrier model * @param \Magento\Framework\DataObject $request * @return \Magento\Framework\DataObject */ - protected function _doShipmentRequest(\Magento\Framework\DataObject $request) + protected function _doShipmentRequest(\Magento\Framework\DataObject $request): ?DataObject { return null; @@ -568,7 +571,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * * @return bool */ - public function rollBack($data) + public function rollBack($data): bool { return true; } @@ -581,7 +584,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ * @return array|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function getContainerTypes(\Magento\Framework\DataObject $params = null) + public function getContainerTypes(\Magento\Framework\DataObject $params = null): bool|array { $result = []; $allowedContainers = $this->getConfigData('containers'); @@ -717,7 +720,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ */ private function _requestTracking($trackInfo, array $result): array { - $response = $this->trackbobgoShipment($trackInfo); + $response = $this->trackBobgoShipment($trackInfo); $result = $this->prepareActivity($response[0], $result); @@ -768,7 +771,6 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } } - /** * Prepare received checkpoints and activity from bobgo Shipment Tracking API * @param $response @@ -783,7 +785,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ 'activity' => $checkpoint['status'], 'deliverydate' => $this->formatDate($checkpoint['time']), 'deliverytime' => $this->formatTime($checkpoint['time']), - // 'deliverylocation' => 'Unavailable', + // 'deliverylocation' => 'Unavailable',//TODO: remove this line ]; } return $result; @@ -817,13 +819,12 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } } - /** * Curl request to bobgo Shipment Tracking API * @param $trackInfo * @return mixed */ - private function trackbobgoShipment($trackInfo): mixed + private function trackBobgoShipment($trackInfo): mixed { $this->curl->get(uData::TRACKING . $trackInfo); @@ -833,6 +834,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ } /** + * Build The Payload for bobgo API Request and return response * @param array $payload * @return mixed */ @@ -847,7 +849,6 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\ return $rates; } - /** * @param string $destStreet * @return string[] diff --git a/Plugin/Block/Tracking/PopUpDeliveryDate.php b/Plugin/Block/Tracking/PopUpDeliveryDate.php index 87b6137..420d39b 100644 --- a/Plugin/Block/Tracking/PopUpDeliveryDate.php +++ b/Plugin/Block/Tracking/PopUpDeliveryDate.php @@ -40,8 +40,7 @@ class PopupDeliveryDate foreach ($subject->getTrackingInfo() as $trackingData) { foreach ($trackingData as $trackingInfo) { if ($trackingInfo instanceof Status) { - $carrier = $trackingInfo->getCarrier(); - return $carrier; + return $trackingInfo->getCarrier(); } } } diff --git a/view/frontend/web/js/model/shipping-rates-validation-rules.js b/view/frontend/web/js/model/shipping-rates-validation-rules.js index 0cf80ca..0841e90 100644 --- a/view/frontend/web/js/model/shipping-rates-validation-rules.js +++ b/view/frontend/web/js/model/shipping-rates-validation-rules.js @@ -18,9 +18,6 @@ define( }, 'city': { 'required': true - }, - 'suburb': { - 'required': true } }; } -- GitLab