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' => __('一个密码输入框的的例子')
)

17条留言

      1. 淘宝 系统里那样的。形如下面这样的。这样,假如有10个商品,不用挨个设置了,直接选择是A还是B。

        运费模板A
        |目的地 | 运费首重 | 续重
        |北京 | 8 元 | 1
        |天津 | 10 | 2
        |
        |
        运费模板B全国
        |目的地 | 运费首重 | 续重
        |北京 | 8 元 | 0
        |天津 | 8 | 0
        | 上海 | 8元 |
        |

        1. 用flat rate就可以实现不带续重的功能,选模版和续重woocommerce不支持,可以找找插件。

  1. Hi Sola,

    It’s slow when i access woocommerce site, is woo call some google APIs ? How can i stop it
    i am in mainland in china.

    minshin

  2. Hi Sola,

    I want to display only the order number when customer pay with paypal, do you have a solution of it?

    Thanks!

    1. Hi,
      Do you mean that you want to display only the order number on the “Order Received” page?
      If I got you right, my thought is that you can copy the template of “Order Received” page to your theme folder and delete anything you don’t need. This is a safe way to override a woocommerce page template.
      Here is a tutorial to show you how:
      http://www.foxrunsoftware.net/articles/wordpress/how-to-override-woocommerce-template-files/
      And the template file you need to modify is wp-content/plugins/woocommerce/templates/checkout/thankyou.php

  3. 你好!
    First of all thank you for the code below. It works well. But now I encountered a different problem. When paying via paypal only the price of the product will be converted to USD, not the shipping costs. These are stated as an additional item and are not converted from RMB. How is it possible to convert the order total (product + shipping costs) from RMB to USD?
    Would be great if you could help me.

    Thanks
    Rob

    1. Hi,
      This is all about paypal args, please try the code below

      add_filter('woocommerce_paypal_args', 'convert_rmb_to_usd');
      function convert_rmb_to_usd($paypal_args){
          if ( $paypal_args['currency_code'] == 'RMB'){
              $convert_rate = 6.2116; //Set converting rate
              $paypal_args['amount_1'] = round( $paypal_args['amount_1'] / $convert_rate, 2); //Convert product price
              $paypal_args['amount_2'] = round( $paypal_args['amount_2'] / $convert_rate, 2); //Convert shipping costs
          }
          return $paypal_args;
      }
      

      The filter ” woocommerce_paypal_args” allows one to have full control over the paypal args that are sent to paypal, just make any changes you want before sending.

  4. Im using the Chinese currency settings in WooCommerce and finding the numbers tagged correctly. It looks like you have the same issue I have.

    Im having an issue with exporting shopping carts in Chinese Yuan to Paypal. When Paypal receives the CNY number, it charges the amount in the equal number, but in US dollars. For example, the shopping cart is in 75 CNY, then the paypal charges 75 USD.

    Can you tell me how you were able to resolve this issue? lkbgift(at)gmail(dot)com

    1. Hi,
      I am not running an WooCommerce website right now, but I believe the solution is to convert the price before sending it to paypal, so please try the code below and let me know the result. The code goes into your theme’s functions.php

      add_filter('woocommerce_paypal_args', 'convert_rmb_to_usd');
      function convert_rmb_to_usd($paypal_args){
          if ( $paypal_args['currency_code'] == 'RMB'){
              $convert_rate = 6.2116;
              $paypal_args['amount_1'] = round( $paypal_args['amount_1'] / $convert_rate, 2);
          }
          return $paypal_args;
      }
      

      $convert_rate = 6.2116 is the rate used to convert rmb to usd, set it to the rate you wish.

  5. Hi Sola. Did you ever find a solution to the RMB/Woocommerce currency conversion issue with Paypal that didn’t involve Multilingual?

    Thanks for the great article 🙂

    1. I think the key problem is that paypal doesn’t support Chinese Yuan, so the RMB price must be converted to currencies that supported by Paypal. On the other hand, if RMB is chosen,  that might suggest the potential customers are from China, then paypal is not the most preferable payment method, you might want to try alipay since it is widely used in China.

评论功能已关闭