US allowed States through Woocommerce custom setti

2019-07-25 03:19发布

I have a function that take the US state list and removes states from it that we don't sell to. This works fine and removes the states from several areas, when creating an order, billing & shipping state dropdowns, etc.

Here is that function.

function wc_sell_only_states($states) {

    $states_not_allowed['US'] = array(
        'AL' => __('Alabama', 'woocommerce'),
        'AR' => __('Arkansas', 'woocommerce'),
        'MD' => __('Maryland', 'woocommerce'),
        'MI' => __('Michigan', 'woocommerce'),
        'MO' => __('Missouri', 'woocommerce'),
        'MS' => __('Mississippi', 'woocommerce'),
        'NJ' => __('New Jersey', 'woocommerce'),
        'SC' => __('South Carolina', 'woocommerce'),
        'TX' => __('Texas', 'woocommerce'),
        'VA' => __('Virginia', 'woocommerce'),
        'WV' => __('West Virginia', 'woocommerce'),
        'AA' => __('Armed Forces', 'woocommerce'),
        'AE' => __('Armed Forces', 'woocommerce'),
        'AP' => __('Armed Forces', 'woocommerce'),
        // MANUALLY ADD STATES NOT ALLOWED HERE 'STATE_ABBREVIATION' => __('State_Name', 'woocommerce'),
    );

    foreach ($states['US'] as $key1 => $value) {
        foreach ($states_not_allowed['US'] as $key2 => $value) {
            if ($key1 === $key2) {
                unset($states['US'][$key1]);
            }
        }
    }

    return $states;
}

add_filter('woocommerce_states', 'wc_sell_only_states');

As you can see, when I need to make a change to the states not allowed list, I have to manually go in and add the state. To make this easier on the client, I was thinking of making an admin page that has a checkbox for all 50 states. I have the page created but not sure the of the best way to save the data and tie it back into my function.

Here I create the admin page...

function states_admin_page() {
    global $allowed_states;
    add_menu_page( __( 'States', 'states' ), __( 'States', 'states' ), 'edit_posts', 'add_data', 'my_states', 'dashicons-groups', 6 ) ;
}

add_action('admin_menu', 'states_admin_page');

Here is my callback function... (Not working)

function gemcore_states() {

    if(!empty($_POST)) {
        global $wpdb;
        $table = 'states';
        $data = array('selected' => $_POST['AA']);
        $where = array('code' => 'AA');
        $format = '%d';

        $wpdb->update($table, $data, $format, $where);

        if($success){
            echo 'data has been save' ; 
        }
    }else{
        ?>
        <form method="post">                
            <label>Armed Forces (AA): <input type="checkbox" name="AA" /></label><br />
            <!-- ... the rest of the states -->
            <input type="submit" value="submit">
        </form>
        <?php 
    }  
}

What is the best way to grab the checkbox values and save them to my database as 0 or 1? Then grab this data in my original function, wc_sell_only_states().

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-25 04:03

The following code will add To Woocommerce settings a submenu entry for "US states settings" page with a form containing checkboxes for all US states. Each state checked and validated will be available and allowed.

The settings data will be saved in wp_options table as all settings.

Here is the code:

// Add a submenu entry to Woocommerce menu settings
add_action('admin_menu', 'states_settings_admin_submenu');
function states_settings_admin_submenu() {
    $states = __( 'Edit states', 'woocommerce' );
    add_submenu_page('woocommerce', $states, $states, 'edit_shop_orders', 'edit', 'wc_states_setting_page');
}

// The content for the Woocommerce submenu entry  
function wc_states_setting_page() {
    $states         = array();
    $country_code   = 'US';

    include_once WC()->plugin_path() . '/i18n/states/' . $country_code . '.php';

    if( isset($_POST['submit_us_states']) ) {
        $new_states = [];
        foreach( $states[$country_code] as $code => $label ){
            $new_states[$code] = isset($_POST[$code]) && $_POST[$code] ? '1' : '';
        }
        update_option( 'allowed_us_states', $new_states );
    }

    $allowed = get_option( 'allowed_us_states' );

    // Styling
    echo '<style> .us-states li {width: 200px; float:left; display:block;} .us-states {max-width: 600px;} .us-states .button {float: right; margin-right: 135px;} </style>';

    // The form
    echo '<form method="post" class="us-states"><h1>'.__("Allowed US States settings").'</h1><ul>';

    foreach( $states[$country_code] as $code => $label ){
        $checked = isset($allowed[$code]) && $allowed[$code] ? ' checked="checked"' : '';
        echo '<li><label><input type="checkbox" name="'.$code.'"'.$checked.'> '.$label.'</label></li>';
    }

    echo '</ul><br><br><input type="submit" value="submit" name="submit_us_states" class="button alt"></form>';
}

// Filtering allowed US states
add_filter('woocommerce_states', 'allowed_us_states' );
function allowed_us_states( $states ) {
    if( $allowed = get_option( 'allowed_us_states' ) ){
        foreach( $allowed as $code => $boolean ){
            if( ! $boolean )
                unset($states['US'][$code]);
        }
    }
    return $states;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

enter image description here

enter image description here

查看更多
登录 后发表回答