<?php

defined( 'ABSPATH' ) || exit;

// Exit if cities and zones feature is enabled
if(get_option( 'wcsm_cities_zones') === 'enabled'){
    return;
}

/**
 * Register the custom shipping method class
 */
add_action( 'woocommerce_shipping_init', 'wcsm_custom_shipping_method_init' );
function wcsm_custom_shipping_method_init() {
    class WCSM_Custom_Method extends WC_Shipping_Method {
        public function __construct() {
            $this->id                 = 'wcsm_shipping'; // ID for your method
            $this->method_title       = __( 'WCSM Custom Shipping' ,'shipping-manager-for-woocommerce');  // Title shown in admin
            $this->method_description = __( 'Custom shipping based on city selection','shipping-manager-for-woocommerce' ); // Description shown in admin
            $this->enabled            = "yes"; 
            $this->init();
        }

        function init() {
            $this->init_form_fields();
            $this->init_settings();
            $this->title = __('Shipping Cost', 'shipping-manager-for-woocommerce'); // Title shown to customer
            add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
        }

        public function calculate_shipping( $package = array() ) {
            // We set cost to 0 initially; your filter below will override it
            $this->add_rate( array(
                'id'    => $this->id,
                'label' => $this->title,
                'cost'  => 0,
            ) );
        }
    }
}

/**
 * Add the method to WooCommerce
 */
add_filter( 'woocommerce_shipping_methods', 'wcsm_add_custom_shipping_method' );
function wcsm_add_custom_shipping_method( $methods ) {
    $methods['wcsm_shipping'] = 'WCSM_Custom_Method';
    return $methods;
}

