FlexibleShippingMethodsChecker.php
3 days ago
ShippingMethod.php
3 days ago
WooSettingsPageChecker.php
3 days ago
ShippingMethod.php
68 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helpers for Shipping method. |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\NewRulesTablePointer |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\Helpers; |
| 9 | |
| 10 | /** |
| 11 | * Collection of helpers for Shipping method. |
| 12 | */ |
| 13 | class ShippingMethod { |
| 14 | |
| 15 | /** |
| 16 | * Pattern of option with Flexible Shipping methods. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const FS_METHODS_OPTION_PREFIX = 'flexible_shipping_methods_%d'; |
| 21 | |
| 22 | /** |
| 23 | * Checks if there are Flexible Shipping methods in the Shipping Zones. |
| 24 | * |
| 25 | * @param string $method_name Name of Shipping method. |
| 26 | * |
| 27 | * @return bool Status. |
| 28 | */ |
| 29 | public static function check_if_method_exists_in_zones( $method_name ) { |
| 30 | $zones = \WC_Shipping_Zones::get_zones(); |
| 31 | foreach ( $zones as $zone ) { |
| 32 | $zone_instance = \WC_Shipping_Zones::get_zone( $zone['zone_id'] ); |
| 33 | |
| 34 | if ( self::check_if_method_exists_in_zone( $method_name, $zone_instance ) === true ) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Checks if there are Flexible Shipping methods in the Shipping Zone. |
| 44 | * |
| 45 | * @param string $method_name Name of Shipping method. |
| 46 | * @param \WC_Shipping_Zone $zone_instance Instance of Shipping Zone. |
| 47 | * |
| 48 | * @return bool Status. |
| 49 | */ |
| 50 | private static function check_if_method_exists_in_zone( $method_name, $zone_instance ) { |
| 51 | $zone_methods = $zone_instance->get_shipping_methods(); |
| 52 | foreach ( $zone_methods as $zone_method ) { |
| 53 | if ( $zone_method->id !== $method_name ) { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | $option_key = sprintf( self::FS_METHODS_OPTION_PREFIX, $zone_method->instance_id ); |
| 58 | $shipping_methods = get_option( $option_key, [] ); |
| 59 | if ( $shipping_methods ) { |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | } |
| 68 |