Skip to content

Commit

Permalink
Code refactor and update admin custom price logic
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Aug 24, 2018
1 parent 4c17841 commit 9ca271b
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 142 deletions.
14 changes: 8 additions & 6 deletions Block/Adminhtml/Order/Create/Shipping/Method/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace MagePal\CustomShippingRate\Block\Adminhtml\Order\Create\Shipping\Method;

use MagePal\CustomShippingRate\Model\Carrier;

class Form extends \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form
{
protected $activeMethodRate;
Expand All @@ -19,7 +21,7 @@ class Form extends \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\F
public function getActiveCustomShippingRateMethod()
{
$rate = $this->getActiveMethodRate();
return $rate && $rate->getCarrier() == \MagePal\CustomShippingRate\Model\Carrier::CODE ? $rate->getMethod() : '';
return $rate && $rate->getCarrier() == Carrier::CODE ? $rate->getMethod() : '';
}

/**
Expand All @@ -44,7 +46,7 @@ public function isCustomShippingRateActive()
$this->activeMethodRate = $this->getActiveMethodRate();
}

return $this->activeMethodRate && $this->activeMethodRate->getCarrier() == \MagePal\CustomShippingRate\Model\Carrier::CODE ? true : false;
return $this->activeMethodRate && $this->activeMethodRate->getCarrier() == Carrier::CODE ? true : false;
}

/**
Expand All @@ -56,14 +58,14 @@ public function getGroupShippingRates()
{
$rates = $this->getShippingRates();

if (array_key_exists(\MagePal\CustomShippingRate\Model\Carrier::CODE, $rates)) {
if (array_key_exists(Carrier::CODE, $rates)) {
if (!$this->isCustomShippingRateActive()) {
unset($rates[\MagePal\CustomShippingRate\Model\Carrier::CODE]);
unset($rates[Carrier::CODE]);
} else {
$activeRateMethod = $this->getActiveCustomShippingRateMethod();
foreach ($rates[\MagePal\CustomShippingRate\Model\Carrier::CODE] as $key => $rate) {
foreach ($rates[Carrier::CODE] as $key => $rate) {
if ($rate->getMethod() != $activeRateMethod) {
unset($rates[\MagePal\CustomShippingRate\Model\Carrier::CODE][$key]);
unset($rates[Carrier::CODE][$key]);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Block/Adminhtml/System/Config/Form/Field/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
return sprintf(
'<a href ="%s">%s</a>',
$this->_urlBuilder->getUrl('adminhtml/system_config/edit/section/carriers'),
__('Store > Configuration > Shipping Methods > Custom Shipping Rate')
'<a href ="%s#carriers_customshippingrate-link">%s</a>',
$this->_urlBuilder->getUrl('adminhtml/system_config/edit/section/carriers'),
__('Store > Configuration > Shipping Methods > Custom Shipping Rate')
);
}
}
88 changes: 60 additions & 28 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@

namespace MagePal\CustomShippingRate\Helper;

use Magento\Store\Model\ScopeInterface;
use MagePal\CustomShippingRate\Model\Carrier;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var array
*/
protected $shippingType;

/**
* @var array
*/
protected $codes = [
'code' => [
'label' => 'Code',
Expand All @@ -32,63 +43,81 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
]
];

/**
* @var array
*/
protected $headerTemplate;

/**
* @return array|mixed
*/
public function getShippingType()
{
$arrayValues = [];
$configData = $this->getConfigData('shipping_type');

if (is_string($configData) && !empty($configData) && $configData !== '[]') {
if ($this->isJson($configData)) {
$arrayValues = (array) json_decode($configData, true);
} else {
$arrayValues = (array) array_values(unserialize($configData));
if (!$this->shippingType) {
$arrayValues = [];
$configData = $this->getConfigData('shipping_type');

if (is_string($configData) && !empty($configData) && $configData !== '[]') {
if ($this->isJson($configData)) {
$arrayValues = (array) json_decode($configData, true);
} else {
$arrayValues = (array) array_values(unserialize($configData));
}
}

$arrayValues = $this->shippingArrayObject($arrayValues);

usort($arrayValues, function ($a, $b) {
if (array_key_exists('sort_order', $a)) {
return $a['sort_order'] - $b['sort_order'];
} else {
return 0;
}
});

$this->shippingType = $arrayValues;
}

$arrayValues = $this->shippingArrayObject($arrayValues);
return $this->shippingType;
}

usort($arrayValues, function ($a, $b) {
if (array_key_exists('sort_order', $a)) {
return $a['sort_order'] - $b['sort_order'];
} else {
return 0;
/**
* input {code}_{method}
* return method
* @param $method_code
* @return string
*/
public function getShippingCodeFromMethod($method_code)
{
foreach ($this->getShippingType() as $shippingType) {
if (Carrier::CODE . '_' . $shippingType['code'] == $method_code) {
return $shippingType['code'];
break;
}
});
}

return $arrayValues;
return '';
}

/**
* @return bool
*/
public function isEnabled()
{
return (bool)$this->getConfigData('active');
return $this->scopeConfig->isSetFlag('carriers/' . Carrier::CODE . '/active');
}

/**
* Retrieve information from carrier configuration
*
* @param string $field
* @return void|false|string
* @return string
*/
public function getConfigData($field)
{
$code = \MagePal\CustomShippingRate\Model\Carrier::CODE;
if (empty($code)) {
return false;
}

$path = 'carriers/' . $code . '/' . $field;

return $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
'carriers/' . Carrier::CODE . '/' . $field,
ScopeInterface::SCOPE_STORE
);
}

Expand All @@ -104,6 +133,9 @@ public function isJson($string)
return (json_last_error() == JSON_ERROR_NONE);
}

/**
* @return array
*/
public function getHeaderTemplate()
{
if (!$this->headerTemplate) {
Expand Down Expand Up @@ -135,7 +167,7 @@ public function shippingArrayObject($values)
$requiredFields = $this->getHeaderTemplate();

if (is_array($values)) {
foreach ($values as $key => &$row) {
foreach ($values as &$row) {
$row = array_merge($requiredFields, $row);
}
}
Expand Down
82 changes: 56 additions & 26 deletions Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@

namespace MagePal\CustomShippingRate\Model;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Shipping\Helper\Carrier as ShippingCarrierHelper;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Rate\ResultFactory;
use MagePal\CustomShippingRate\Helper\Data;
use Psr\Log\LoggerInterface;

class Carrier extends AbstractCarrier implements \Magento\Shipping\Model\Carrier\CarrierInterface
class Carrier extends AbstractCarrier implements CarrierInterface
{
/**
* Code of the carrier
Expand All @@ -28,14 +38,14 @@ class Carrier extends AbstractCarrier implements \Magento\Shipping\Model\Carrier

/**
*
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
* @var MethodFactory
*/
protected $_rateMethodFactory;

/**
* Carrier helper
*
* @var \Magento\Shipping\Helper\Carrier
* @var ShippingCarrierHelper
*/
protected $_carrierHelper;

Expand All @@ -45,35 +55,36 @@ class Carrier extends AbstractCarrier implements \Magento\Shipping\Model\Carrier
protected $_rateFactory;

/**
* @var \Magento\Framework\App\State
* @var State
*/
protected $_state;

/**
* @var \MagePal\CustomShippingRate\Helper\Data
* @var Data
*/
protected $_customShippingRateHelper;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param ScopeConfigInterface $scopeConfig
* @param ErrorFactory $rateErrorFactory
* @param LoggerInterface $logger
* @param ResultFactory $rateFactory
* @param ShippingCarrierHelper $carrierHelper
* @param MethodFactory $rateMethodFactory
* @param State $state
* @param Data $customShippingRateHelper
* @param array $data
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateFactory
* @param \Magento\Shipping\Helper\Carrier $carrierHelper
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
\Magento\Shipping\Helper\Carrier $carrierHelper,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
\Magento\Framework\App\State $state,
\MagePal\CustomShippingRate\Helper\Data $customShippingRateHelper,
ScopeConfigInterface $scopeConfig,
ErrorFactory $rateErrorFactory,
LoggerInterface $logger,
ResultFactory $rateFactory,
ShippingCarrierHelper $carrierHelper,
MethodFactory $rateMethodFactory,
State $state,
Data $customShippingRateHelper,
array $data = []
) {
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
Expand All @@ -91,13 +102,14 @@ public function __construct(
* Collect and get rates
*
* @param RateRequest $request
* @return \Magento\Quote\Model\Quote\Address\RateResult\Error|bool|Result
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection|\Magento\Shipping\Model\Rate\Result
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function collectRates(RateRequest $request)
{
$result = $this->_rateFactory->create();

if (!$this->getConfigFlag('active') || ($this->_state->getAreaCode() != \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE && !$this->getConfigFlag('show_on_frontend'))) {
if (!$this->getConfigFlag('active') || (!$this->isAdmin() && $this->hideShippingMethodOnFrontend())) {
return $result;
}

Expand All @@ -117,10 +129,10 @@ public function collectRates(RateRequest $request)
}

/**
* Get allowed shipping methods
*
* @return array
*/
* Get allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
return [$this->getCarrierCode() => __($this->getConfigData('name'))];
Expand All @@ -130,4 +142,22 @@ public function isTrackingAvailable()
{
return false;
}

/**
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function hideShippingMethodOnFrontend()
{
return !$this->getConfigFlag('show_on_frontend');
}

/**
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function isAdmin()
{
return $this->_state->getAreaCode() == FrontNameResolver::AREA_CODE;
}
}
Loading

0 comments on commit 9ca271b

Please sign in to comment.