From c6f8cc1c7893557e1834b814a31548d1a7f3aee6 Mon Sep 17 00:00:00 2001 From: Gundo Sifhufhi <sifhufhisg@gmail.com> Date: Tue, 20 Dec 2022 15:01:54 +0200 Subject: [PATCH] Add plugin to dropdown carriers in admin to enable :Shipping an order should allow the user to select "uAfrica.com" and specify the tracking reference. --- .../Model/Carrier/Customshipping.php | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/uafrica/Customshipping/Model/Carrier/Customshipping.php b/uafrica/Customshipping/Model/Carrier/Customshipping.php index f6eea26..4017aab 100644 --- a/uafrica/Customshipping/Model/Carrier/Customshipping.php +++ b/uafrica/Customshipping/Model/Carrier/Customshipping.php @@ -14,6 +14,12 @@ use Magento\Quote\Model\Quote\Address\RateResult\Method; use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Magento\Quote\Model\Quote\Address\RateRequest; use Psr\Log\LoggerInterface; +use Magento\Framework\App\ActionInterface; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\HTTP\Client\Curl; +use Magento\Framework\HTTP\Client\CurlFactory; /** * @category uafrica @@ -47,6 +53,23 @@ class Customshipping extends AbstractCarrier implements CarrierInterface */ protected $_rateMethodFactory; + + + //TODO: REFACTO THIS + /** + * @var \Magento\Framework\Controller\Result\JsonFactory + */ + protected $jsonFactory; + + /** + * @var \Magento\Framework\HTTP\Client\Curl + */ + protected $curl; + + /** + * @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory + */ + /** * @param ScopeConfigInterface $scopeConfig * @param ErrorFactory $rateErrorFactory @@ -61,8 +84,12 @@ class Customshipping extends AbstractCarrier implements CarrierInterface LoggerInterface $logger, ResultFactory $rateResultFactory, MethodFactory $rateMethodFactory, + JsonFactory $jsonFactory, + CurlFactory $curlFactory, array $data = [] ) { + $this->jsonFactory = $jsonFactory; + $this->curl = $curlFactory->create(); $this->_rateResultFactory = $rateResultFactory; $this->_rateMethodFactory = $rateMethodFactory; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); @@ -75,10 +102,49 @@ class Customshipping extends AbstractCarrier implements CarrierInterface * @return array * @api */ - public function getAllowedMethods() + public function getAllowedMethods(): array { return [$this->getCarrierCode() => __($this->getConfigData('name'))]; } + /** + * Allow Display of uAfrica/Bobgo in the list of carriers (Admin) + * @return bool + */ + public function isTrackingAvailable(): bool + { + return true; + } + +// /** +// * Obtain Tracking Information from uAfrica/Bobgo +// * @param string $trackings +// */ +// public function getTrackingInfo($trackings) +// { +// // $result = $this->_trackFactory->create(); +// // $tracking = explode(',', $trackings); +// // Get Tracking Results From uafrica API +// $this->curl->get("https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=UADPCTGF"); +// +// $result = $this->curl->getBody(); +// +// $bobGo = \Safe\json_decode($result, true); +//// $result->setUrl('https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=UADPCTGF'); +//// $result->setTracking($trackings); +//// $result->setCarrierTitle($this->getConfigData('title')); +// $track = $this->jsonFactory->create()->setData($bobGo); +// +// return $track; +// } +// /** +// * Get tracking information +// * +// */ +// +// public function getTracking() +// { +// return $this->getTrackingInfo(); +// } /** * Collect and get rates for storefront -- GitLab