Skip to content

Commit

Permalink
Merge pull request #34 from contardi/master
Browse files Browse the repository at this point in the history
Fix: added possibility to select intelipost on tracking options
  • Loading branch information
fgmarchione authored Apr 25, 2024
2 parents 2295a94 + affd42e commit 83ad6a0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
46 changes: 37 additions & 9 deletions Model/Carrier/Intelipost.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Shipping\Model\Tracking\Result as TrackingResult;
use Magento\Shipping\Model\Tracking\Result\Error as TrackingError;
use Magento\Shipping\Model\Tracking\Result\ErrorFactory as TrackErrorFactory;
use Magento\Shipping\Model\Tracking\Result\StatusFactory as TrackStatusFactory;
use Magento\Shipping\Model\Tracking\ResultFactory as TrackResultFactory;

class Intelipost extends AbstractCarrier implements CarrierInterface
{
Expand Down Expand Up @@ -46,31 +51,39 @@ class Intelipost extends AbstractCarrier implements CarrierInterface
/** @var ProductRepository */
protected $productRepository;

/** @var TrackStatusFactory */
protected $trackStatusFactory;

/**
* @var TrackResultFactory
*/
protected $trackResultFactory;

/**
* @param ScopeConfigInterface $scopeConfig
* @param ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param ResultFactory $rateResultFactory
* @param MethodFactory $rateMethodFactory
* @param Data $helper
* @param Api $api
* @param ProductRepository $productRespository
* @param array $data
* @var TrackErrorFactory
*/
protected $trackErrorFactory;

public function __construct(
ScopeConfigInterface $scopeConfig,
ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
ResultFactory $rateResultFactory,
MethodFactory $rateMethodFactory,
ProductRepository $productRespository,
TrackStatusFactory $trackStatusFactory,
TrackResultFactory $trackResultFactory,
TrackErrorFactory $trackErrorFactory,
Api $api,
Data $helper,
array $data = []
) {
$this->rateResultFactory = $rateResultFactory;
$this->rateMethodFactory = $rateMethodFactory;
$this->productRepository = $productRespository;
$this->trackStatusFactory = $trackStatusFactory;
$this->trackResultFactory = $trackResultFactory;
$this->trackErrorFactory = $trackErrorFactory;
$this->scopeConfig = $scopeConfig;
$this->helper = $helper;
$this->api = $api;
Expand Down Expand Up @@ -440,4 +453,19 @@ public function getVolumes($response, $cartQty)

return $volumes;
}

/**
* Check if carrier has shipping tracking option available
*
* @return bool
*/
public function isTrackingAvailable()
{
return true;
}

public function getTrackingInfo(string $trackingNumber): array
{
return ['number' => $trackingNumber];
}
}
30 changes: 23 additions & 7 deletions Plugin/Tracking/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Intelipost\Shipping\Helper\Data;
use Magento\Shipping\Model\InfoFactory;
use Magento\Shipping\Model\Tracking\Result;
use Magento\Shipping\Model\Tracking\Result\AbstractResult;

class Popup
{
Expand Down Expand Up @@ -45,18 +47,32 @@ public function aroundExecute(\Magento\Shipping\Controller\Tracking\Popup $subje
$hash = $subject->getRequest()->getParam('hash');
$shippingInfoModel = $this->shippingInfoFactory->create()->loadByHash($hash);
$trackingInfo = $shippingInfoModel->getTrackingInfo();
if (count($trackingInfo) == 1) {
$tracking = reset($trackingInfo);
if (isset($tracking[0]) && count($tracking) == 1) {
if (filter_var($tracking[0]['number'], FILTER_VALIDATE_URL) !== false) {
return $subject->getResponse()->setRedirect($tracking[0]['number']);
}

$trackingNumber = $this->getTrackingNumber($trackingInfo);
if ($trackingNumber) {
if (filter_var($trackingNumber, FILTER_VALIDATE_URL) !== false) {
$subject->getResponse()->setRedirect($trackingNumber);
return;
}
}
} catch (\Exception $e) {
$this->helper->getLogger()->error($e->getMessage());
}

return $proceed();
$proceed();
}

protected function getTrackingNumber(array $trackingInfo): string
{
$trackingNumber = '';
foreach ($trackingInfo as $tracking) {
if (isset($tracking[0])) {
$trackingItem = $tracking[0];
if (is_array($trackingItem)) {
return $trackingItem['number'];
}
}
}
return $trackingNumber;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "intelipost/magento2",
"description": "Intelipost Shipping",
"type": "magento2-module",
"version": "2.7.4",
"version": "2.7.5",
"require": {
"guzzlehttp/guzzle": ">=6.3.3"
},
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
-->
<config>
<module name="Intelipost_Shipping" setup_version="2.7.4">
<module name="Intelipost_Shipping" setup_version="2.7.5">
<sequence>
<module name="Magento_Quote"/>
</sequence>
Expand Down

0 comments on commit 83ad6a0

Please sign in to comment.