diff --git a/Block/System/Config/Form/Field/Version.php b/Block/System/Config/Form/Field/Version.php new file mode 100644 index 0000000000000000000000000000000000000000..2ebcbba8b10029a319b41a897bdca308050f8a9e --- /dev/null +++ b/Block/System/Config/Form/Field/Version.php @@ -0,0 +1,66 @@ +<?php + +namespace BobGroup\BobGo\Block\System\Config\Form\Field; + +use Magento\Framework\Data\Form\Element\AbstractElement; +use BobGroup\BobGo\Helper\Data; + +/** + * Displays Version number in System Configuration + * + * This block is responsible for displaying the version number of the BobGo extension + * in the system configuration settings. + * + * @website https://www.bobgo.co.za + */ +class Version extends \Magento\Config\Block\System\Config\Form\Field +{ + /** + * @var string + */ + public const EXTENSION_URL = 'https://www.bobgo.co.za'; + + /** + * @var Data + */ + protected Data $_helper; + + /** + * Constructor + * + * @param \Magento\Backend\Block\Template\Context $context + * @param Data $helper + */ + public function __construct( + \Magento\Backend\Block\Template\Context $context, + Data $helper + ) { + $this->_helper = $helper; + parent::__construct($context); + } + + /** + * Get HTML for the element + * + * @param AbstractElement $element + * @return string + */ + protected function _getElementHtml(AbstractElement $element): string + { + $extensionVersion = $this->_helper->getExtensionVersion(); + $extensionTitle = 'BobGo'; + $versionLabel = sprintf( + '<a href="%s" title="%s" target="_blank">%s</a>', + self::EXTENSION_URL, + $extensionTitle, + $extensionVersion + ); + + // Set the value using setData + $element->setData('value', $versionLabel); + + // Ensure the return value is a string or provide a fallback + $value = $element->getData('value'); + return is_string($value) ? $value : ''; + } +} diff --git a/Block/TrackingBlock.php b/Block/TrackingBlock.php index f77dab086d89a6c33ced6075aabddf3c8ba7e9b9..69068a2c4abf4dd5f57fe145272f83fa2f64350e 100644 --- a/Block/TrackingBlock.php +++ b/Block/TrackingBlock.php @@ -1,18 +1,27 @@ <?php + namespace BobGroup\BobGo\Block; + +use Magento\Framework\View\Element\Template; + class TrackingBlock extends \Magento\Framework\View\Element\Template { - public function __construct( - \Magento\Backend\Block\Template\Context $context, - array $data = [] - ) + protected $response; + + protected function _toHtml() { - parent::__construct($context, $data); + $this->_logger->info('Block HTML rendered: ' . $this->getNameInLayout()); + return parent::_toHtml(); } - public function getHelloWorld() + public function setResponse(array $response): self { - return 'Hello World'; + $this->_response = $response; + return $this; } + public function getResponse() + { + return $this->response; + } } diff --git a/Controller/Tracking/Index.php b/Controller/Tracking/Index.php index 9955e8d89015b9207f76511000c4de88dd193b84..7d073baf4d3dbb9042cb9ae145085c65bc3d8b9d 100644 --- a/Controller/Tracking/Index.php +++ b/Controller/Tracking/Index.php @@ -2,38 +2,142 @@ namespace BobGroup\BobGo\Controller\Tracking; use Psr\Log\LoggerInterface; +use Magento\Framework\App\Action\Context; +use Magento\Framework\View\Result\PageFactory; +use BobGroup\BobGo\Model\Carrier\UData; +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\HTTP\Client\Curl; +use Magento\Store\Model\StoreManagerInterface; class Index extends \Magento\Framework\App\Action\Action { - /** - * @var \Magento\Framework\View\Result\PageFactory - */ protected $resultPageFactory; - + protected $jsonFactory; + protected $curl; protected $logger; + protected StoreManagerInterface $storeManager; - /** - * @param \Magento\Framework\App\Action\Context $context - * @param \Magento\Framework\View\Result\PageFactory resultPageFactory - */ public function __construct( - \Magento\Framework\App\Action\Context $context, + Context $context, + PageFactory $resultPageFactory, + JsonFactory $jsonFactory, LoggerInterface $logger, - \Magento\Framework\View\Result\PageFactory $resultPageFactory - ) - { + StoreManagerInterface $storeManager, + Curl $curl + ) { $this->resultPageFactory = $resultPageFactory; + $this->jsonFactory = $jsonFactory; $this->logger = $logger; + $this->storeManager = $storeManager; + $this->curl = $curl; parent::__construct($context); } - /** - * Default customer account page - * - * @return void - */ + public function execute() { $this->logger->info('Page Controller is executed.'); + $trackingReference = $this->getRequest()->getParam('order_reference'); + + $this->logger->info('Tracking reference:', [$trackingReference]); + + $channel = $this->getStoreUrl(); + + $this->logger->info('Channel:', [$channel]); + +// if ($trackingReference) { +// $trackingUrl = sprintf(UData::TRACKING, $channel, $trackingReference); +// +// try { +// // Make the API call +// $this->curl->get($trackingUrl); +// $response = $this->curl->getBody(); +// +// // Optionally, you can decode the response if it's JSON +// $response = json_decode($response, true); +// +// // Handle the response (e.g., display it on the page) +// $resultJson = $this->jsonFactory->create(); +// return $resultJson->setData($response); +// +// } catch (\Exception $e) { +// $this->messageManager->addErrorMessage(__('Unable to track the order.')); +// } +// } + + if ($trackingReference) { + $trackingUrl = sprintf(UData::TRACKING, $channel, $trackingReference); + + try { + // Make the API call + $this->curl->get($trackingUrl); + $response = $this->curl->getBody(); + + // Decode the response + $this->logger->info('Response:', [$response]); + + $decodedResponse = json_decode($response, true); + $this->logger->info('Decoded Response:', [$decodedResponse]); + + if (is_array($decodedResponse) && isset($decodedResponse[0])) { + $shipmentData = $decodedResponse[0]; + + // Set response in the block + // In your controller action or block: + $layout = $this->_view->getLayout(); + $this->logger->info('Layout Handles: ' . implode(', ', $layout->getUpdate()->getHandles())); + + + $blocks = $layout->getAllBlocks(); + + $blockNames = []; + foreach ($blocks as $block) { + $blockNames[] = $block->getNameInLayout(); + } + + $this->logger->info('Available Blocks:', $blockNames); + + $block = $layout->getBlock('bobgo.tracking.index'); + + $this->logger->info('BobGo block:', [$block]); + + if ($block) { + $block->setResponse($shipmentData); + $this->logger->info('Block found and data set.'); + } else { + $this->logger->info('Block not found.'); + } + } else { + $this->logger->info('Unexpected response format.'); + } + + } catch (\Exception $e) { + $this->logger->info('Track error: ' . $e->getMessage()); + $this->messageManager->addErrorMessage(__('Unable to track the order.')); + } + } + 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/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/etc/frontend/di.xml b/etc/frontend/di.xml index bc1aaebb43890432e80d8e9d27bfd5838b7c8e8b..3814d81d23f359b3853da4117db4940839865a6f 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -13,4 +13,9 @@ <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument> </arguments> </type> + <type name="Magento\Framework\View\Layout"> + <arguments> + <argument name="debug" xsi:type="boolean">true</argument> + </arguments> + </type> </config> diff --git a/view/frontend/layout/customer_account.xml b/view/frontend/layout/customer_account.xml index 54c3c216093c4923d26ef9045871b873be08f1a0..be9a72f764165ba9ca0f301f2ac5643cc1b6d850 100644 --- a/view/frontend/layout/customer_account.xml +++ b/view/frontend/layout/customer_account.xml @@ -6,7 +6,7 @@ <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-tracking"> <arguments> <argument name="path" xsi:type="string">bobgo/tracking/index</argument> - <argument name="label" xsi:type="string">Tracking</argument> + <argument name="label" xsi:type="string">Track my order</argument> </arguments> </block> </referenceBlock> diff --git a/view/frontend/templates/tracking/index.phtml b/view/frontend/templates/tracking/index.phtml index 8c81e6b2d4a48d5fb34a18ca77a1afd1e1edd602..f6b8ef81ec96051eb32a6c5f7c553b37d48ceaac 100644 --- a/view/frontend/templates/tracking/index.phtml +++ b/view/frontend/templates/tracking/index.phtml @@ -1,5 +1,76 @@ -<h2> - <?php echo $block->getHelloWorld(); ?> -</h2> +<style> + .track-order-container { + max-width: 600px; + margin: auto; + padding: 20px; + border: 1px solid #ddd; + background-color: #f9f9f9; + } -echo 'My First Link Page'; + .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"> + <form action="<?php echo $this->getUrl('bobgo/tracking/index'); ?>" method="post"> + <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 //if ($response = $block->getResponse()): ?> +<!-- <div class="tracking-response">--> +<!-- <!-- Handle and display the API response -->--> +<!-- <h2>--><?php //echo __('Tracking Information'); ?><!--</h2>--> +<!-- <p>--><?php //echo __('Status: ' . $response['status']); ?><!--</p>--> +<!-- <!-- Add more fields as needed from the response -->--> +<!-- </div>--> +<!-- --><?php //endif; ?> + + <?php + +//<!-- --><?php +// $shipmentData = $block->getResponse(); +// if ($shipmentData && is_array($shipmentData)) { +// foreach ($shipmentData as $shipment) { +// echo '<div class="tracking-details">'; +// echo '<h2>' . __('Shipping Details') . '</h2>'; +// echo '<p>' . __('Shipment: ') . $shipment['shipment_tracking_reference'] . '</p>'; +// echo '<p>' . __('Order: ') . ltrim($shipment['order_number'], '0') . '</p>'; +// echo '<p>' . __('Courier: ') . $shipment['courier_name'] . '</p>'; +// +// echo '<h2>' . __('Tracking Details') . '</h2>'; +// echo '<p>' . __('Current Status: ') . $shipment['status_friendly'] . '</p>'; +// +// echo '<footer>'; +// echo '<p>' . __('For additional information, please contact BobGo (support@bobgo.co.za).') . '</p>'; +// echo '<img src="' . $shipment['courier_logo'] . '" alt="Courier Logo" style="width: 100px;"/>'; +// echo '</footer>'; +// echo '</div>'; +// } +// } else { +// echo '<p>No tracking data available.</p>'; +// } +// ?> +</div> diff --git a/view/frontend/web/images/logo.png b/view/frontend/web/images/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..a1214967803c946558511b30e91ac9600543a707 Binary files /dev/null and b/view/frontend/web/images/logo.png differ