diff --git a/Block/TrackOrderLink.php b/Block/TrackOrderLink.php new file mode 100644 index 0000000000000000000000000000000000000000..ada8c220e6cfa6c217f6caf5ce949885487b5470 --- /dev/null +++ b/Block/TrackOrderLink.php @@ -0,0 +1,41 @@ +<?php + +namespace BobGroup\BobGo\Block; + +use Magento\Framework\View\Element\Html\Link\Current; +use Magento\Framework\App\Config\ScopeConfigInterface; + +class TrackOrderLink extends Current +{ + protected $scopeConfig; + + public function __construct( + \Magento\Framework\View\Element\Template\Context $context, + ScopeConfigInterface $scopeConfig, + \Magento\Framework\App\DefaultPathInterface $defaultPath, + array $data = [] + ) { + $this->scopeConfig = $scopeConfig; + parent::__construct($context, $defaultPath, $data); + } + + protected function _toHtml() + { + // Check if the Track My Order feature is enabled + $isEnabled = $this->scopeConfig->isSetFlag( + 'carriers/bobgo/enable_track_order', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + + // Return an empty string if the feature is disabled + if (!$isEnabled) { + return ''; + } + + // Use the parent class's rendering method + return parent::_toHtml(); + } +} + + + diff --git a/Block/TrackingBlock.php b/Block/TrackingBlock.php new file mode 100644 index 0000000000000000000000000000000000000000..647cb3dbec3f6b0e662c5b75389566ab5e77894e --- /dev/null +++ b/Block/TrackingBlock.php @@ -0,0 +1,26 @@ +<?php + +namespace BobGroup\BobGo\Block; + +use Magento\Framework\View\Element\Template; +use Magento\Framework\Registry; + +class TrackingBlock extends \Magento\Framework\View\Element\Template +{ + protected $registry; + + public function __construct( + Template\Context $context, + Registry $registry, + array $data = [] + ) { + $this->registry = $registry; + parent::__construct($context, $data); + } + + public function getResponse() + { + return $this->registry->registry('shipment_data'); + } +} + diff --git a/Controller/Tracking/Index.php b/Controller/Tracking/Index.php new file mode 100644 index 0000000000000000000000000000000000000000..78e527f3cf741512ba42d898c378fe839204254a --- /dev/null +++ b/Controller/Tracking/Index.php @@ -0,0 +1,108 @@ +<?php +namespace BobGroup\BobGo\Controller\Tracking; + +use Magento\Framework\App\Action\Context; +use Magento\Framework\View\Result\PageFactory; +use Magento\Framework\Registry; +use Psr\Log\LoggerInterface; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Controller\Result\RedirectFactory; +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\HTTP\Client\Curl; +use Magento\Store\Model\StoreManagerInterface; +use BobGroup\BobGo\Model\Carrier\UData; + +class Index extends \Magento\Framework\App\Action\Action +{ + protected $resultPageFactory; + protected $jsonFactory; + protected $curl; + protected $logger; + protected $scopeConfig; + protected $redirectFactory; + protected $registry; + protected StoreManagerInterface $storeManager; + + public function __construct( + Context $context, + PageFactory $resultPageFactory, + JsonFactory $jsonFactory, + LoggerInterface $logger, + ScopeConfigInterface $scopeConfig, + RedirectFactory $redirectFactory, + StoreManagerInterface $storeManager, + Curl $curl, + Registry $registry + ) { + $this->resultPageFactory = $resultPageFactory; + $this->jsonFactory = $jsonFactory; + $this->logger = $logger; + $this->scopeConfig = $scopeConfig; + $this->redirectFactory = $redirectFactory; + $this->storeManager = $storeManager; + $this->curl = $curl; + $this->registry = $registry; + parent::__construct($context); + } + + public function execute() + { + // This is only an extra check after the TrackOrderLink block + // Check if the "Track My Order" feature is enabled + $isEnabled = $this->scopeConfig->isSetFlag( + 'carriers/bobgo/enable_track_order', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + + if (!$isEnabled) { + // If the feature is disabled, redirect to home page or show a 404 error + return $this->redirectFactory->create()->setPath('noroute'); + } + + $trackingReference = $this->getRequest()->getParam('order_reference'); + + $channel = $this->getStoreUrl(); + + if ($trackingReference) { + $trackingUrl = sprintf(UData::TRACKING, $channel, $trackingReference); + $this->curl->get($trackingUrl); + $response = $this->curl->getBody(); + + $decodedResponse = json_decode($response, true); + + if (is_array($decodedResponse) && isset($decodedResponse[0])) { + $shipmentData = $decodedResponse[0]; + + // Save data to the registry + $this->registry->register('shipment_data', $shipmentData); + + } else { + // Return early the response is not valid + return $this->resultPageFactory->create(); + } + } + + return $this->resultPageFactory->create(); + } + + private function getStoreUrl(): string + { + $url = $this->storeManager->getStore()->getBaseUrl(); + + // Remove protocol (http:// or https://) + $host = preg_replace('#^https?://#', '', $url); + + // Ensure $host is a string before using it in explode + $host = $host ?? ''; + + // Remove everything after the host (e.g., paths, query strings) + $host = explode('/', $host)[0]; + + // If the host starts with 'www.', remove it + if (strpos($host, 'www.') === 0) { + $host = substr($host, 4); + } + + return $host; + } +} diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php index 386e4cbc07cd86549a73eaed2cfa25fa22a35185..2809b13f9ba6364acd2667ee292656b11e410329 100644 --- a/Model/Carrier/BobGo.php +++ b/Model/Carrier/BobGo.php @@ -527,141 +527,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Get tracking - * - * @param string|string[] $trackings - * @return \Magento\Shipping\Model\Tracking\Result|null - */ - public function getTracking($trackings) - { - $this->setTrackingRequest(); // Ensure this method is correctly defined - - if (!is_array($trackings)) { - $trackings = [$trackings]; - } - - foreach ($trackings as $tracking) { - $this->_getXMLTracking([$tracking]); // Ensure _getXMLTracking processes tracking correctly - } - - return $this->_result; // Ensure _result is a \Magento\Shipping\Model\Tracking\Result - } - - /** - * Set tracking request - * - * @return void - */ - protected function setTrackingRequest() - { - $r = new \Magento\Framework\DataObject(); - - $account = $this->getConfigData('account'); - $r->setData('account', $account); // Using setData with the key 'account' - - $this->_rawTrackingRequest = $r; - } - - /** - * Get tracking request - * - * @return \Magento\Framework\DataObject|null - */ - protected function getTrackingRequest(): ?\Magento\Framework\DataObject - { - return $this->_rawTrackingRequest; - } - - /** - * Send request for tracking - * - * @param string[] $tracking - * @return void - */ - protected function _getXMLTracking($tracking) - { - $this->_parseTrackingResponse($tracking); - } - - /** - * Parse tracking response - * - * @param string|array<int,string> $trackingValue - * @return void - */ - protected function _parseTrackingResponse($trackingValue) - { - $result = $this->getResult(); - $carrierTitle = $this->getConfigData('title'); - $counter = 0; - - if (!is_array($trackingValue)) { - $trackingValue = [$trackingValue]; - } - - foreach ($trackingValue as $trackingReference) { - $tracking = $this->_trackStatusFactory->create(); - - $tracking->setCarrier(self::CODE); - $tracking->setCarrierTitle($carrierTitle); - - // Adjust as needed based on the environment - $tracking->setUrl(UData::TRACKING . $trackingReference); - $tracking->setTracking($trackingReference); - $tracking->addData($this->processTrackingDetails($trackingReference)); - - if ($result) { - $result->append($tracking); - $counter++; - } - } - - // Tracking Details Not Available - if ($counter === 0) { - $this->appendTrackingError( - $trackingValue[0] ?? '', - (string)__('For some reason we can\'t retrieve tracking info right now.') - ); - } - } - - /** - * Get tracking response - * - * @return string - */ - public function getResponse(): string - { - $statuses = ''; - - // If $_result is of type \Magento\Shipping\Model\Tracking\Result, handle it - if ($this->_result instanceof \Magento\Shipping\Model\Tracking\Result) { - if ($trackings = $this->_result->getAllTrackings()) { - foreach ($trackings as $tracking) { - if ($data = $tracking->getAllData()) { - if (!empty($data['status'])) { - $statuses .= __($data['status']) . "\n<br/>"; - } else { - $statuses .= __('Empty response') . "\n<br/>"; - } - } - } - } - } - -// // Handle \Magento\Shipping\Model\Rate\Result if needed -// if ($this->_result instanceof \Magento\Shipping\Model\Rate\Result) { -// // Implement the logic for Rate\Result if applicable -// } - - if (trim($statuses) === '') { - $statuses = (string)__('Empty response'); - } - - return $statuses; - } - /** * Get allowed shipping methods * @@ -773,55 +638,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $data; } - /** - * Parse track details response from Bob Go. - * - * @param string $trackInfo - * @return array<string, array<int, array<string, string>>> - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) - */ - private function processTrackingDetails(string $trackInfo): array - { - $result = [ - 'shippeddate' => [], // Initializing as an array of arrays - 'deliverydate' => [], // Initializing as an array of arrays - 'deliverytime' => [], // Initializing as an array of arrays - 'deliverylocation' => [], // Initializing as an array of arrays - 'weight' => [], // Initializing as an array of arrays - 'progressdetail' => [], // This will be populated with an array of arrays - ]; - - $result = $this->_requestTracking($trackInfo, $result); - - return $result; - } - - /** - * Append error message to rate result instance. - * - * @param string $trackingValue - * @param string $errorMessage - * @return void - */ - private function appendTrackingError(string $trackingValue, string $errorMessage): void - { - $error = $this->_trackErrorFactory->create(); - $error->setCarrier(self::CODE); - $error->setCarrierTitle($this->getConfigData('title')); - $error->setTracking($trackingValue); - $error->setErrorMessage($errorMessage); - - $result = $this->getResult(); - - if ($result !== null) { - $result->append($error); - } else { - // Handle the case where $result is null, such as logging an error - $this->_logger->error('Failed to append tracking error: Result object is null.'); - } - } - /** * Format a date to 'd M Y'. * @@ -883,26 +699,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Perform API Request for Shipment Tracking to Bob Go API and return response. - * - * @param string $trackInfo The tracking information or tracking ID. - * @param array<string,array<int,array<string,string>>> $result The result array to be - * populated with tracking details. - * @return array<string, array<int, array<string, string>>> The updated result array with tracking details. - */ - private function _requestTracking(string $trackInfo, array $result): array - { - $response = $this->trackBobGoShipment($trackInfo); - - // Validate that the response is an array and contains at least one element - if (is_array($response) && isset($response[0]) && is_array($response[0])) { - $result = $this->prepareActivity($response[0], $result); - } - - return $result; - } - /** * Format rates from Bob Go API response and append to rate result instance of carrier. * @@ -976,35 +772,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Prepare received checkpoints and activity from Bob Go Shipment Tracking API. - * - * @param array<string,mixed> $response The API response containing tracking checkpoints. - * @param array<string,array<int,array<string,string>>> $result The result array to be - * populated with activity details. - * @return array<string, array<int, array<string, string>>> The updated result array with activity details. - */ - private function prepareActivity(array $response, array $result): array - { - if (isset($response['checkpoints']) && is_array($response['checkpoints'])) { - foreach ($response['checkpoints'] as $checkpoint) { - if (is_array($checkpoint) && - isset($checkpoint['status'], $checkpoint['time']) && - is_string($checkpoint['status']) && - is_string($checkpoint['time']) - ) { - $result['progressdetail'][] = [ - 'activity' => $checkpoint['status'], - 'deliverydate' => $this->formatDate($checkpoint['time']), - 'deliverytime' => $this->formatTime($checkpoint['time']), - ]; - } - } - } - - return $result; - } - /** * Get Working Days between time of checkout and delivery date (min and max). * @@ -1037,21 +804,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $no_days - $weekends; } - /** - * Curl request to Bob Go Shipment Tracking API. - * - * @param string $trackInfo The tracking information or tracking ID. - * @return mixed The decoded API response. - */ - private function trackBobGoShipment(string $trackInfo): mixed - { - $this->curl->get(UData::TRACKING . $trackInfo); - - $response = $this->curl->getBody(); - - return json_decode($response, true); - } - /** * Build the payload for Bob Go API request and return the response. * @@ -1199,27 +951,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $itemsArray; } - /** - * Checks if the required data fields are present in the request. - * - * @param \Magento\Framework\DataObject $request The data object containing the request information. - * @return bool True if all required fields are present, otherwise false. - */ - public function hasRequiredData(\Magento\Framework\DataObject $request): bool - { - $requiredFields = [ - 'dest_country_id', - 'dest_region_id', - ]; - - foreach ($requiredFields as $field) { - if (!$request->getData($field)) { - return false; - } - } - return true; - } - /** * Trigger a test for rates. * diff --git a/Model/Carrier/UData.php b/Model/Carrier/UData.php index 47b5d8b050f519a4e96f2ae432f3a0ea6ab70e0d..d173baaf9bf68790a4b4e7490eff6e79b408105e 100644 --- a/Model/Carrier/UData.php +++ b/Model/Carrier/UData.php @@ -12,12 +12,12 @@ class UData * * @var string */ - public const TRACKING = 'https://api.bobgo.co.za/tracking?channel=%s&tracking_reference=%s'; + public const TRACKING = 'https://api.dev.bobgo.co.za/tracking?channel=%s&tracking_reference=%s'; /** * Rates API Endpoint * * @var string */ - public const RATES_ENDPOINT = 'https://api.bobgo.co.za/rates-at-checkout/magento'; + public const RATES_ENDPOINT = 'https://api.dev.bobgo.co.za/rates-at-checkout/magento'; } diff --git a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php b/Plugin/Block/DataProviders/Tracking/ChangeTitle.php deleted file mode 100644 index a035d238ef81b90a649f0b7ae33541f6b7c6e7ee..0000000000000000000000000000000000000000 --- a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Plugin\Block\DataProviders\Tracking; - -use BobGroup\BobGo\Model\Carrier; -use Magento\Shipping\Model\Tracking\Result\Status; -use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject; - -/** - * Plugin to change delivery date title with bobgo customized value - */ - -class ChangeTitle -{ - /** - * Title modification in case if bobgo used as carrier - * - * @param Subject $subject - * @param \Magento\Framework\Phrase|string $result - * @param Status $trackingStatus - * @return \Magento\Framework\Phrase|string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterGetTitle(Subject $subject, $result, Status $trackingStatus) - { - if ($trackingStatus->getCarrier() === \BobGroup\BobGo\Model\Carrier\BobGo::CODE) { - $result = __('Expected delivery:'); - } - return $result; - } -} diff --git a/Plugin/Block/Tracking/PopUpDeliveryDate.php b/Plugin/Block/Tracking/PopUpDeliveryDate.php deleted file mode 100644 index bb375285de7c84f6104c1b4014689803c4e9365f..0000000000000000000000000000000000000000 --- a/Plugin/Block/Tracking/PopUpDeliveryDate.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Plugin\Block\Tracking; - -use Magento\Shipping\Block\Tracking\Popup; -use Magento\Shipping\Model\Tracking\Result\Status; - -/* - * Plugin to update delivery date value in case if Bob Go is a carrier used - */ -class PopupDeliveryDate -{ - /** - * Bob Go carrier code - */ - private const BOB_GO_CARRIER_CODE = 'bobgo_carrier_code'; // Replace with your actual carrier code - - /** - * Show only date for expected delivery in case if Bob Go is a carrier - * - * @param Popup $subject - * @param string $result - * @param string $date - * @param string $time - * @return string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time) - { - if ($this->getCarrier($subject) === self::BOB_GO_CARRIER_CODE) { - $result = $subject->formatDeliveryDate($date); - } - return $result; - } - - /** - * Retrieve carrier name from tracking info - * - * @param Popup $subject - * @return string - */ - private function getCarrier(Popup $subject): string - { - foreach ($subject->getTrackingInfo() as $trackingData) { - foreach ($trackingData as $trackingInfo) { - if ($trackingInfo instanceof Status) { - return $trackingInfo->getCarrier() ?? ''; - } - } - } - return ''; - } -} diff --git a/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php b/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php deleted file mode 100644 index 4d8bd9d0a66447160396590cc4b878cb8acfad25..0000000000000000000000000000000000000000 --- a/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php +++ /dev/null @@ -1,62 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Test\Unit\Plugin\Block\DataProviders\Tracking; - -use BobGroup\BobGo\Plugin\Block\DataProviders\Tracking\ChangeTitle; -use BobGroup\BobGo\Model\Carrier\BobGo; -use Magento\Shipping\Model\Tracking\Result\Status; -use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject; -use PHPUnit\Framework\TestCase; - -class ChangeTitleTest extends TestCase -{ - /** - * @var ChangeTitle - */ - private $plugin; - - protected function setUp(): void - { - // Instantiate the ChangeTitle plugin - $this->plugin = new ChangeTitle(); - } - - public function testAfterGetTitleWithBobGoCarrier(): void - { - // Create a custom Status object with BobGo carrier - $status = $this->getMockBuilder(Status::class) - ->setMethods(['getCarrier']) - ->getMock(); - - $status->method('getCarrier')->willReturn(BobGo::CODE); - - // Mock the Subject class - $subjectMock = $this->createMock(Subject::class); - - // Call the plugin method afterGetTitle - $result = $this->plugin->afterGetTitle($subjectMock, 'Original Title', $status); - - // Assert that the title was changed for BobGo carrier - $this->assertEquals('Expected delivery:', $result); - } - - public function testAfterGetTitleWithOtherCarrier(): void - { - // Create a custom Status object with a different carrier - $status = $this->getMockBuilder(Status::class) - ->setMethods(['getCarrier']) - ->getMock(); - - $status->method('getCarrier')->willReturn('other_carrier_code'); - - // Mock the Subject class - $subjectMock = $this->createMock(Subject::class); - - // Call the plugin method afterGetTitle - $originalTitle = 'Original Title'; - $result = $this->plugin->afterGetTitle($subjectMock, $originalTitle, $status); - - // Assert that the title was not changed for other carriers - $this->assertEquals($originalTitle, $result); - } -} diff --git a/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php b/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php deleted file mode 100644 index 6c01770254cd04592f5fbafa8bfccacd4d6bfd27..0000000000000000000000000000000000000000 --- a/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php +++ /dev/null @@ -1,92 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Test\Unit\Plugin\Block\Tracking; - -use BobGroup\BobGo\Plugin\Block\Tracking\PopupDeliveryDate; -use Magento\Shipping\Block\Tracking\Popup; -use Magento\Shipping\Model\Tracking\Result\Status; -use PHPUnit\Framework\TestCase; - -class PopupDeliveryDateTest extends TestCase -{ - /** - * @var PopupDeliveryDate - */ - private $plugin; - - protected function setUp(): void - { - // Instantiate the PopupDeliveryDate plugin - $this->plugin = new PopupDeliveryDate(); - } - - public function testAfterFormatDeliveryDateTimeWithBobGoCarrier(): void - { - // Create an instance of the Status class - $status = new Status(); - $status->setCarrier('bobgo_carrier_code'); - - // Mock the Popup class - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([ - ['tracking_info' => $status], - ]); - - // Mock the formatDeliveryDate method - $popupMock->method('formatDeliveryDate') - ->with('2024-08-19') - ->willReturn('Aug 19, 2024'); - - // Call the plugin method afterFormatDeliveryDateTime - $result = $this->plugin->afterFormatDeliveryDateTime( - $popupMock, - 'Aug 19, 2024 10:00 AM', - '2024-08-19', - '10:00 AM' - ); - - // Assert that the time was stripped for BobGo carrier - $this->assertEquals('Aug 19, 2024', $result); - } - - public function testAfterFormatDeliveryDateTimeWithOtherCarrier(): void - { - // Create an instance of the Status class - $status = new Status(); - $status->setCarrier('other_carrier_code'); - - // Mock the Popup class - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([ - ['tracking_info' => $status], - ]); - - // Call the plugin method afterFormatDeliveryDateTime - $result = $this->plugin->afterFormatDeliveryDateTime( - $popupMock, - 'Aug 19, 2024 10:00 AM', - '2024-08-19', - '10:00 AM' - ); - - // Assert that the time remains unchanged for other carriers - $this->assertEquals('Aug 19, 2024 10:00 AM', $result); - } - - public function testGetCarrierWithNoTrackingInfo(): void - { - // Mock the Popup class with no tracking info - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([]); - - // Call the getCarrier method directly - $reflection = new \ReflectionClass($this->plugin); - $method = $reflection->getMethod('getCarrier'); - $method->setAccessible(true); - - $result = $method->invokeArgs($this->plugin, [$popupMock]); - - // Assert that an empty string is returned when no tracking info is available - $this->assertEquals('', $result); - } -} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 410c2c2b9a14e1ecfe406f498fb95d51783d9206..263bc8b6ea4907d1838e777473e854f13cbb0d41 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -28,7 +28,13 @@ <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> <comment>Displays the delivery timeframe and additional service level description, as configured on Bob Go.</comment> </field> + <field id="enable_track_order" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> + <label>Enable Track My Order</label> + <comment>When this setting is enabled, your customers will be presented with a page to track orders.</comment> + <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> + </field> </group> </section> + </system> </config> diff --git a/etc/frontend/routes.xml b/etc/frontend/routes.xml new file mode 100644 index 0000000000000000000000000000000000000000..d6d10467741f62ea0f0f24d7fba12bc9e921e814 --- /dev/null +++ b/etc/frontend/routes.xml @@ -0,0 +1,9 @@ +<?xml version="1.0"?> + +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> + <router id="standard"> + <route id="bobgo" frontName="bobgo"> + <module name="BobGroup_BobGo" /> + </route> + </router> +</config> diff --git a/view/frontend/layout/bobgo_tracking_index.xml b/view/frontend/layout/bobgo_tracking_index.xml new file mode 100644 index 0000000000000000000000000000000000000000..e36faa5492b864f1f27524486ba0ce148a364b64 --- /dev/null +++ b/view/frontend/layout/bobgo_tracking_index.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> + <update handle="customer_account"/> + <head> + <title> + Track my order + </title> + </head> + <body> + <referenceContainer name="content"> + <block class="BobGroup\BobGo\Block\TrackingBlock" name="bobgo.tracking.index" template="BobGroup_BobGo::tracking/index.phtml" cacheable="false" /> + </referenceContainer> + </body> +</page> diff --git a/view/frontend/layout/customer_account.xml b/view/frontend/layout/customer_account.xml new file mode 100644 index 0000000000000000000000000000000000000000..701335df1dc4d481523171edc19e24bbac950feb --- /dev/null +++ b/view/frontend/layout/customer_account.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> + <body> + <referenceBlock name="customer_account_navigation"> + <!-- Add conditional menu to the end of the sidebar --> + <block class="BobGroup\BobGo\Block\TrackOrderLink" name="customer-account-navigation-tracking"> + <arguments> + <argument name="path" xsi:type="string">bobgo/tracking/index</argument> + <argument name="label" xsi:type="string">Track my order</argument> + </arguments> + </block> + </referenceBlock> + </body> +</page> diff --git a/view/frontend/templates/tracking/index.phtml b/view/frontend/templates/tracking/index.phtml new file mode 100644 index 0000000000000000000000000000000000000000..b9f8ba09a5b8ffbcb738f9ab839ca8fbb20dee97 --- /dev/null +++ b/view/frontend/templates/tracking/index.phtml @@ -0,0 +1,127 @@ +<?php +$shipmentData = $this->getResponse(); +?> + +<style> + .track-order-container { + max-width: 600px; + margin: auto; + padding: 20px; + border: 1px solid #ddd; + background-color: #f9f9f9; + } + + .tracking-details h2 { + margin-top: 20px; + } + + .tracking-details p { + margin: 5px 0; + } + + footer { + margin-top: 20px; + font-size: 0.9em; + text-align: center; + } + + footer img { + display: block; + margin: 10px auto; + } +</style> + +<div class="track-order-container"> + <?php if (empty($shipmentData)) : ?> + <form action="<?php echo $this->getUrl('bobgo/tracking/index'); ?>" method="post" class="track-order-form"> + <div class="field"> + <label for="order_reference"><?php echo __('Order Number/Tracking Reference:'); ?></label> + <input type="text" id="order_reference" name="order_reference" required> + </div> + <br> + <div class="actions-toolbar"> + <button type="submit" class="action submit primary"><?php echo __('Track Order'); ?></button> + </div> + </form> + <?php endif; ?> + + <?php if ($shipmentData && is_array($shipmentData)) : ?> + <div class="tracking-details"> + <h2><?php echo __('Shipping Details'); ?></h2> + + <?php if (isset($shipmentData['shipment_tracking_reference'])) : ?> + <p><strong><?php echo __('Shipment:'); ?></strong> <?php echo $shipmentData['shipment_tracking_reference']; ?></p> + <?php endif; ?> + + <?php if (isset($shipmentData['order_number'])) : ?> + <p><strong><?php echo __('Order:'); ?></strong> <?php echo ltrim($shipmentData['order_number'], '0'); ?></p> + <?php endif; ?> + + <?php if (isset($shipmentData['courier_name'])) : ?> + <p><strong><?php echo __('Courier:'); ?></strong> <?php echo $shipmentData['courier_name']; ?></p> + <?php endif; ?> + + <br> + <h2><?php echo __('Tracking Details'); ?></h2> + + <?php if (isset($shipmentData['status']) && isset($shipmentData['status_friendly'])) : ?> + <?php if ($shipmentData['status'] == 'pending-collection') : ?> + <p><?php echo 'Your shipment will be collected soon. Please check back later for more information.' ?></p> + <p><strong><?php echo __('Current Status:'); ?></strong> <?php echo $shipmentData['status_friendly']; ?></p> + <?php elseif (in_array($shipmentData['status'], ['cancelled-by-courier', 'cancelled'])) : ?> + <p><?php echo 'The shipment has been cancelled.'; ?></p> + <p><strong><?php echo __('Current Status:'); ?></strong> <?php echo $shipmentData['status_friendly']; ?></p> + <?php else : ?> + <?php if (empty($shipmentData['checkpoints'])) : ?> + <p><?php echo 'Tracking information is not yet available. Please check back later for more information.'; ?></p> + <?php else : ?> + <p><?php echo 'Tracking information is available below:'; ?></p> + <table id="table_checkpoints"> + <thead> + <tr> + <th><?php echo __('Date and Time'); ?></th> + <th><?php echo __('Status'); ?></th> + <th><?php echo __('Message'); ?></th> + </tr> + </thead> + <tbody> + <?php foreach ($shipmentData['checkpoints'] as $checkpoint) : ?> + <tr> + <td> + <?php + $timeDate = new DateTime($checkpoint['time']); + echo $timeDate->format('Y M d, H:i'); + ?> + </td> + <td><strong><?php echo $checkpoint['status_friendly']; ?></strong></td> + <td><?php echo $checkpoint['message']; ?></td> + </tr> + <?php endforeach; ?> + </tbody> + </table> + <?php endif; ?> + <?php endif; ?> + <?php endif; ?> + </div> + + <br> + <footer> + <p> + <?php + echo sprintf( + __('For additional information, please contact %s (%s) and quote tracking reference %s.'), + $shipmentData['courier_name'], + $shipmentData['courier_phone'], + $shipmentData['id'] + ); + ?> + </p> + <img id="show_branding" class="image" + src="https://img.bob.co.za/bobgo/image/upload/bobgo-logos/bobgo_logo_smart_shipping_black.png?tr=w-400&ts=20231024" + alt="Bob Go logo" style="width: 200px;"> + </footer> + <?php else : ?> + <br> + <p><?php echo __('No tracking data available.'); ?></p> + <?php endif; ?> +</div>