英文原说明文件地址:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/文章源自原紫番博客-https://www.yuanzifan.com/54384.html
下面的代码实现的功能:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
让woo的CheckOut页面,国家、省市、邮编等字段从必选变为可选。文章源自原紫番博客-https://www.yuanzifan.com/54384.html
将下列代码放进functions.php中即可实现:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
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;
}
下面具体说明下相关的参数:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
checkout类将已加载的字段添加到其“ checkout_fields”数组中,并添加其他一些字段,例如“订单备注”。文章源自原紫番博客-https://www.yuanzifan.com/54384.html
$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')
)
);
覆盖原有的设置,如改属性,改名字等,下面代码改订单备注:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
// 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;
}
把订单的备注栏删掉的代码:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
// 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;
}
下面是常用的参数:文章源自原紫番博客-https://www.yuanzifan.com/54384.html
全部CheckOut页面的参数:woocommerce_checkout_fields:
- Billing
billing_first_namebilling_last_namebilling_companybilling_address_1billing_address_2billing_citybilling_postcodebilling_countrybilling_statebilling_emailbilling_phone
- Shipping
shipping_first_nameshipping_last_nameshipping_companyshipping_address_1shipping_address_2shipping_cityshipping_postcodeshipping_countryshipping_state
- Account
account_usernameaccount_passwordaccount_password-2
- Order
order_comments
以上所有参数均有以下属性
type– 类别,文本,文区域,或者选项,当是选项的时候要设置选项label– 名字placeholder– 默认选项,或者说文本里面默认的提示class– 类别required– 是否必填,有true和false两个选项clear– true or false, applies a clear fix to the field/labellabel_class– class for the label elementoptions– 如果是选择框,这里要填入选项,格式:(key => value pairs)
某些特殊情况下,要使用 woocommerce_default_address_fields 过滤器. 改过滤器可操作以下账单相关的字段。
countryfirst_namelast_namecompanyaddress_1address_2citystatepostcode












评论