WooCommerce定义了许多钩子,我们可以利用这些钩子完成一些自定义的功能
- woocommerce_default_address_fields:下单地址过滤器钩子
- woocommerce_checkout_fields :checkout所有信息过滤器钩子
- woocommerce_before_checkout_billing_form :
- woocommerce_after_checkout_billing_form
- woocommerce_before_checkout_registration_form
- woocommerce_after_checkout_registration_form
- woocommerce_before_checkout_shipping_form
- woocommerce_after_checkout_shipping_form
- woocommerce_before_order_notes
- woocommerce_after_order_notes
用途
- 禁用某个地址字段
add_filter( 'woocommerce_default_address_fields' , 'disable_address_fields_validation' ); function disable_address_fields_validation( $address_fields_array ) { unset( $address_fields_array['state']['validate']); unset( $address_fields_array['postcode']['validate']); unset( $address_fields_array['first_name']['validate']); unset( $address_fields_array['company']['validate']); unset( $address_fields_array['country']['validate']); unset( $address_fields_array['city']['validate']); unset( $address_fields_array['address_1']['validate']); unset( $address_fields_array['address_2']['validate']); return $address_fields_array; }
- 禁用某个字段
add_filter( 'woocommerce_checkout_fields ' , 'disable_checkout_fields_validation' ); function disable_checkout_fields_validation( $woo_checkout_fields_array ) { unset( $woo_checkout_fields_array['billing']['billing_phone']['validate'] ); unset( $woo_checkout_fields_array['billing']['billing_company'] ); // remove company field unset( $woo_checkout_fields_array['billing']['billing_country'] ); unset( $woo_checkout_fields_array['billing']['billing_address_1'] ); unset( $woo_checkout_fields_array['billing']['billing_address_2'] ); unset( $woo_checkout_fields_array['billing']['billing_city'] ); unset( $woo_checkout_fields_array['billing']['billing_state'] ); // remove state field unset( $woo_checkout_fields_array['billing']['billing_postcode'] ); return $woo_checkout_fields_array; }
- 设置某个字段必填/非必填
add_filter( 'woocommerce_checkout_fields' , 'misha_not_required_fields', 9999 ); function misha_not_required_fields( $checkout_field ) { // 将字段设置为非必填 unset( $checkout_field['billing']['billing_last_name']['required'] ); // 这就是重点 unset( $checkout_field['billing']['billing_phone']['required'] ); // 将字段设置为必填 // $checkout_field['billing']['billing_company']['required'] = true; return $checkout_field; }
- 更改标签名和占位符
add_filter( 'woocommerce_checkout_fields' , 'update_labels_placeholders', 9999 ); function update_labels_placeholders( $checkout_field ) { // first name can be changed with woocommerce_default_address_fields as well $checkout_field['billing']['billing_first_name']['label'] = 'new label'; $checkout_field['order']['order_comments']['placeholder'] = 'new placeholder'; return $checkout_field; }