| 1 |
<?php |
| 2 |
|
| 3 |
namespace Packetery\Module\Carrier; |
| 4 |
|
| 5 |
use Packetery\Core\Entity; |
| 6 |
use Packetery\Module\Carrier; |
| 7 |
use Packetery\Module\Options\OptionsProvider; |
| 8 |
use Packetery\Module\Shipping\BaseShippingMethod; |
| 9 |
use WC_Shipping_Zone; |
| 10 |
use WC_Shipping_Zones; |
| 11 |
|
| 12 |
class CarrierActivityBridge { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var OptionsProvider |
| 16 |
*/ |
| 17 |
private $optionsProvider; |
| 18 |
|
| 19 |
public function __construct( OptionsProvider $optionsProvider ) { |
| 20 |
$this->optionsProvider = $optionsProvider; |
| 21 |
} |
| 22 |
|
| 23 |
private function getActiveCarrierIds(): array { |
| 24 |
static $activeMethods; |
| 25 |
|
| 26 |
if ( isset( $activeMethods ) ) { |
| 27 |
return $activeMethods; |
| 28 |
} |
| 29 |
|
| 30 |
$activeMethods = []; |
| 31 |
$shippingZones = WC_Shipping_Zones::get_zones(); |
| 32 |
$defaultShippingZone = WC_Shipping_Zones::get_zone_by(); |
| 33 |
if ( $defaultShippingZone instanceof WC_Shipping_Zone ) { |
| 34 |
$shippingZones[] = [ |
| 35 |
'shipping_methods' => $defaultShippingZone->get_shipping_methods(), |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
foreach ( $shippingZones as $shippingZone ) { |
| 40 |
$shippingMethods = $shippingZone['shipping_methods']; |
| 41 |
|
| 42 |
foreach ( $shippingMethods as $shippingMethod ) { |
| 43 |
if ( $shippingMethod instanceof BaseShippingMethod && $shippingMethod->enabled === 'yes' ) { |
| 44 |
$activeMethods[] = $shippingMethod::CARRIER_ID; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $activeMethods; |
| 50 |
} |
| 51 |
|
| 52 |
public function isActive( Entity\Carrier $carrier, Carrier\Options $carrierOptions ): bool { |
| 53 |
if ( $this->optionsProvider->isWcCarrierConfigEnabled() ) { |
| 54 |
return $carrier->isAvailable() && in_array( $carrier->getId(), $this->getActiveCarrierIds(), true ); |
| 55 |
} |
| 56 |
|
| 57 |
return $carrier->isAvailable() && $carrierOptions->isActive(); |
| 58 |
} |
| 59 |
} |
| 60 |
|