From ce78fe39eca1e219716eee9ed333734de4550a52 Mon Sep 17 00:00:00 2001 From: AmooAti Date: Sun, 7 Nov 2021 12:36:45 +0330 Subject: [PATCH 1/5] add shipping method available validation to saveShippingMethod() --- packages/Webkul/Checkout/src/Cart.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 73042aa0e..bd2c3e451 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -446,6 +446,17 @@ class Cart return false; } + $shippingMethods = Shipping::getShippingMethods(); + $isMethodAvailable = false; + foreach ($shippingMethods as $shippingMethod) { + if ($shippingMethod['method'] == $shippingMethodCode) { + $isMethodAvailable = true; + } + } + if (!$isMethodAvailable) { + return false; + } + $cart->shipping_method = $shippingMethodCode; $cart->save(); From 58ca0b2627d3311dc107716e9194e25ae00bdd44 Mon Sep 17 00:00:00 2001 From: AmooAti Date: Sun, 7 Nov 2021 12:39:46 +0330 Subject: [PATCH 2/5] better foreach :D --- packages/Webkul/Checkout/src/Cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index bd2c3e451..d3ad7533c 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -451,6 +451,7 @@ class Cart foreach ($shippingMethods as $shippingMethod) { if ($shippingMethod['method'] == $shippingMethodCode) { $isMethodAvailable = true; + break; } } if (!$isMethodAvailable) { From 44cb03e30c9875dabf6378651aed9482cc20ed3a Mon Sep 17 00:00:00 2001 From: AmooAti Date: Sun, 7 Nov 2021 12:58:49 +0330 Subject: [PATCH 3/5] changed to propper way of checking to pass unit test --- packages/Webkul/Checkout/src/Cart.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index d3ad7533c..182a407d2 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -446,12 +446,13 @@ class Cart return false; } - $shippingMethods = Shipping::getShippingMethods(); + $shippingMethods = Shipping::collectRates()['shippingMethods']; $isMethodAvailable = false; foreach ($shippingMethods as $shippingMethod) { - if ($shippingMethod['method'] == $shippingMethodCode) { + foreach ($shippingMethod['rates'] as $rate) + if ($rate->method == $shippingMethodCode) { $isMethodAvailable = true; - break; + break 2; } } if (!$isMethodAvailable) { From 154808b1815999e4d2b5f5c2b18cad1dcc42b29e Mon Sep 17 00:00:00 2001 From: AmooAti Date: Mon, 8 Nov 2021 11:15:12 +0330 Subject: [PATCH 4/5] add method to shipping method attributes and better approach for checking if shipping method is available --- packages/Webkul/Checkout/src/Cart.php | 11 +------- .../src/Carriers/AbstractShipping.php | 27 ++++++++++++------- .../Webkul/Shipping/src/Carriers/FlatRate.php | 8 +++--- .../Webkul/Shipping/src/Carriers/Free.php | 6 +++-- packages/Webkul/Shipping/src/Shipping.php | 5 ++-- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 182a407d2..eb1643f79 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -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; } diff --git a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php index 8394be99b..13b0bcb3c 100755 --- a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php +++ b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php @@ -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 + * @param string $field * @return mixed */ public function getConfigData($field) diff --git a/packages/Webkul/Shipping/src/Carriers/FlatRate.php b/packages/Webkul/Shipping/src/Carriers/FlatRate.php index 5d342aa03..aa36b1692 100755 --- a/packages/Webkul/Shipping/src/Carriers/FlatRate.php +++ b/packages/Webkul/Shipping/src/Carriers/FlatRate.php @@ -14,11 +14,13 @@ use Webkul\Checkout\Facades\Cart; class FlatRate extends AbstractShipping { /** - * Payment method code + * Shipping method code * * @var string */ - protected $code = 'flatrate'; + 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'); diff --git a/packages/Webkul/Shipping/src/Carriers/Free.php b/packages/Webkul/Shipping/src/Carriers/Free.php index a90b053bb..c33f0db5b 100755 --- a/packages/Webkul/Shipping/src/Carriers/Free.php +++ b/packages/Webkul/Shipping/src/Carriers/Free.php @@ -13,11 +13,13 @@ use Webkul\Shipping\Facades\Shipping; class Free extends AbstractShipping { /** - * Payment method code + * Shipping method code * * @var string */ - protected $code = 'free'; + protected $code = 'free'; + + protected $method = 'free_free'; /** * Returns rate for flatrate diff --git a/packages/Webkul/Shipping/src/Shipping.php b/packages/Webkul/Shipping/src/Shipping.php index 3d1dc7151..aaac7e124 100755 --- a/packages/Webkul/Shipping/src/Shipping.php +++ b/packages/Webkul/Shipping/src/Shipping.php @@ -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() ]; From 84431fa4bcc0526c4a01173d8e066a590c051a01 Mon Sep 17 00:00:00 2001 From: AmooAti Date: Mon, 8 Nov 2021 16:22:58 +0330 Subject: [PATCH 5/5] doc type and style --- packages/Webkul/Checkout/src/Cart.php | 2 +- .../Webkul/Shipping/src/Carriers/AbstractShipping.php | 11 +++++++++++ packages/Webkul/Shipping/src/Carriers/FlatRate.php | 7 ++++++- packages/Webkul/Shipping/src/Carriers/Free.php | 7 ++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index eb1643f79..0c0736029 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -446,7 +446,7 @@ class Cart return false; } - if (!array_key_exists($shippingMethodCode, Shipping::getShippingMethods())) { + if (! array_key_exists($shippingMethodCode, Shipping::getShippingMethods())) { return false; } diff --git a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php index 13b0bcb3c..7e688162a 100755 --- a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php +++ b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php @@ -4,7 +4,18 @@ namespace Webkul\Shipping\Carriers; abstract class AbstractShipping { + /** + * Shipping method carrier code + * + * @var string + */ protected $code; + + /** + * Shipping method code + * + * @var string + */ protected $method; abstract public function calculate(); diff --git a/packages/Webkul/Shipping/src/Carriers/FlatRate.php b/packages/Webkul/Shipping/src/Carriers/FlatRate.php index aa36b1692..4e2a5e20e 100755 --- a/packages/Webkul/Shipping/src/Carriers/FlatRate.php +++ b/packages/Webkul/Shipping/src/Carriers/FlatRate.php @@ -14,12 +14,17 @@ use Webkul\Checkout\Facades\Cart; class FlatRate extends AbstractShipping { /** - * Shipping method code + * Shipping method carrier code * * @var string */ protected $code = 'flatrate'; + /** + * Shipping method code + * + * @var string + */ protected $method = 'flatrate_flatrate'; /** diff --git a/packages/Webkul/Shipping/src/Carriers/Free.php b/packages/Webkul/Shipping/src/Carriers/Free.php index c33f0db5b..7704d5bf6 100755 --- a/packages/Webkul/Shipping/src/Carriers/Free.php +++ b/packages/Webkul/Shipping/src/Carriers/Free.php @@ -13,12 +13,17 @@ use Webkul\Shipping\Facades\Shipping; class Free extends AbstractShipping { /** - * Shipping method code + * Shipping method carrier code * * @var string */ protected $code = 'free'; + /** + * Shipping method code + * + * @var string + */ protected $method = 'free_free'; /**