Adding Custom Setting Options to WooCommerce with the WooCommerce Setting Api Interface
In the development of WooCommece themes or plug-ins, we often need to set some data, such as payment plug-ins, we need to save some of the payment gateway data to the database, at this time, we need to use the WooCommerce Settings interface to add some settings for the plug-ins, WooCommerce Settings Api is very simple to use and we commonly use WordPress fields plug-ins are very similar. WooCommerce Settings Api is very simple to use, and we often use WordPress fields plugin is very similar to the whole WooCommerce settings options are realized through the Settings Api. Let's take a look at how to use the WooCommerce Setup Api adds setup options.
定义表单字段
We can use the init_form_fields method to define set option fields in the class constructor:
$this->init_form_fields();加载设置之前,我们必须已经定义了该设置,定义设置的代码示例如下,我们只需要设置一个 form_fields 数组就可以了。
/**
* Initialization of gateway setup form fields
*/
function init_form_fields() {
$this->form_fields = array(
'title' => array(
'title' => __( 'title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Title of the payment gateway that the user will see when checking out', 'woocommerce' ), 'description' => __( 'Title of the payment gateway that the user will see when checking out', 'default' => __(
'default' => __( 'paypal payment', 'woocommerce' )
),
'description' => array(
'type' => 'textarea', 'description' => __( 'description', 'woocommerce' ), 'description' => array(
"description" => __( 'Description of the payment gateway that the user will see when checking out', 'woocommerce' ),
'default' => __( "Paying with Alipay is safe and convenient." , 'woocommerce')
)
);
}In the above sample code, we have defined two options - Title and Description. The title is a text field, the description is a multi-paragraph text field, note the above code to set the default value and options in the method, we can add a variety of form type options, the following is the format of the supported form types:
'option_name' => array(
'title' => 'Set the page's title',
'description' => 'Set the description of the page', 'type' => 'text|password|textarea|checkbox|select|multise
'type' => 'text|password|textarea|checkbox|select|multiselect', 'default' => 'Set the description of the page', 'option_name' => array(
'default' => 'Default value for the option', 'class' => 'input
'class' => 'CSS Class of the input box', 'css' => 'Input Box
'css' => 'CSS style of the input box', 'label' => 'Label', 'css' => 'CSS style of the input box', 'css' => '
'label' => 'Label', // only used if type is checkbox
'options' => array(
'key' => 'value'
)
)显示管理选项
Create a method named admin_options containing the following code:
function admin_options() {
? >
<h2><?php _e('插件名称','woocommerce'); ?></h2>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table>
<?php
}上面的代码将以正确的格式输出我们的设置。
保存管理选项
With the more setup options input screen, we also need to save the setup options to our database, simply by mounting our method of saving the data to the process_admin_options hook.WooCommerce Payment Gateway挂载到网关保存动作上:
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));;
add_action('woocommerce_update_options_shipping_methods', array(&$this, 'process_admin_options'));;加载设置
保存了设置后,我们就可以加载我们已经保存的设置了:
// Load the settings
$this->init_settings();Once the above is done, we can load the settings values from the settings API - the init_settings method above handles the settings values automatically.
// Define variables for user settings
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];Learning to add custom settings options to WooCommerce through the Settings API is a must for advanced development through WooCommerce, for the WooCommerce Development有兴趣,想在这方便深入发展的,可以深入了解尝试一下。如果在开发过程中遇到了问题,欢迎在评论中提出讨论。