用钩子函数处理Woo商城结算页地址栏的必填字段

2019/10/2812:16:57 评论 62 views

英文原说明文件地址:

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

下面的代码实现的功能:

让woo的CheckOut页面,国家、省市、邮编等字段从必选变为可选。

将下列代码放进functions.php中即可实现:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
	$fields['billing']['billing_first_name']['required'] = true;
	$fields['billing']['billing_last_name']['required'] = true;
	$fields['billing']['billing_country']['required'] = false;
	$fields['billing']['billing_address_1']['required'] = false;
	$fields['billing']['billing_address_2']['required'] = false;
	$fields['billing']['billing_city']['required'] = false;
	$fields['billing']['billing_postcode']['required'] = false;
	$fields['billing']['billing_country']['required'] = false;
	$fields['billing']['billing_state']['required'] = false;
	return $fields;
}

下面具体说明下相关的参数:

checkout类将已加载的字段添加到其“ checkout_fields”数组中,并添加其他一些字段,例如“订单备注”。

$this->checkout_fields['billing']    = $woocommerce->countries->get_address_fields( $this->get_value('billing_country'), 'billing_' );
$this->checkout_fields['shipping']   = $woocommerce->countries->get_address_fields( $this->get_value('shipping_country'), 'shipping_' );
$this->checkout_fields['account']    = array(
    'account_username' => array(
        'type' => 'text',
        'label' => __('Account username', 'woocommerce'),
        'placeholder' => _x('Username', 'placeholder', 'woocommerce')
        ),
    'account_password' => array(
        'type' => 'password',
        'label' => __('Account password', 'woocommerce'),
        'placeholder' => _x('Password', 'placeholder', 'woocommerce'),
        'class' => array('form-row-first')
        ),
    'account_password-2' => array(
        'type' => 'password',
        'label' => __('Account password', 'woocommerce'),
        'placeholder' => _x('Password', 'placeholder', 'woocommerce'),
        'class' => array('form-row-last'),
        'label_class' => array('hidden')
        )
    );
$this->checkout_fields['order']  = array(
    'order_comments' => array(
        'type' => 'textarea',
        'class' => array('notes'),
        'label' => __('Order Notes', 'woocommerce'),
        'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
        )
    );

覆盖原有的设置,如改属性,改名字等,下面代码改订单备注:

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['order']['order_comments']['placeholder'] = 'My new placeholder';
     $fields['order']['order_comments']['label'] = 'My new label';
     return $fields;
}

把订单的备注栏删掉的代码:

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     unset($fields['order']['order_comments']);//unset是删掉

     return $fields;
}

下面是常用的参数:

全部CheckOut页面的参数:woocommerce_checkout_fields:

  • Billing
    • billing_first_name
    • billing_last_name
    • billing_company
    • billing_address_1
    • billing_address_2
    • billing_city
    • billing_postcode
    • billing_country
    • billing_state
    • billing_email
    • billing_phone
  • Shipping
    • shipping_first_name
    • shipping_last_name
    • shipping_company
    • shipping_address_1
    • shipping_address_2
    • shipping_city
    • shipping_postcode
    • shipping_country
    • shipping_state
  • Account
    • account_username
    • account_password
    • account_password-2
  • Order
    • order_comments

以上所有参数均有以下属性

  • type – 类别,文本,文区域,或者选项,当是选项的时候要设置选项
  • label – 名字
  • placeholder – 默认选项,或者说文本里面默认的提示
  • class – 类别
  • required – 是否必填,有true和false两个选项
  • clear – true or false, applies a clear fix to the field/label
  • label_class – class for the label element
  • options – 如果是选择框,这里要填入选项,格式:(key => value pairs)

某些特殊情况下,要使用 woocommerce_default_address_fields 过滤器. 改过滤器可操作以下账单相关的字段。

  • country
  • first_name
  • last_name
  • company
  • address_1
  • address_2
  • city
  • state
  • postcode

 

 

 

历史上的今天
十月
28
原梓番主讲Excel进阶与提升视频课(淘宝在线观看)
Wordpress建站问题咨询解决
PPT精品模板
Excel表格制作

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: