Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    There are lots of plugin tutorials out there for creating option screens – you’d just need to use the same option names WooCommerces uses to store this information.

    Thread Starter rkvisit

    (@rkvisit)

    hi mike,
    i already created the menu page but couldn’t get the fields to show up. can you simply give me an example.Currently my code is as follows.

    <?php
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    // Hook for adding admin menus
    add_action('admin_menu', 'mt_add_pages');
    
    // action function for above hook
    function mt_add_pages() {
       add_menu_page(__('TNT Settings','menu-test'), __('TNT Settings','tnt-shipping'), 'manage_options', 'tnt_Settings', 'tnt_Settings_page',plugins_url('liftingOnlineShipping/assests/images/menu-icon.png'), 150 );
    }
    
    // mt_toplevel_page() displays the page content for the custom Test Toplevel menu
    function tnt_Settings_page() {
        echo "<h2>" . __( 'TNT Settings', 'tnt-shipping' ) . "</h2>";
    	//must check that the user has the required capability
        if (!current_user_can('manage_options'))
        {
          wp_die( __('You do not have sufficient permissions to access this page.') );
        }
    	function init() {
    					// Load the settings API
    					$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
    					$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
    					// Define user set variables
    					$this->password		= $this->get_option('Password');
    					$this->username		= $this->get_option('User Name');
    					$this->acntno		= $this->get_option('Account No');
    					$this->shippingfrom	= $this->get_option('shippingfrom');
    					$this->shippingpost	=$this->get_option('shippingpost');
    					$this->shippingcountry=$this->get_option('shippingcountry');
    					$this->shippingstate= $this->get_option('shippingstate');
    					$this->title        = $this->get_option( 'title' );
    					$this->availability = $this->get_option( 'availability' );
    					$this->countries    = $this->get_option( 'countries' );
    					$this->tax_status   = $this->get_option( 'tax_status' );
    					$this->cost         = $this->get_option( 'cost' );
    					$this->type         = $this->get_option( 'type', 'class' );
    					$this->options      = $this->get_option( 'options', false );
    
    				}
    	function init_form_fields() {
    					$this->form_fields = include( dirname(__FILE__).'/'.'includes/settings-flat-rate.php' );
    	}
    
    }

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I suggest you don’t bother at all with the shipping classes in wc – just have your OWN form fields and update the option for the shipping method’s settings. This is just an array of option names and values stored in a single option.

    Thread Starter rkvisit

    (@rkvisit)

    hi thanks for quick reply,
    the thing is i added my own form to this page as you said. i’ve already created my option values and they can be changed by going to woocommerce->settings->shipping->my shippin method.but my concern is show them in a different menu page so if i need to update the fields i can do it via this menu page(feels its easy for the client and safer).how to call their values to my page.if i need to show get_option(‘password’) it gives me null value is there any prefixes i need to use?? like get_option(‘woocommerce_lftonline__rate_reg_username’).I tried like this but still failed to load the value to the text box.

    function tnt_Settings_page() {
    
        //must check that the user has the required capability
        if (!current_user_can('manage_options'))
        {
          wp_die( __('You do not have sufficient permissions to access this page.') );
        }
    
        // variables for the field and option names
        $opt_name = 'woocommerce_lftonline__rate_reg_username';
        $hidden_field_name = 'mt_submit_hidden';
        $data_field_name = 'woocommerce_lftonline__rate_reg_username';
    
        // Read in existing option value from database
        $opt_val = get_option( $opt_name );
    
        // See if the user has posted us some information
        if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
            // Read their posted value
            $opt_val = $_POST[ $data_field_name ];
    
            // Save the posted value in the database
            update_option( $opt_name, $opt_val );
    
            // Put a "settings saved" message on the screen
    
    ?>
    <div class="updated"><p><strong><?php _e('settings saved.', 'tnt-shipping' ); ?></strong></p></div>
    <?php
    
        }
    
        // Now display the settings editing screen
    
        echo '<div class="wrap">';
    
        // header
    
        echo "<h2>" . _e( 'TNT Settings', 'tnt-shipping' ) . "</h2>";
    
        // settings form
    
        ?>
    
    <form name="form1" method="post" action="">
    <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
    
    <p><?php _e("Your User Name:", 'tnt-shipping' ); ?>
    <input type="text" name="<?php echo $data_field_name; ?>" id="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
    </p><hr />
    
    <p class="submit">
    <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
    </p>
    
    </form>
    </div>
    
    <?php
    
        }
    
        // Now display the settings editing screen
    
        echo '<div class="wrap">';
    
        // header
    
        echo "<h2>" . __( 'TNT Settings', 'tnt-shipping' ) . "</h2>";
    
        // settings form
    
        ?>
    
    <form name="form1" method="post" action="">
    <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
    
    <p><?php _e("Your User Name:", 'tnt-shipping' ); ?>
    <input type="text" name="<?php echo $data_field_name; ?>" id="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
    </p><hr />
    
    <p class="submit">
    <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
    </p>
    
    </form>
    </div>
    
    <?php
    
    }

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Shipping settings are stored in for example, woocommerce_flat_rate_settings. It’s serialised.

    Thread Starter rkvisit

    (@rkvisit)

    hi Mike,
    thanks for your guidance. achieved it!
    had to write a function to update the values this was just a test hope this will be helpful if anyone needed to get this done.

    function tnt_admin_notices() {
    	echo "<div id='notice' class='updated fade'><p>You need to configure your TNT Settings</p></div>\n";
    }
    // mt_settings_page() displays the page content for the Test Settings submenu
    function tnt_Settings_page() {
    
        //must check that the user has the required capability
        if (!current_user_can('manage_options'))
        {
          wp_die( __('You do not have sufficient permissions to access this page.') );
        }
    
    	$lifting_rate_settings	= maybe_unserialize( get_option( 'woocommerce_lftonline_settings' ) );
    	mail('[email protected]','wait',print_r($lifting_rate_settings,true));
        // variables for the field and option names
        $opt_name = $lifting_rate_settings['username'];
    	//echo $opt_name."hi";
        $hidden_field_name = 'mt_submit_hidden';
        $data_field_name = 'woocommerce_lftonline_username';
    
        // Read in existing option value from database
        //$opt_val = get_option( $opt_name );
        $opt_val = $opt_name;
    
        // See if the user has posted us some information
        // If they did, this hidden field will be set to 'Y'
        if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
            // Read their posted value
            $opt_val = $_POST[ $data_field_name ];
    
            // Save the posted value in the database
            update_option( $data_field_name, $opt_val );
    		alt_update_option('username',$opt_val,'woocommerce_lftonline_settings');
    
            // Put a "settings saved" message on the screen
    
    ?>
    <div class="updated"><p><strong><?php _e('settings saved.', 'tnt-shipping' ); ?></strong></p></div>
    <?php
    
        }
    
        // Now display the settings editing screen
    
        echo '<div class="wrap">';
    
        // header
    
        echo "<h2>" . __( 'TNT Settings', 'tnt-shipping' ) . "</h2>";
    
        // settings form
    
        ?>
    
    <form name="form1" method="post" action="">
    <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
    
    <p><?php _e("Your User Name:", 'tnt-shipping' ); ?>
    <input type="text" name="<?php echo $data_field_name; ?>" id="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
    </p><hr />
    
    <p class="submit">
    <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
    </p>
    
    </form>
    </div>
    
    <?php
    
    }
    //update serialized data
    function alt_update_option($opt_key,$opt_val,$opt_group){
        // get options-data as it exists before update
    	$options	= maybe_unserialize( get_option( $opt_group ) );
    
        // update it
        $options[$opt_key] = $opt_val;
        // store updated data
        update_option($opt_group,$options);
    }

    Thread Starter rkvisit

    (@rkvisit)

    oh! some lines there are only for testing.mail and print_r not needed!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add shipping settings as a seperate admin menu’ is closed to new replies.