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

英文原说明文件地址:文章源自原紫番博客-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

  1. add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  2.  
  3. // Our hooked in function - $fields is passed via the filter!
  4. function custom_override_checkout_fields( $fields ) {
  5. $fields['billing']['billing_first_name']['required'] = true;
  6. $fields['billing']['billing_last_name']['required'] = true;
  7. $fields['billing']['billing_country']['required'] = false;
  8. $fields['billing']['billing_address_1']['required'] = false;
  9. $fields['billing']['billing_address_2']['required'] = false;
  10. $fields['billing']['billing_city']['required'] = false;
  11. $fields['billing']['billing_postcode']['required'] = false;
  12. $fields['billing']['billing_country']['required'] = false;
  13. $fields['billing']['billing_state']['required'] = false;
  14. return $fields;
  15. }

下面具体说明下相关的参数:文章源自原紫番博客-https://www.yuanzifan.com/54384.html

checkout类将已加载的字段添加到其“ checkout_fields”数组中,并添加其他一些字段,例如“订单备注”。文章源自原紫番博客-https://www.yuanzifan.com/54384.html

  1. $this->checkout_fields['billing'] = $woocommerce->countries->get_address_fields( $this->get_value('billing_country'), 'billing_' );
  2. $this->checkout_fields['shipping'] = $woocommerce->countries->get_address_fields( $this->get_value('shipping_country'), 'shipping_' );
  3. $this->checkout_fields['account'] = array(
  4. 'account_username' => array(
  5. 'type' => 'text',
  6. 'label' => __('Account username', 'woocommerce'),
  7. 'placeholder' => _x('Username', 'placeholder', 'woocommerce')
  8. ),
  9. 'account_password' => array(
  10. 'type' => 'password',
  11. 'label' => __('Account password', 'woocommerce'),
  12. 'placeholder' => _x('Password', 'placeholder', 'woocommerce'),
  13. 'class' => array('form-row-first')
  14. ),
  15. 'account_password-2' => array(
  16. 'type' => 'password',
  17. 'label' => __('Account password', 'woocommerce'),
  18. 'placeholder' => _x('Password', 'placeholder', 'woocommerce'),
  19. 'class' => array('form-row-last'),
  20. 'label_class' => array('hidden')
  21. )
  22. );
  23. $this->checkout_fields['order'] = array(
  24. 'order_comments' => array(
  25. 'type' => 'textarea',
  26. 'class' => array('notes'),
  27. 'label' => __('Order Notes', 'woocommerce'),
  28. 'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
  29. )
  30. );

覆盖原有的设置,如改属性,改名字等,下面代码改订单备注:文章源自原紫番博客-https://www.yuanzifan.com/54384.html

  1. // Hook in
  2. add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  3.  
  4. // Our hooked in function - $fields is passed via the filter!
  5. function custom_override_checkout_fields( $fields ) {
  6. $fields['order']['order_comments']['placeholder'] = 'My new placeholder';
  7. $fields['order']['order_comments']['label'] = 'My new label';
  8. return $fields;
  9. }

把订单的备注栏删掉的代码:文章源自原紫番博客-https://www.yuanzifan.com/54384.html

  1. // Hook in
  2. add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
  3.  
  4. // Our hooked in function - $fields is passed via the filter!
  5. function custom_override_checkout_fields( $fields ) {
  6. unset($fields['order']['order_comments']);//unset是删掉
  7.  
  8. return $fields;
  9. }

下面是常用的参数:文章源自原紫番博客-https://www.yuanzifan.com/54384.html

全部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

 

 

 

站长微信
扫码添加(注明来意)
weinxin
Yuanzifan99
原梓番博客公众号
博客内容精选
weinxin
原梓番博客
 

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证
加载失败