如何从WooCommerce管理员输入字段中获取值?

时间:2017-03-05 作者:Donatas

我在后端添加了几个输入字段:

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();
这是它在后端的外观:https://ibb.co/cNd7BF

我唯一的问题是如何用php代码从输入字段中检索这些值?

1 个回复
最合适的回答,由SO网友:Faysal Mahamud 整理而成

请试着理解代码。那里发生了什么。

update_settings 函数更新WordPress选项表中的所有表单设置值。因此,如果在选项表中搜索,您会发现设置值存储在中的选项表中DB.

所以现在很容易检索选项表的值。So使用get_option 作用你可以在任何需要的地方使用它。请参见下文。

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

相关推荐