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

tracking payload data being displayed

parent d5dc33c1
Branches
Tags
2 merge requests!201.0.41,!15Tracking page
......@@ -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');
}
}
<?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();
// Save data to the registry
$this->registry->register('shipment_data', $shipmentData);
$this->logger->info('Shipment data registered in the registry.');
$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.');
}
......
......@@ -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
$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>';
}
?>
//<!-- --><?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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment