flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
Management
/
ShippingMethodManagement.php
ShippingMethodManagement.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class ShippingMethodManagement |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod\Management |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod\Management; |
| 9 | |
| 10 | use WC_Shipping_Zone; |
| 11 | use WC_Shipping_Zones; |
| 12 | use wpdb; |
| 13 | use WPDesk\FS\TableRate\ShippingMethodSingle; |
| 14 | |
| 15 | /** |
| 16 | * @codeCoverageIgnore |
| 17 | */ |
| 18 | class ShippingMethodManagement { |
| 19 | |
| 20 | /** |
| 21 | * @var wpdb |
| 22 | */ |
| 23 | private $wpdb; |
| 24 | |
| 25 | /** |
| 26 | * @param wpdb $wpdb |
| 27 | */ |
| 28 | public function __construct( wpdb $wpdb ) { |
| 29 | $this->wpdb = $wpdb; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param int $instance_id . |
| 34 | * @param bool $status . |
| 35 | * @param WC_Shipping_Zone $zone . |
| 36 | */ |
| 37 | public function set_shipping_method_status( int $instance_id, bool $status, WC_Shipping_Zone $zone ) { |
| 38 | $status = $status ? 1 : 0; |
| 39 | |
| 40 | if ( $this->update_shipping_method_field( $instance_id, 'is_enabled', $status ) ) { |
| 41 | $shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id ); |
| 42 | |
| 43 | do_action( 'woocommerce_shipping_zone_method_status_toggled', $shipping_method->instance_id, $shipping_method->id, $zone->get_id(), $status ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param int $zone_id . |
| 49 | * @param int $method_order . |
| 50 | * @param int $number_of_shipping_methods . |
| 51 | */ |
| 52 | public function update_shipping_methods_order( int $zone_id, int $method_order, int $number_of_shipping_methods ) { |
| 53 | $this->wpdb->query( $this->wpdb->prepare( sprintf( 'UPDATE `%s` SET `method_order` = `method_order`+%%d WHERE `zone_id` = %%d AND `method_order` > %%d', $this->get_table() ), $number_of_shipping_methods, $zone_id, $method_order ) ); // phpcs:ignore |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int $instance_id . |
| 58 | * |
| 59 | * @return int |
| 60 | */ |
| 61 | public function get_shipping_method_order( int $instance_id ): int { |
| 62 | return (int) $this->wpdb->get_var( $this->wpdb->prepare( sprintf( 'SELECT `method_order` FROM `%s` WHERE `instance_id` = %%d ORDER BY `method_order` ASC', $this->get_table() ), $instance_id ) ); // phpcs:ignore |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param int $instance_id . |
| 67 | * |
| 68 | * @return bool|ShippingMethodSingle |
| 69 | */ |
| 70 | public function get_shipping_method( int $instance_id ) { |
| 71 | return WC_Shipping_Zones::get_shipping_method( $instance_id ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param int $instance_id . |
| 76 | * |
| 77 | * @return bool|WC_Shipping_Zone |
| 78 | */ |
| 79 | public function get_shipping_zone( int $instance_id ) { |
| 80 | return WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param int $instance_id . |
| 85 | * @param string $key . |
| 86 | * @param string $value . |
| 87 | * |
| 88 | * @return bool|int |
| 89 | */ |
| 90 | public function update_shipping_method_field( int $instance_id, string $key, string $value ) { |
| 91 | return $this->wpdb->update( $this->get_table(), [ $key => $value ], [ 'instance_id' => $instance_id ] ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return string |
| 96 | */ |
| 97 | private function get_table(): string { |
| 98 | return $this->wpdb->prefix . 'woocommerce_shipping_zone_methods'; |
| 99 | } |
| 100 | } |
| 101 |