| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Webkul\Shipping;
- use Illuminate\Support\Facades\Config;
- use Webkul\Checkout\Facades\Cart;
- class Shipping
- {
- /**
- * Rates.
- *
- * @var array
- */
- protected $rates = [];
- /**
- * Collects rate from available shipping methods.
- *
- * @return array|bool
- */
- public function collectRates()
- {
- if (! Cart::getCart()) {
- return false;
- }
- $this->removeAllShippingRates();
- $ratesList = [];
- foreach (Config::get('carriers') as $shippingMethod) {
- $object = new $shippingMethod['class'];
- if ($rates = $object->calculate()) {
- if (is_array($rates)) {
- $ratesList[] = $rates;
- } else {
- $ratesList[] = [$rates];
- }
- }
- }
- $this->rates = array_merge(...$ratesList);
- $this->saveAllShippingRates();
- return [
- 'shippingMethods' => $this->getGroupedAllShippingRates(),
- ];
- }
- /**
- * Remove all shipping rates.
- *
- * @return void
- */
- public function removeAllShippingRates()
- {
- if (! $cart = Cart::getCart()) {
- return;
- }
- $cart->shipping_rates()->delete();
- $this->rates = [];
- }
- /**
- * Save all shipping rates.
- *
- * @return void
- */
- public function saveAllShippingRates()
- {
- if (! $cart = Cart::getCart()) {
- return;
- }
- $shippingAddress = $cart->shipping_address;
- if (! $shippingAddress) {
- return;
- }
- foreach ($this->rates as $rate) {
- $rate->cart_id = $cart->id;
- $rate->cart_address_id = $shippingAddress->id;
- $rate->price_incl_tax = $rate->price;
- $rate->base_price_incl_tax = $rate->base_price;
- $rate->save();
- }
- }
- /**
- * Returns shipping rates, grouped by shipping method.
- *
- * @return array
- */
- public function getGroupedAllShippingRates()
- {
- $rates = [];
- foreach ($this->rates as $rate) {
- if (! isset($rates[$rate->carrier])) {
- $rates[$rate->carrier] = [
- 'carrier_title' => $rate->carrier_title,
- 'rates' => [],
- ];
- }
- $rate['base_formatted_price'] = core()->currency($rate->base_price);
- $rates[$rate->carrier]['rates'][] = $rate;
- }
- return $rates;
- }
- /**
- * Returns active shipping methods.
- *
- * @return array
- */
- public function getShippingMethods()
- {
- $methods = [];
- foreach (Config::get('carriers') as $shippingMethod) {
- $object = new $shippingMethod['class'];
- if (! $object->isAvailable()) {
- continue;
- }
- $methods[] = [
- 'code' => $object->getCode(),
- 'method' => $object->getMethod(),
- 'method_title' => $object->getTitle(),
- 'description' => $object->getDescription(),
- ];
- }
- return $methods;
- }
- /**
- * Is method exist in active shipping methods.
- *
- * @param string $shippingMethodCode
- * @return bool
- */
- public function isMethodCodeExists($shippingMethodCode)
- {
- $shippingMethods = $this->collectRates()['shippingMethods'] ?? [];
- if (
- empty($shippingMethods)
- || ! $shippingMethods
- ) {
- return false;
- }
- foreach ($shippingMethods as $shippingMethod) {
- foreach ($shippingMethod['rates'] as $rate) {
- if ($rate->method === $shippingMethodCode) {
- return true;
- }
- }
- }
- return false;
- }
- }
|