Woocommerce Settings API 如何使用

优质
小牛编辑
126浏览
2023-12-01

Woocommerce Settings Api通常用于保存和读取Shipping methods(送货方式)和Payment gateways(支付网关)的设置。

定义选项

在构造函数中调用下列代码来定义选项

$this->init_form_fields();

选项具体有哪些内容应该提前定义

/**
 * Initialise Gateway Settings Form Fields
 */
 function init_form_fields() {
     $this->form_fields = array(
     'title' => array(
          'title' => __( 'Title', 'woocommerce' ),
          'type' => 'text',
          'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
          'default' => __( 'PayPal', 'woocommerce' )
          ),
     'description' => array(
          'title' => __( 'Description', 'woocommerce' ),
          'type' => 'textarea',
          'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ),
          'default' => __("Pay via PayPal; you can pay with your credit card if you don't have a PayPal account", 'woocommerce')
           )
     );
} // End init_form_fields()

这段代码定义了两个选项,title和description,分别是text field和textarea field。

定义具体选项的格式如下

'option_name' => array(
     'title' => 'Title for your option shown on the settings page',
     'description' => 'Description for your option shown on the settings page',
     'type' => 'text|password|textarea|checkbox|select|multiselect',
     'default' => 'Default value for the option',
     'class' => 'Class for the input',
     'css' => 'CSS rules added line to the input',
     'label' => 'Label', // checkbox only
     'options' => array(
          'key' => 'value'
     ) // array of options for select/multiselects only
)

在后台显示管理员选项

创建admin_options方法,并包含如下代码

function admin_options() {
 ?>
 <h3><?php _e('You plugin name','woocommerce'); ?></h3>
 <table class="form-table">
 <?php $this->generate_settings_html(); ?>
 </table> <?php
 }

该方法会输出相应的管理员选项。

保存管理员选项

在构造函数中通过钩子函数来保存选项。

保存支付方式的选项(Payment gateways)

add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));

保存送货方式的选项(Shipping method)

add_action('woocommerce_update_options_shipping_methods', array(&$this, 'process_admin_options'));

如何使用保存的设置

在构造函数中初始化设置,调用下面的代码可以给settings成员赋值

// Load the settings.
$this->init_settings();

之后就可以访问定义的设置了,例如获取title和description的值,只需要访问$this->settings

// Define user set variables
 $this->title = $this->settings['title'];
 $this->description = $this->settings['description'];

创建不同类型的选项的代码示例

textarea

'my_textarea' =>  array(
	'title' =>	__('Textarea示例'),
	'type' => 'textarea',
	'description' => __('一个textarea的例子'),
	'css'=>'width:400px; height:200px'
)

select

'my_textarea' =>  array(
	'title' =>	__('Select下拉菜单示例'),
	'type' => 'select',
	'options' => array(
		'option value 1' => 'option label 1',
		'option value 2' => 'option label 2',
		'option value 3' => 'option label 3'
	),
	'description' => __('一个select下拉菜单的例子'),
)

multiselect多选下拉菜单

'my_textarea' =>  array(
    'title' =>    __('多选Select下拉菜单示例'),
    'type' => 'multiselect',
    'options' => array(
        'option value 1' => 'option label 1',
        'option value 2' => 'option label 2',
        'option value 3' => 'option label 3'
    ),
    'description' => __('一个多选select下拉菜单的例子'),
)

checkbox

'my_textarea' =>  array(
	'title' =>	__('checkbox示例'),
	'type' => 'checkbox',
	'label' => '选项名称',
	'description' => __('一个checkbox的例子')
)

password

'my_textarea' =>  array(
	'title' =>	__('Password示例'),
	'type' => 'password',
	'description' => __('一个密码输入框的的例子')
)