繁体   English   中英

WooCommerce:在结账时将账单地址字段的输入大写 + 更新订单元数据?

[英]WooCommerce: Capitalize input to the billing address field at checkout + update the order meta?

我被要求在结帐字段中添加格式代码,以在客户锁定大写字母或输入全部小写字母时强制更改标题大小写,因为这些值需要传递到客户的 Quickbooks 帐户适当的大写。 截至目前,除了地址字段外,一切正常,可能是因为数字在大多数条目之前。 当在结帐页面上输入全部小写时,大小写更改有效,但一旦下订单,它就会按照我输入的方式显示。 这是我到目前为止所拥有的:

function wc_checkout_alter_field_input() {
    if (is_checkout()):
        if (!wp_script_is('jquery', 'done')) {
            wp_enqueue_script('jquery');
        }

        wp_add_inline_script('wc-checkout', 'jQuery(document).ready(function($){ $("#billing_address_1").keyup(function() { $(this).val($(this).val().substr(0, 1).toUpperCase() + $(this).val().substr(1).toLowerCase());});});');
    

add_action('wp_enqueue_scripts', 'wc_checkout_alter_field_input');
#billing_address_1 {
    text-transform: capitalize;
}

我尝试使用我为所有结帐字段指定的代码。 只有这个我有问题,因为数字和字母都需要作为地址输入。 我试图强制提交的订单将地址显示为标题大小写(123 Example St)。

我猜想我需要在某处添加正则表达式,但我不熟悉它,如有必要,我将不胜感激任何关于如何使用它的见解 go 。

如果您关心这些值是如何保存在数据库中的,您需要将自定义代码添加到您的网站,以连接到保存结帐字段的操作,即 woocommerce_checkout_posted_data 在这里找到。

add_filter('woocommerce_checkout_posted_data', 'my_custom_woocommerce_checkout_posted_data');
function my_custom_woocommerce_checkout_posted_data($data){
  // The data posted by the user comes from the $data param. 
  // You would look for the fields being posted, 
  // like "billing_first_name" and "billing_last_name"

  if($data['billing_first_name']){
    /*
      From hello world to Hello World
    */
    $data['billing_first_name'] = ucwords($data['billing_first_name']);
  }

  if($data['billing_last_name']){
    $data['billing_last_name'] = ucwords($data['billing_last_name']);
  }

  return $data;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM