diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 0c0736029..d5cb8f504 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 (! Shipping::isMethodCodeExists($shippingMethodCode)) { return false; } diff --git a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php index 1886f6409..506bb209e 100755 --- a/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php +++ b/packages/Webkul/Shipping/src/Carriers/AbstractShipping.php @@ -2,17 +2,19 @@ namespace Webkul\Shipping\Carriers; +use Webkul\Shipping\Exceptions\CarrierCodeException; + abstract class AbstractShipping { /** - * Shipping method carrier code + * Shipping method carrier code. * * @var string */ protected $code; /** - * Shipping method code + * Shipping method code. * * @var string */ @@ -21,7 +23,7 @@ abstract class AbstractShipping abstract public function calculate(); /** - * Checks if shipping method is available + * Checks if shipping method is available. * * @return array */ @@ -31,30 +33,37 @@ abstract class AbstractShipping } /** - * Returns shipping method code + * Returns shipping method carrier code. * - * @return array + * @return string */ public function getCode() { if (empty($this->code)) { - // throw exception + throw new CarrierCodeException('Carrier code should be initialized.'); } return $this->code; } + /** + * Return shipping method code. + * + * @return string + */ public function getMethod() { if (empty($this->method)) { - // throw exception + $code = $this->getCode(); + + return $code . '_' . $code; } return $this->method; } /** - * Returns shipping method title + * Returns shipping method title. * * @return array */ @@ -64,7 +73,7 @@ abstract class AbstractShipping } /** - * Returns shipping method description + * Returns shipping method description. * * @return array */ @@ -74,9 +83,9 @@ abstract class AbstractShipping } /** - * Retrieve information from shipping configuration + * Retrieve information from shipping configuration. * - * @param string $field + * @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 4e2a5e20e..c189138a8 100755 --- a/packages/Webkul/Shipping/src/Carriers/FlatRate.php +++ b/packages/Webkul/Shipping/src/Carriers/FlatRate.php @@ -2,35 +2,29 @@ namespace Webkul\Shipping\Carriers; -use Config; -use Webkul\Checkout\Models\CartShippingRate; -use Webkul\Shipping\Facades\Shipping; use Webkul\Checkout\Facades\Cart; +use Webkul\Checkout\Models\CartShippingRate; -/** - * Class Rate. - * - */ class FlatRate extends AbstractShipping { /** - * Shipping method carrier code + * Shipping method carrier code. * * @var string */ protected $code = 'flatrate'; /** - * Shipping method code + * Shipping method code. * * @var string */ protected $method = 'flatrate_flatrate'; /** - * Returns rate for flatrate + * Calculate rate for flatrate. * - * @return CartShippingRate|false + * @return \Webkul\Checkout\Models\CartShippingRate|false */ public function calculate() { @@ -38,31 +32,41 @@ class FlatRate extends AbstractShipping return false; } + return $this->getRate(); + } + + /** + * Get rate. + * + * @return \Webkul\Checkout\Models\CartShippingRate + */ + public function getRate(): \Webkul\Checkout\Models\CartShippingRate + { $cart = Cart::getCart(); - $object = new CartShippingRate; + $cartShippingRate = new CartShippingRate; - $object->carrier = 'flatrate'; - $object->carrier_title = $this->getConfigData('title'); - $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'); - $object->price = 0; - $object->base_price = 0; + $cartShippingRate->carrier = $this->getCode(); + $cartShippingRate->carrier_title = $this->getConfigData('title'); + $cartShippingRate->method = $this->getMethod(); + $cartShippingRate->method_title = $this->getConfigData('title'); + $cartShippingRate->method_description = $this->getConfigData('description'); + $cartShippingRate->is_calculate_tax = $this->getConfigData('is_calculate_tax'); + $cartShippingRate->price = 0; + $cartShippingRate->base_price = 0; if ($this->getConfigData('type') == 'per_unit') { foreach ($cart->items as $item) { if ($item->product->getTypeInstance()->isStockable()) { - $object->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity; - $object->base_price += $this->getConfigData('default_rate') * $item->quantity; + $cartShippingRate->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity; + $cartShippingRate->base_price += $this->getConfigData('default_rate') * $item->quantity; } } } else { - $object->price = core()->convertPrice($this->getConfigData('default_rate')); - $object->base_price = $this->getConfigData('default_rate'); + $cartShippingRate->price = core()->convertPrice($this->getConfigData('default_rate')); + $cartShippingRate->base_price = $this->getConfigData('default_rate'); } - return $object; + return $cartShippingRate; } -} \ No newline at end of file +} diff --git a/packages/Webkul/Shipping/src/Carriers/Free.php b/packages/Webkul/Shipping/src/Carriers/Free.php index 7704d5bf6..8115da105 100755 --- a/packages/Webkul/Shipping/src/Carriers/Free.php +++ b/packages/Webkul/Shipping/src/Carriers/Free.php @@ -2,32 +2,26 @@ namespace Webkul\Shipping\Carriers; -use Config; use Webkul\Checkout\Models\CartShippingRate; -use Webkul\Shipping\Facades\Shipping; -/** - * Class Rate. - * - */ class Free extends AbstractShipping { /** - * Shipping method carrier code + * Shipping method carrier code. * * @var string */ protected $code = 'free'; /** - * Shipping method code + * Shipping method code. * * @var string */ protected $method = 'free_free'; /** - * Returns rate for flatrate + * Calculate rate for free shipping. * * @return CartShippingRate|false */ @@ -37,17 +31,27 @@ class Free extends AbstractShipping return false; } - $object = new CartShippingRate; - - $object->carrier = 'free'; - $object->carrier_title = $this->getConfigData('title'); - $object->method = 'free_free'; - $object->method_title = $this->getConfigData('title'); - $object->method_description = $this->getConfigData('description'); - $object->is_calculate_tax = $this->getConfigData('is_calculate_tax'); - $object->price = 0; - $object->base_price = 0; - - return $object; + return $this->getRate(); } -} \ No newline at end of file + + /** + * Get rate. + * + * @return \Webkul\Checkout\Models\CartShippingRate + */ + public function getRate(): \Webkul\Checkout\Models\CartShippingRate + { + $cartShippingRate = new CartShippingRate; + + $cartShippingRate->carrier = $this->getCode(); + $cartShippingRate->carrier_title = $this->getConfigData('title'); + $cartShippingRate->method = $this->getMethod(); + $cartShippingRate->method_title = $this->getConfigData('title'); + $cartShippingRate->method_description = $this->getConfigData('description'); + $cartShippingRate->is_calculate_tax = $this->getConfigData('is_calculate_tax'); + $cartShippingRate->price = 0; + $cartShippingRate->base_price = 0; + + return $cartShippingRate; + } +} diff --git a/packages/Webkul/Shipping/src/Config/carriers.php b/packages/Webkul/Shipping/src/Config/carriers.php index fd870fa61..f5422100b 100755 --- a/packages/Webkul/Shipping/src/Config/carriers.php +++ b/packages/Webkul/Shipping/src/Config/carriers.php @@ -21,4 +21,4 @@ return [ 'default_rate' => '0', 'class' => 'Webkul\Shipping\Carriers\Free', ] -]; \ No newline at end of file +]; diff --git a/packages/Webkul/Shipping/src/Exceptions/CarrierCodeException.php b/packages/Webkul/Shipping/src/Exceptions/CarrierCodeException.php new file mode 100755 index 000000000..217f8c663 --- /dev/null +++ b/packages/Webkul/Shipping/src/Exceptions/CarrierCodeException.php @@ -0,0 +1,9 @@ +getMethod()] = [ + $methods[] = [ 'code' => $object->getCode(), 'method' => $object->getMethod(), 'method_title' => $object->getTitle(), @@ -140,4 +136,17 @@ class Shipping return $methods; } -} \ No newline at end of file + + /** + * Is method exist in active shipping methods. + * + * @param string $shippingMethodCode + * @return boolean + */ + public function isMethodCodeExists($shippingMethodCode) + { + $activeShippingMethods = collect($this->getShippingMethods()); + + return $activeShippingMethods->contains('method', $shippingMethodCode); + } +}