Skip to content

Commit

Permalink
Add better multi-store support
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Feb 27, 2021
1 parent 144c3b0 commit 4cf5807
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 293 deletions.
126 changes: 0 additions & 126 deletions Block/Adminhtml/System/Config/Form/Composer/Version.php

This file was deleted.

77 changes: 0 additions & 77 deletions Block/Adminhtml/System/Config/Form/Module/Version.php

This file was deleted.

34 changes: 20 additions & 14 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
use Magento\Store\Model\ScopeInterface;
use MagePal\CustomShippingRate\Model\Carrier;

/**
* Class Data
* @package MagePal\CustomShippingRate\Helper
*/
class Data extends AbstractHelper
{
/**
Expand Down Expand Up @@ -54,13 +50,14 @@ class Data extends AbstractHelper
protected $headerTemplate;

/**
* @param null $storeId
* @return array|mixed
*/
public function getShippingType()
public function getShippingType($storeId = null)
{
if (!$this->shippingType) {
$arrayValues = [];
$configData = $this->getConfigData('shipping_type');
$configData = $this->getConfigData('shipping_type', $storeId);

if (is_string($configData) && !empty($configData) && $configData !== '[]') {
if ($this->isJson($configData)) {
Expand Down Expand Up @@ -90,18 +87,21 @@ public function getShippingType()
* input {code}_{method}
* return method
* @param $method_code
* @param null $storeId
* @return string
*/
public function getShippingCodeFromMethod($method_code)
public function getShippingCodeFromMethod($method_code, $storeId = null)
{
foreach ($this->getShippingType() as $shippingType) {
$result = '';

foreach ($this->getShippingType($storeId) as $shippingType) {
if (Carrier::CODE . '_' . $shippingType['code'] == $method_code) {
return $shippingType['code'];
$result = $shippingType['code'];
break;
}
}

return '';
return $result;
}

/**
Expand All @@ -110,20 +110,26 @@ public function getShippingCodeFromMethod($method_code)
*/
public function isEnabled($storeId = null)
{
return $this->scopeConfig->isSetFlag('carriers/' . Carrier::CODE . '/active', ScopeInterface::SCOPE_STORE, $storeId);
return $this->scopeConfig->isSetFlag(
'carriers/' . Carrier::CODE . '/active',
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Retrieve information from carrier configuration
*
* @param string $field
* @param string $field
* @param null $storeId
* @return string
*/
public function getConfigData($field)
public function getConfigData($field, $storeId = null)
{
return $this->scopeConfig->getValue(
'carriers/' . Carrier::CODE . '/' . $field,
ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE,
$storeId
);
}

Expand Down
6 changes: 1 addition & 5 deletions Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
use MagePal\CustomShippingRate\Helper\Data;
use Psr\Log\LoggerInterface;

/**
* Class Carrier
* @package MagePal\CustomShippingRate\Model
*/
class Carrier extends AbstractCarrier implements CarrierInterface
{
/**
Expand Down Expand Up @@ -121,7 +117,7 @@ public function collectRates(RateRequest $request)
return $result;
}

foreach ($this->_customShippingRateHelper->getShippingType() as $shippingType) {
foreach ($this->_customShippingRateHelper->getShippingType($request->getStoreId()) as $shippingType) {
$rate = $this->_rateMethodFactory->create();
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
Expand Down
4 changes: 0 additions & 4 deletions Plugin/Model/Quote/AddressPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

use Magento\Quote\Api\Data\AddressInterface;

/**
* Class AddressPlugin
* @package MagePal\CustomShippingRate\Plugin\Model\Quote
*/
class AddressPlugin
{

Expand Down
14 changes: 6 additions & 8 deletions Plugin/Quote/Address/Total/ShippingPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
use MagePal\CustomShippingRate\Helper\Data;
use MagePal\CustomShippingRate\Model\Carrier;

/**
* Class ShippingPlugin
* @package MagePal\CustomShippingRate\Plugin\Quote\Address\Total
*/
class ShippingPlugin
{
/**
Expand Down Expand Up @@ -54,15 +50,16 @@ public function aroundCollect(
$shipping = $shippingAssignment->getShipping();
$address = $shipping->getAddress();
$method = $address->getShippingMethod();
$storeId = $quote->getStoreId();

if (!$this->customShippingRateHelper->isEnabled($quote->getStore()->getStoreId())
if (!$this->customShippingRateHelper->isEnabled($storeId)
|| $address->getAddressType() != Address::ADDRESS_TYPE_SHIPPING
|| strpos($method, Carrier::CODE) === false
) {
return $proceed($quote, $shippingAssignment, $total);
}

$customShippingOption = $this->getCustomShippingJsonToArray($method, $address);
$customShippingOption = $this->getCustomShippingJsonToArray($method, $address, $storeId);

if ($customShippingOption && strpos($method, $customShippingOption['code']) !== false) {
//update shipping code
Expand Down Expand Up @@ -96,9 +93,10 @@ protected function updateCustomRate($address, $customShippingOption)
/**
* @param $json
* @param $address
* @param null $storeId
* @return array|bool
*/
private function getCustomShippingJsonToArray($json, $address)
private function getCustomShippingJsonToArray($json, $address, $storeId = null)
{
$isJson = $this->customShippingRateHelper->isJson($json);

Expand All @@ -111,7 +109,7 @@ private function getCustomShippingJsonToArray($json, $address)

$jsonToArray = [
'code' => $json,
'type' => $this->customShippingRateHelper->getShippingCodeFromMethod($json),
'type' => $this->customShippingRateHelper->getShippingCodeFromMethod($json, $storeId),
'rate' => $rate
];

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
[![Total Downloads](https://poser.pugx.org/magepal/magento2-customshippingrate/downloads)](https://packagist.org/packages/magepal/magento2-customshippingrate)
[![Latest Stable Version](https://poser.pugx.org/magepal/magento2-customshippingrate/v/stable)](https://packagist.org/packages/magepal/magento2-customshippingrate)

<a href="https://www.magepal.com/magento2/extensions/shipping.html" ><img alt="Magento Shipping Extensions" src="https://user-images.githubusercontent.com/1415141/109374323-86591a00-7882-11eb-82c1-b5bb58ca77fe.png" /></a>

As a Magento merchant completing with other major brands, providing simple flat rate shipping is essential to your business success. Our Magento 2 custom flat shipping method extension adds two essential functionality to your Magento store which gives you fully take control your store shipping options by providing the ability to quickly add custom flat fee shipping rates to both admin order creation or display a simple list of flat rates to your website customers. So whether you are using United States Postal Services, United Parcel Service, Federal Express, DHL or any other carriers, our Magento2 shipping extension makes it easy for you to quickly add simple flat rate shipping fees to your Magento site.


### Custom Shipping Rate for Admin Order
Whether you are creating a new order or canceling and rewriting existing orders in Magento Admin, our admin shipping extension gives you the ability to apply a custom shipping rate, method, and description to any order. This free extension is essential for businesses that do a lot of phone orders or mail orders and want to offer special shipping cost for individual customers. With our admin shipping plugin extension changing shipping amount for a particular order is as easy as entering the shipping amount instead of choosing predefined standard shipping rates and invoice your customer as you would with any standard shipping rate.

Expand Down
Loading

0 comments on commit 4cf5807

Please sign in to comment.