Shipping.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Webkul\Shipping;
  3. use Illuminate\Support\Facades\Config;
  4. use Webkul\Checkout\Facades\Cart;
  5. class Shipping
  6. {
  7. /**
  8. * Rates.
  9. *
  10. * @var array
  11. */
  12. protected $rates = [];
  13. /**
  14. * Collects rate from available shipping methods.
  15. *
  16. * @return array|bool
  17. */
  18. public function collectRates()
  19. {
  20. if (! Cart::getCart()) {
  21. return false;
  22. }
  23. $this->removeAllShippingRates();
  24. $ratesList = [];
  25. foreach (Config::get('carriers') as $shippingMethod) {
  26. $object = new $shippingMethod['class'];
  27. if ($rates = $object->calculate()) {
  28. if (is_array($rates)) {
  29. $ratesList[] = $rates;
  30. } else {
  31. $ratesList[] = [$rates];
  32. }
  33. }
  34. }
  35. $this->rates = array_merge(...$ratesList);
  36. $this->saveAllShippingRates();
  37. return [
  38. 'shippingMethods' => $this->getGroupedAllShippingRates(),
  39. ];
  40. }
  41. /**
  42. * Remove all shipping rates.
  43. *
  44. * @return void
  45. */
  46. public function removeAllShippingRates()
  47. {
  48. if (! $cart = Cart::getCart()) {
  49. return;
  50. }
  51. $cart->shipping_rates()->delete();
  52. $this->rates = [];
  53. }
  54. /**
  55. * Save all shipping rates.
  56. *
  57. * @return void
  58. */
  59. public function saveAllShippingRates()
  60. {
  61. if (! $cart = Cart::getCart()) {
  62. return;
  63. }
  64. $shippingAddress = $cart->shipping_address;
  65. if (! $shippingAddress) {
  66. return;
  67. }
  68. foreach ($this->rates as $rate) {
  69. $rate->cart_id = $cart->id;
  70. $rate->cart_address_id = $shippingAddress->id;
  71. $rate->price_incl_tax = $rate->price;
  72. $rate->base_price_incl_tax = $rate->base_price;
  73. $rate->save();
  74. }
  75. }
  76. /**
  77. * Returns shipping rates, grouped by shipping method.
  78. *
  79. * @return array
  80. */
  81. public function getGroupedAllShippingRates()
  82. {
  83. $rates = [];
  84. foreach ($this->rates as $rate) {
  85. if (! isset($rates[$rate->carrier])) {
  86. $rates[$rate->carrier] = [
  87. 'carrier_title' => $rate->carrier_title,
  88. 'rates' => [],
  89. ];
  90. }
  91. $rate['base_formatted_price'] = core()->currency($rate->base_price);
  92. $rates[$rate->carrier]['rates'][] = $rate;
  93. }
  94. return $rates;
  95. }
  96. /**
  97. * Returns active shipping methods.
  98. *
  99. * @return array
  100. */
  101. public function getShippingMethods()
  102. {
  103. $methods = [];
  104. foreach (Config::get('carriers') as $shippingMethod) {
  105. $object = new $shippingMethod['class'];
  106. if (! $object->isAvailable()) {
  107. continue;
  108. }
  109. $methods[] = [
  110. 'code' => $object->getCode(),
  111. 'method' => $object->getMethod(),
  112. 'method_title' => $object->getTitle(),
  113. 'description' => $object->getDescription(),
  114. ];
  115. }
  116. return $methods;
  117. }
  118. /**
  119. * Is method exist in active shipping methods.
  120. *
  121. * @param string $shippingMethodCode
  122. * @return bool
  123. */
  124. public function isMethodCodeExists($shippingMethodCode)
  125. {
  126. $shippingMethods = $this->collectRates()['shippingMethods'] ?? [];
  127. if (
  128. empty($shippingMethods)
  129. || ! $shippingMethods
  130. ) {
  131. return false;
  132. }
  133. foreach ($shippingMethods as $shippingMethod) {
  134. foreach ($shippingMethod['rates'] as $rate) {
  135. if ($rate->method === $shippingMethodCode) {
  136. return true;
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. }