Integration.php
3 days ago
OrderMetaData.php
3 days ago
SettingsFields.php
3 days ago
ShippingRate.php
3 days ago
Tracker.php
3 days ago
Tracker.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FS\TableRate\ShippingMethodsIntegration; |
| 4 | |
| 5 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 6 | |
| 7 | /** |
| 8 | * Class Tracker |
| 9 | * Can add data to tracker. |
| 10 | */ |
| 11 | class Tracker implements Hookable { |
| 12 | |
| 13 | public function hooks(): void { |
| 14 | add_action( |
| 15 | 'wpdesk_tracker_data', |
| 16 | [ |
| 17 | $this, |
| 18 | 'add_data_to_tracker', |
| 19 | ], |
| 20 | \WPDesk_Flexible_Shipping_Tracker::TRACKER_DATA_FILTER_PRIORITY + 1 |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param array $data |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function add_data_to_tracker( $data ) { |
| 30 | $shipping_methods = $this->get_shipping_methods(); |
| 31 | $tracker_data = []; |
| 32 | foreach ( $shipping_methods as $shipping_method ) { |
| 33 | $tracker_data = $this->add_shipping_method_to_tracker_data( $tracker_data, $shipping_method ); |
| 34 | } |
| 35 | $data['flexible_shipping']['rules_table_other_methods'] = $tracker_data; |
| 36 | |
| 37 | return $data; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return \WC_Shipping_Method[] |
| 42 | */ |
| 43 | private function get_shipping_methods(): array { |
| 44 | $shipping_methods = []; |
| 45 | $shipping_zones = [ new \WC_Shipping_Zone( 0 ) ]; |
| 46 | foreach ( \WC_Shipping_Zones::get_zones() as $zone ) { |
| 47 | $shipping_zones[] = new \WC_Shipping_Zone( $zone['zone_id'] ); |
| 48 | } |
| 49 | foreach ( $shipping_zones as $zone ) { |
| 50 | $zone_shipping_methods = $zone->get_shipping_methods( true, 'admin' ); |
| 51 | foreach ( $zone_shipping_methods as $zone_shipping_method ) { |
| 52 | if ( $zone_shipping_method instanceof \WC_Shipping_Method ) { |
| 53 | $shipping_methods[] = $zone_shipping_method; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return $shipping_methods; |
| 59 | } |
| 60 | |
| 61 | private function add_shipping_method_to_tracker_data( array $tracker_data, \WC_Shipping_Method $shipping_method ): array { |
| 62 | if ( $shipping_method->get_option( SettingsFields::FS_CALCULATION_ENABLED, 'no' ) === 'no' ) { |
| 63 | return $tracker_data; |
| 64 | } |
| 65 | if ( empty( $tracker_data[ $shipping_method->id ] ) ) { |
| 66 | $tracker_data[ $shipping_method->id ] = 1; |
| 67 | } else { |
| 68 | ++$tracker_data[ $shipping_method->id ]; |
| 69 | } |
| 70 | |
| 71 | return $tracker_data; |
| 72 | } |
| 73 | } |
| 74 |