diff --git a/Block/TrackingBlock.php b/Block/TrackingBlock.php index 69068a2c4abf4dd5f57fe145272f83fa2f64350e..647cb3dbec3f6b0e662c5b75389566ab5e77894e 100644 --- a/Block/TrackingBlock.php +++ b/Block/TrackingBlock.php @@ -3,25 +3,24 @@ namespace BobGroup\BobGo\Block; use Magento\Framework\View\Element\Template; +use Magento\Framework\Registry; class TrackingBlock extends \Magento\Framework\View\Element\Template { - protected $response; + protected $registry; - protected function _toHtml() - { - $this->_logger->info('Block HTML rendered: ' . $this->getNameInLayout()); - return parent::_toHtml(); - } - - public function setResponse(array $response): self - { - $this->_response = $response; - return $this; + public function __construct( + Template\Context $context, + Registry $registry, + array $data = [] + ) { + $this->registry = $registry; + parent::__construct($context, $data); } public function getResponse() { - return $this->response; + return $this->registry->registry('shipment_data'); } } + diff --git a/Controller/Tracking/Index.php b/Controller/Tracking/Index.php index 7d073baf4d3dbb9042cb9ae145085c65bc3d8b9d..0fc445bd64f66f3fadd8261ea53ccc2e3330fe37 100644 --- a/Controller/Tracking/Index.php +++ b/Controller/Tracking/Index.php @@ -1,13 +1,14 @@ <?php 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\Registry; +use Psr\Log\LoggerInterface; 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 { @@ -15,6 +16,7 @@ class Index extends \Magento\Framework\App\Action\Action protected $jsonFactory; protected $curl; protected $logger; + protected $registry; protected StoreManagerInterface $storeManager; public function __construct( @@ -23,13 +25,15 @@ class Index extends \Magento\Framework\App\Action\Action JsonFactory $jsonFactory, LoggerInterface $logger, StoreManagerInterface $storeManager, - Curl $curl + Curl $curl, + Registry $registry // Add Registry ) { $this->resultPageFactory = $resultPageFactory; $this->jsonFactory = $jsonFactory; $this->logger = $logger; $this->storeManager = $storeManager; $this->curl = $curl; + $this->registry = $registry; // Assign Registry parent::__construct($context); } @@ -44,35 +48,13 @@ class Index extends \Magento\Framework\App\Action\Action $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); @@ -81,31 +63,10 @@ class Index extends \Magento\Framework\App\Action\Action 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]); + // Save data to the registry + $this->registry->register('shipment_data', $shipmentData); + $this->logger->info('Shipment data registered in the registry.'); - 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.'); } diff --git a/view/frontend/templates/tracking/index.phtml b/view/frontend/templates/tracking/index.phtml index f6b8ef81ec96051eb32a6c5f7c553b37d48ceaac..f6e2b2325e110ceb527bce939e8614dd4120c6ff 100644 --- a/view/frontend/templates/tracking/index.phtml +++ b/view/frontend/templates/tracking/index.phtml @@ -39,38 +39,44 @@ </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>'; -// } -// ?> + <?php + $shipmentData = $this->getResponse(); + + if ($shipmentData && is_array($shipmentData)) { + echo '<div class="tracking-details">'; + echo '<h2>' . __('Shipping Details') . '</h2>'; + + // Ensure each accessed array key exists and contains an array where expected + if (isset($shipmentData['shipment_tracking_reference'])) { + echo '<p>' . __('Shipment: ') . $shipmentData['shipment_tracking_reference'] . '</p>'; + } + + if (isset($shipmentData['order_number'])) { + echo '<p>' . __('Order: ') . ltrim($shipmentData['order_number'], '0') . '</p>'; + } + + if (isset($shipmentData['courier_name'])) { + echo '<p>' . __('Courier: ') . $shipmentData['courier_name'] . '</p>'; + } + + echo '<h2>' . __('Tracking Details') . '</h2>'; + + if (isset($shipmentData['status_friendly'])) { + echo '<p>' . __('Current Status: ') . $shipmentData['status_friendly'] . '</p>'; + } + + if (isset($shipmentData['courier_logo'])) { + echo '<footer>'; + echo '<p>' . __('For additional information, please contact BobGo (support@bobgo.co.za).') . '</p>'; + echo '<img src="' . $shipmentData['courier_logo'] . '" alt="Courier Logo" style="width: 100px;"/>'; + echo '</footer>'; + } + + echo '</div>'; + } else { + echo '<p>No tracking data available.</p>'; + } + ?> + + </div>