add method to shipping method attributes and better approach for checking if shipping method is available

This commit is contained in:
AmooAti 2021-11-08 11:15:12 +03:30
parent 44cb03e30c
commit 154808b181
5 changed files with 31 additions and 26 deletions

View File

@ -446,16 +446,7 @@ class Cart
return false;
}
$shippingMethods = Shipping::collectRates()['shippingMethods'];
$isMethodAvailable = false;
foreach ($shippingMethods as $shippingMethod) {
foreach ($shippingMethod['rates'] as $rate)
if ($rate->method == $shippingMethodCode) {
$isMethodAvailable = true;
break 2;
}
}
if (!$isMethodAvailable) {
if (!array_key_exists($shippingMethodCode, Shipping::getShippingMethods())) {
return false;
}

View File

@ -2,14 +2,15 @@
namespace Webkul\Shipping\Carriers;
use Illuminate\Support\Facades\Config;
abstract class AbstractShipping
{
protected $code;
protected $method;
abstract public function calculate();
/**
* Checks if payment method is available
* Checks if shipping method is available
*
* @return array
*/
@ -19,7 +20,7 @@ abstract class AbstractShipping
}
/**
* Returns payment method code
* Returns shipping method code
*
* @return array
*/
@ -32,8 +33,17 @@ abstract class AbstractShipping
return $this->code;
}
public function getMethod()
{
if (empty($this->method)) {
// throw exception
}
return $this->method;
}
/**
* Returns payment method title
* Returns shipping method title
*
* @return array
*/
@ -43,7 +53,7 @@ abstract class AbstractShipping
}
/**
* Returns payment method decription
* Returns shipping method description
*
* @return array
*/
@ -53,10 +63,9 @@ abstract class AbstractShipping
}
/**
* Retrieve information from payment configuration
* Retrieve information from shipping configuration
*
* @param string $field
* @param int|string|null $channelId
* @return mixed
*/
public function getConfigData($field)

View File

@ -14,12 +14,14 @@ use Webkul\Checkout\Facades\Cart;
class FlatRate extends AbstractShipping
{
/**
* Payment method code
* Shipping method code
*
* @var string
*/
protected $code = 'flatrate';
protected $method = 'flatrate_flatrate';
/**
* Returns rate for flatrate
*
@ -37,7 +39,7 @@ class FlatRate extends AbstractShipping
$object->carrier = 'flatrate';
$object->carrier_title = $this->getConfigData('title');
$object->method = 'flatrate_flatrate';
$object->method = $this->method;
$object->method_title = $this->getConfigData('title');
$object->method_description = $this->getConfigData('description');
$object->is_calculate_tax = $this->getConfigData('is_calculate_tax');

View File

@ -13,12 +13,14 @@ use Webkul\Shipping\Facades\Shipping;
class Free extends AbstractShipping
{
/**
* Payment method code
* Shipping method code
*
* @var string
*/
protected $code = 'free';
protected $method = 'free_free';
/**
* Returns rate for flatrate
*

View File

@ -130,8 +130,9 @@ class Shipping
continue;
}
$methods[] = [
'method' => $object->getCode(),
$methods[$object->getMethod()] = [
'code' => $object->getCode(),
'method' => $object->getMethod(),
'method_title' => $object->getTitle(),
'description' => $object->getDescription()
];