在woocommerce中添加新的自定义送货字段

时间:2022-10-25 11:11:03

I'd like to add a new custom billing field in my woocommerce checkout. After adding the following code inside my child theme functions.php, saved it and refreshed the page, I received a blank page. There was no error message. I believe there must be syntax error or something.

我想在我的woocommerce结帐中添加一个新的自定义结算字段。在我的子主题functions.php中添加以下代码,保存并刷新页面后,我收到一个空白页面。没有错误消息。我相信必须有语法错误或其他东西。

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
   $fields['shipping']['about_yourself'] = array(
      'label' => __('About yourself', 'woocommerce'),
      'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
      'required' => false,
      'class' => array('form-row-wide');
      'clear' => true
   );
   return $fields;
}

I was wondering if anyone could help me out.

我想知道是否有人可以帮助我。

Thank you in advance

先感谢您

2 个解决方案

#1


The error is in your array field that should be like:

错误在您的数组字段中应该是这样的:

$fields['shipping']['about_yourself'] = array(
  'label' => __('About yourself', 'woocommerce'),
  'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
  'required' => false,
  'class' => array('form-row-wide'), 
  'clear' => true
);

NOTE: During development process to enable the error_reporting() and set WP_DEBUG true. This is how you enable it during WordPress development in wp-config.php file

注意:在开发过程中启用error_reporting()并将WP_DEBUG设置为true。这是在wp-config.php文件中的WordPress开发期间启用它的方法

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

#2


You have added ; instead of , in 'class' => array('form-row-wide');

你添加了;代替,在'class'=> array('form-row-wide');

So your code would be :

所以你的代码是:

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
    function custom_override_checkout_fields($fields) {
       $fields['shipping']['about_yourself'] = array(
          'label' => __('About yourself', 'woocommerce'),
          'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
          'required' => false,
          'class' => array('form-row-wide'),
          'clear' => true
       );
       return $fields;
}

#1


The error is in your array field that should be like:

错误在您的数组字段中应该是这样的:

$fields['shipping']['about_yourself'] = array(
  'label' => __('About yourself', 'woocommerce'),
  'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
  'required' => false,
  'class' => array('form-row-wide'), 
  'clear' => true
);

NOTE: During development process to enable the error_reporting() and set WP_DEBUG true. This is how you enable it during WordPress development in wp-config.php file

注意:在开发过程中启用error_reporting()并将WP_DEBUG设置为true。这是在wp-config.php文件中的WordPress开发期间启用它的方法

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

#2


You have added ; instead of , in 'class' => array('form-row-wide');

你添加了;代替,在'class'=> array('form-row-wide');

So your code would be :

所以你的代码是:

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
    function custom_override_checkout_fields($fields) {
       $fields['shipping']['about_yourself'] = array(
          'label' => __('About yourself', 'woocommerce'),
          'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
          'required' => false,
          'class' => array('form-row-wide'),
          'clear' => true
       );
       return $fields;
}