How to get values from woocommerce admin input fields?

I have added a few input fields in the backend:

class WC_Settings_Tab_Demo {
    /**
     * Bootstraps the class and hooks required actions & filters.
     *
     */
    public static function init() {
        add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
        add_action( 'woocommerce_settings_tabs_settings_tab_demo', __CLASS__ . '::settings_tab' );
        add_action( 'woocommerce_update_options_settings_tab_demo', __CLASS__ . '::update_settings' );
        add_action('woocommerce_process_product_meta', __CLASS__ . '::process_meta');
    }


    /**
     * Add a new settings tab to the WooCommerce settings tabs array.
     *
     * @param array $settings_tabs Array of WooCommerce setting tabs & their labels, excluding the Subscription tab.
     * @return array $settings_tabs Array of WooCommerce setting tabs & their labels, including the Subscription tab.
     */
    public static function add_settings_tab( $settings_tabs ) {
        $settings_tabs['settings_tab_demo'] = __( 'Pricing', 'woocommerce-settings-tab-demo' );
        return $settings_tabs;
    }
    /**
     * Uses the WooCommerce admin fields API to output settings via the @see woocommerce_admin_fields() function.
     *
     * @uses woocommerce_admin_fields()
     * @uses self::get_settings()
     */
    public static function settings_tab() {
        woocommerce_admin_fields( self::get_settings() );
    }
    /**
     * Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function.
     *
     * @uses woocommerce_update_options()
     * @uses self::get_settings()
     */
    public static function update_settings() {
        woocommerce_update_options( self::get_settings() );
    }


        /**
     * Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function.
     *
     * @uses woocommerce_update_options()
     * @uses self::get_settings()
     */
    public static function process_meta() {

    }


    /**
     * Get all the settings for this plugin for @see woocommerce_admin_fields() function.
     *
     * @return array Array of settings for @see woocommerce_admin_fields() function.
     */
    public static function get_settings() {

    $settings[] = array( 'name' => __( 'Margin settings', 'text-domain' ), 'type' => 'title', 'desc' => __( 'The following options are to configure our margin on top of prices', 'text-domain' ), 'id' => 'wcslider' );

                $settings[] = array(
            'name'     => __( 'Canvas', 'text-domain' ),
            'desc_tip' => __( 'Margin profit in percents added on top of Canvases!', 'text-domain' ),
            'id'       => 'canvas',
            'type'     => 'text',
        );
        // Add second text field option
        $settings[] = array(
            'name'     => __( 'Framed Posters', 'text-domain' ),
            'desc_tip' => __( 'Margin profit in percents added on top of Framed Posters!', 'text-domain' ),
            'id'       => 'framed',
            'type'     => 'text',
        );

                    // Add second text field option
        $settings[] = array(
            'name'     => __( 'Posters', 'text-domain' ),
            'desc_tip' => __( 'Margin profit in percents added on top of Posters!', 'text-domain' ),
            'id'       => 'posters',
            'type'     => 'text',
        );

        $settings[] = array( 'type' => 'sectionend', 'id' => 'posters' );


        return apply_filters( 'wc_settings_tab_demo_settings', $settings );
    }
}
WC_Settings_Tab_Demo::init();

this is how it looks in the backend: https://ibb.co/cNd7BF

My only question is how to retrieve these values from input field with php code?

1 Answer
1

Please try to understand the code. What happened there.

update_settings function update all the form settings value in the WordPress options table. So if you search in options table you will find that the settings value are stored in the options table in DB.

So now its very easy to retrieve the value of options table. So use get_option function. You can use this anywhere you need. Please see below.

<?php print get_option( 'canvas', true );?> //23 
<?php print get_option( 'posters', true );?> //233
<?php print get_option( 'framed', true );?> //21

Leave a Comment