FlatRate.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Webkul\Shipping\Carriers;
  3. use Webkul\Checkout\Facades\Cart;
  4. use Webkul\Checkout\Models\CartShippingRate;
  5. class FlatRate extends AbstractShipping
  6. {
  7. /**
  8. * Shipping method carrier code.
  9. *
  10. * @var string
  11. */
  12. protected $code = 'flatrate';
  13. /**
  14. * Shipping method code.
  15. *
  16. * @var string
  17. */
  18. protected $method = 'flatrate_flatrate';
  19. /**
  20. * Calculate rate for flatrate.
  21. *
  22. * @return \Webkul\Checkout\Models\CartShippingRate|false
  23. */
  24. public function calculate()
  25. {
  26. if (! $this->isAvailable()) {
  27. return false;
  28. }
  29. return $this->getRate();
  30. }
  31. /**
  32. * Get rate.
  33. */
  34. public function getRate(): CartShippingRate
  35. {
  36. $cart = Cart::getCart();
  37. $cartShippingRate = new CartShippingRate;
  38. $cartShippingRate->carrier = $this->getCode();
  39. $cartShippingRate->carrier_title = $this->getConfigData('title');
  40. $cartShippingRate->method = $this->getMethod();
  41. $cartShippingRate->method_title = $this->getConfigData('title');
  42. $cartShippingRate->method_description = $this->getConfigData('description');
  43. $cartShippingRate->price = 0;
  44. $cartShippingRate->base_price = 0;
  45. if ($this->getConfigData('type') == 'per_unit') {
  46. foreach ($cart->items as $item) {
  47. if ($item->getTypeInstance()->isStockable()) {
  48. $cartShippingRate->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity;
  49. $cartShippingRate->base_price += $this->getConfigData('default_rate') * $item->quantity;
  50. }
  51. }
  52. } else {
  53. $cartShippingRate->price = core()->convertPrice($this->getConfigData('default_rate'));
  54. $cartShippingRate->base_price = $this->getConfigData('default_rate');
  55. }
  56. return $cartShippingRate;
  57. }
  58. }