Merge pull request #5313 from devansh-webkul/improvised-shipping-classes
Shipping package improved
This commit is contained in:
commit
02336b5e27
|
|
@ -446,7 +446,7 @@ class Cart
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! array_key_exists($shippingMethodCode, Shipping::getShippingMethods())) {
|
if (! Shipping::isMethodCodeExists($shippingMethodCode)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,19 @@
|
||||||
|
|
||||||
namespace Webkul\Shipping\Carriers;
|
namespace Webkul\Shipping\Carriers;
|
||||||
|
|
||||||
|
use Webkul\Shipping\Exceptions\CarrierCodeException;
|
||||||
|
|
||||||
abstract class AbstractShipping
|
abstract class AbstractShipping
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Shipping method carrier code
|
* Shipping method carrier code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $code;
|
protected $code;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shipping method code
|
* Shipping method code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
|
|
@ -21,7 +23,7 @@ abstract class AbstractShipping
|
||||||
abstract public function calculate();
|
abstract public function calculate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if shipping method is available
|
* Checks if shipping method is available.
|
||||||
*
|
*
|
||||||
* @return array
|
* @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()
|
public function getCode()
|
||||||
{
|
{
|
||||||
if (empty($this->code)) {
|
if (empty($this->code)) {
|
||||||
// throw exception
|
throw new CarrierCodeException('Carrier code should be initialized.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->code;
|
return $this->code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return shipping method code.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
{
|
{
|
||||||
if (empty($this->method)) {
|
if (empty($this->method)) {
|
||||||
// throw exception
|
$code = $this->getCode();
|
||||||
|
|
||||||
|
return $code . '_' . $code;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->method;
|
return $this->method;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns shipping method title
|
* Returns shipping method title.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -64,7 +73,7 @@ abstract class AbstractShipping
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns shipping method description
|
* Returns shipping method description.
|
||||||
*
|
*
|
||||||
* @return array
|
* @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
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getConfigData($field)
|
public function getConfigData($field)
|
||||||
|
|
|
||||||
|
|
@ -2,35 +2,29 @@
|
||||||
|
|
||||||
namespace Webkul\Shipping\Carriers;
|
namespace Webkul\Shipping\Carriers;
|
||||||
|
|
||||||
use Config;
|
|
||||||
use Webkul\Checkout\Models\CartShippingRate;
|
|
||||||
use Webkul\Shipping\Facades\Shipping;
|
|
||||||
use Webkul\Checkout\Facades\Cart;
|
use Webkul\Checkout\Facades\Cart;
|
||||||
|
use Webkul\Checkout\Models\CartShippingRate;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Rate.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class FlatRate extends AbstractShipping
|
class FlatRate extends AbstractShipping
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Shipping method carrier code
|
* Shipping method carrier code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $code = 'flatrate';
|
protected $code = 'flatrate';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shipping method code
|
* Shipping method code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $method = 'flatrate_flatrate';
|
protected $method = 'flatrate_flatrate';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns rate for flatrate
|
* Calculate rate for flatrate.
|
||||||
*
|
*
|
||||||
* @return CartShippingRate|false
|
* @return \Webkul\Checkout\Models\CartShippingRate|false
|
||||||
*/
|
*/
|
||||||
public function calculate()
|
public function calculate()
|
||||||
{
|
{
|
||||||
|
|
@ -38,31 +32,41 @@ class FlatRate extends AbstractShipping
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->getRate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get rate.
|
||||||
|
*
|
||||||
|
* @return \Webkul\Checkout\Models\CartShippingRate
|
||||||
|
*/
|
||||||
|
public function getRate(): \Webkul\Checkout\Models\CartShippingRate
|
||||||
|
{
|
||||||
$cart = Cart::getCart();
|
$cart = Cart::getCart();
|
||||||
|
|
||||||
$object = new CartShippingRate;
|
$cartShippingRate = new CartShippingRate;
|
||||||
|
|
||||||
$object->carrier = 'flatrate';
|
$cartShippingRate->carrier = $this->getCode();
|
||||||
$object->carrier_title = $this->getConfigData('title');
|
$cartShippingRate->carrier_title = $this->getConfigData('title');
|
||||||
$object->method = $this->method;
|
$cartShippingRate->method = $this->getMethod();
|
||||||
$object->method_title = $this->getConfigData('title');
|
$cartShippingRate->method_title = $this->getConfigData('title');
|
||||||
$object->method_description = $this->getConfigData('description');
|
$cartShippingRate->method_description = $this->getConfigData('description');
|
||||||
$object->is_calculate_tax = $this->getConfigData('is_calculate_tax');
|
$cartShippingRate->is_calculate_tax = $this->getConfigData('is_calculate_tax');
|
||||||
$object->price = 0;
|
$cartShippingRate->price = 0;
|
||||||
$object->base_price = 0;
|
$cartShippingRate->base_price = 0;
|
||||||
|
|
||||||
if ($this->getConfigData('type') == 'per_unit') {
|
if ($this->getConfigData('type') == 'per_unit') {
|
||||||
foreach ($cart->items as $item) {
|
foreach ($cart->items as $item) {
|
||||||
if ($item->product->getTypeInstance()->isStockable()) {
|
if ($item->product->getTypeInstance()->isStockable()) {
|
||||||
$object->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity;
|
$cartShippingRate->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity;
|
||||||
$object->base_price += $this->getConfigData('default_rate') * $item->quantity;
|
$cartShippingRate->base_price += $this->getConfigData('default_rate') * $item->quantity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$object->price = core()->convertPrice($this->getConfigData('default_rate'));
|
$cartShippingRate->price = core()->convertPrice($this->getConfigData('default_rate'));
|
||||||
$object->base_price = $this->getConfigData('default_rate');
|
$cartShippingRate->base_price = $this->getConfigData('default_rate');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $object;
|
return $cartShippingRate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,32 +2,26 @@
|
||||||
|
|
||||||
namespace Webkul\Shipping\Carriers;
|
namespace Webkul\Shipping\Carriers;
|
||||||
|
|
||||||
use Config;
|
|
||||||
use Webkul\Checkout\Models\CartShippingRate;
|
use Webkul\Checkout\Models\CartShippingRate;
|
||||||
use Webkul\Shipping\Facades\Shipping;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Rate.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class Free extends AbstractShipping
|
class Free extends AbstractShipping
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Shipping method carrier code
|
* Shipping method carrier code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $code = 'free';
|
protected $code = 'free';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shipping method code
|
* Shipping method code.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $method = 'free_free';
|
protected $method = 'free_free';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns rate for flatrate
|
* Calculate rate for free shipping.
|
||||||
*
|
*
|
||||||
* @return CartShippingRate|false
|
* @return CartShippingRate|false
|
||||||
*/
|
*/
|
||||||
|
|
@ -37,17 +31,27 @@ class Free extends AbstractShipping
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$object = new CartShippingRate;
|
return $this->getRate();
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,4 @@ return [
|
||||||
'default_rate' => '0',
|
'default_rate' => '0',
|
||||||
'class' => 'Webkul\Shipping\Carriers\Free',
|
'class' => 'Webkul\Shipping\Carriers\Free',
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Webkul\Shipping\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class CarrierCodeException extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -5,21 +5,17 @@ namespace Webkul\Shipping;
|
||||||
use Illuminate\Support\Facades\Config;
|
use Illuminate\Support\Facades\Config;
|
||||||
use Webkul\Checkout\Facades\Cart;
|
use Webkul\Checkout\Facades\Cart;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Shipping.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class Shipping
|
class Shipping
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Rates
|
* Rates.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $rates = [];
|
protected $rates = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects rate from available shipping methods
|
* Collects rate from available shipping methods.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -53,7 +49,7 @@ class Shipping
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persist shipping rate to database
|
* Remove all shipping rates.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -69,7 +65,7 @@ class Shipping
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persist shipping rate to database
|
* Save all shipping rates.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -92,7 +88,7 @@ class Shipping
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns shipping rates, grouped by shipping method
|
* Returns shipping rates, grouped by shipping method.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -115,7 +111,7 @@ class Shipping
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns active shipping methods
|
* Returns active shipping methods.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -130,7 +126,7 @@ class Shipping
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$methods[$object->getMethod()] = [
|
$methods[] = [
|
||||||
'code' => $object->getCode(),
|
'code' => $object->getCode(),
|
||||||
'method' => $object->getMethod(),
|
'method' => $object->getMethod(),
|
||||||
'method_title' => $object->getTitle(),
|
'method_title' => $object->getTitle(),
|
||||||
|
|
@ -140,4 +136,17 @@ class Shipping
|
||||||
|
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue