WooCommerce - 签出不同人自定义状态的条件字段

2023-12-14

我需要修改 woocommerce 网站的结账流程。该过程由人员的状态决定,可能是以下之一:
- 'legal entity'
- 'individual'

我需要一个选择器'User status' (或单选按钮),就在“billing_first_name”和“billing_last_name”之后。

选择器应该这样工作:

  1. If 'legal entity' is selected, it should display 3 fields:
    • '电话号码'
    • 'email'
    • '序列号'
  2. If 'individual' is selected, it should display 3 other fields:
    • '自定义字段1'
    • '自定义字段2'
    • '自定义字段3'

我尝试了 WooCommerce Checkout Manager 和另一个插件,但问题是我无法制定匹配条件。

我怎样才能实现这个目标?

Thanks.


对于 WooCommerce 3+(update):

Since WooCommerce 3.0 结账字段发生了一些变化,因此不可能像以前那样对字段进行重新排序。

有一个新的'优先事项'争论处理字段顺序,也适用于结帐字段和我的帐户字段。

下面我只是更新与订购字段相关的部分:

## 3. Ordering the billing fields

// Set the order of the fields
$billing_fields_new_order = array(
    'billing_first_name', 'billing_last_name', 'billing_status',
    'billing_email',      'billing_phone',     'billing_serial_id',
    'billing_custom1',    'billing_custom2',   'billing_custom3',
    'billing_company',    'billing_address_1', 'billing_address_2',
    'billing_postcode',   'billing_city',      'billing_country',
);

$count = 0;
$priority = 10;

// Updating the 'priority' argument
foreach($billing_fields_new_order as $field_name){
    $count++;
    $fields['billing'][$field_name]['priority'] = $count * $priority;
}

// END: returning the customized checkout fields
return $fields;

参考: 在 WooCommerce 3 中重新排序结账字段


原答案:

你需要使用woocommerce_checkout_fields钩。然后首先创建新字段。自定义一些字段类后。完成后重新排序字段以满足您的愿望。

这是代码:

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_billing_fields' );
function custom_checkout_billing_fields( $fields ) {

// 1. Creating the additional custom billing fields

    // The "status" selector
    $fields['billing']['billing_status']['type'] = 'select';
    $fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
    $fields['billing']['billing_status']['required'] = true;
    $fields['billing']['billing_status']['label'] = __('User status', 'my_theme_slug');
    $fields['billing']['billing_status']['placeholder'] = __('Chose an option', 'my_theme_slug');
    $fields['billing']['billing_status']['options'] = array(
        '' => 'Chose an option',
        '1' => 'Legal entity',
        '2' => 'Individual'
    );

    // The "Serial ID" text field
    $fields['billing']['billing_serial_id']['type'] = 'text';
    $fields['billing']['billing_serial_id']['class'] = array('form-row-wide', 'status-group1');
    $fields['billing']['billing_serial_id']['required'] = true;
    $fields['billing']['billing_serial_id']['label'] = __('Serial ID', 'my_theme_slug');
    $fields['billing']['billing_serial_id']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug');

    // The "Custom 1" text field
    $fields['billing']['billing_custom1']['type'] = 'text';
    $fields['billing']['billing_custom1']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom1']['required'] = true;
    $fields['billing']['billing_custom1']['label'] = __('Custom name 1', 'my_theme_slug');
    $fields['billing']['billing_custom1']['placeholder'] = __('Enter your custom1', 'my_theme_slug');

    // The "Custom 2" text field
    $fields['billing']['billing_custom2']['type'] = 'text';
    $fields['billing']['billing_custom2']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom2']['required'] = true;
    $fields['billing']['billing_custom2']['label'] = __('Custom name 2', 'my_theme_slug');
    $fields['billing']['billing_custom2']['placeholder'] = __('Enter your custom2', 'my_theme_slug');

    // The "Custom 3" text field
    $fields['billing']['billing_custom3']['type'] = 'text';
    $fields['billing']['billing_custom3']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom3']['required'] = true;
    $fields['billing']['billing_custom3']['label'] = __('Custom name 3', 'my_theme_slug');
    $fields['billing']['billing_custom3']['placeholder'] = __('Enter your custom3', 'my_theme_slug');


// 2. Customizing 'billing_email' and 'billing_phone' fields ['class']

    $fields['billing']['billing_email']['class'] = array('form-row-first', 'status-group1');
    $fields['billing']['billing_phone']['class'] = array('form-row-last', 'status-group1');


// 3. Ordering the billing fields

    $fields_order = array(
        'billing_first_name', 'billing_last_name', 'billing_status',
        'billing_email',      'billing_phone',     'billing_serial_id',
        'billing_custom1',    'billing_custom2',   'billing_custom3',
        'billing_company',    'billing_address_1', 'billing_address_2',
        'billing_postcode',   'billing_city',      'billing_country',
    );
    foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field];

    $fields['billing'] = $ordered_fields;
    

// Returning Checkout customized billing fields

    return $fields;

}

当然,这会出现在您的活动子主题或主题的 function.php 文件中

该代码已经过测试并且功能齐全。

现在有了这段代码,您可以提出一个问题来处理这个问题的“条件”部分(正如我在评论中告诉您的那样)......

官方参考:WooThemes - 使用操作和过滤器自定义结账字段

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WooCommerce - 签出不同人自定义状态的条件字段 的相关文章

  • Monolog,如何将 PHP 数组记录到控制台?

    我正在使用浏览器处理程序将消息记录到 JS 控制台 require once vendor autoload php use Monolog Logger use Monolog Handler BrowserConsoleHandler
  • 使用 PHP 将 SVG 图像转换为 PNG

    我正在开发一个网络项目 该项目涉及动态生成的美国地图 根据一组数据为不同的州着色 这个 SVG 文件为我提供了一张很好的美国空白地图 并且很容易更改每个州的颜色 困难在于 IE 浏览器不支持 SVG 因此为了让我使用 svg 提供的便捷语法
  • 简单的颜色变化

    我正在创建一个用户界面 用户可以在其中更改页面的颜色值 我想要的是获取分配给其背景颜色的值并将其变亮一定程度 我只是想获得一条亮点线 而不必每次都制作新图像 示例 用户将背景颜色设置为 ECECEC 现在我希望某个元素边框变成 F4F4F4
  • preg_match 所有以@开头的单词?

    我对正则表达式不太确定 所以我不得不问你 如何用 PHP 判断字符串中是否包含以 开头的单词 例如我有一个像 This is for codeworxx 这样的字符串 我很抱歉 但我没有任何起点 希望你能帮忙 谢谢 萨沙 好的 谢谢你的结果
  • 如何通过 PDO 使用密码哈希来使我的代码更安全? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我的代码实际上可以工作 但它一点也不安全 我不想使用 MD5 因为它不是那么安全 我一直在查找密码哈希 但我不确定如何将其合并到我的代
  • 如何处理 PHP 中浮点数的奇怪舍入

    众所周知 浮点运算并不总是完全准确 但是如何处理它的不一致之处呢 As an example in PHP 5 2 9 this doesn t happen in 5 3 echo round 14 99225 4 14 9923 ech
  • 从 json 数组获取值并执行 sql 插入

    这是我的数组 json 1 Device ID a9a3346be4375a92 Date 2012 05 31 Time 15 22 59 Latitude 51 4972912 Longitude 0 1108178 2 Device
  • json_encode 创建格式错误的 JSON 数据?

    我有一个 codeigniter 应用程序将一些数据从数据库返回到视图 我正在尝试将其作为 json 数据发送回来 问题是返回的数据格式错误 它看起来像这样 2 5 Admin1 2 10 Admin2 当我在 jsonlint com 上
  • MySQL:“您的 SQL 语法错误...靠近键...”? [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我发现了一个非常酷的用于丢失密码的脚本 但是这一行给我带来了问题 r mysql query INSERT INTO keys u
  • Lumen 微框架 => php artisan key:generate

    我正在尝试 PHP 微框架 Lumen 来自 Laravel 我的第一步就是调查 env example文件并复制一份以供我使用 env文件 就像 Laravel 中一样 有一个变量 APP KEY 现在我尝试了简单的命令php artis
  • 如何使用额外标记输出 wp_list_categories

    我目前正在使用下面的脚本在无序列表中输出我的所有 WordPress 类别 如何获得带有额外标记的输出 ul ul 例如 ul li Category 1 rsaquo li li Category 2 rsaquo li ul 代替 ul
  • PHP URL 验证

    我知道有无数的线程问这个问题 但我一直无法找到一个可以帮助我解决这个问题的线程 我基本上试图解析大约 10 000 000 个 URL 的列表 确保它们根据以下标准有效 然后获取根域 URL 此列表包含您能想象到的几乎所有内容 包括类似的内
  • 使用 Flot、html、PHP 和 MySql 查询绘制多个图表

    我正在尝试使用 Flot html PHP 和 MySql 查询绘制多个图表 但我陷入了困境 因为我找不到在同一个 html 页面中绘制多个 flot 的方法 为简单起见 在数据库 test db3 映像中包含以下字段 表1 用户名 发送邮
  • 仅在 Chrome 上我收到此错误:Uncaught TypeError: Illegal constructor [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 当我在 Chrome 上加载 jQuery 时 我会收到此错误 Uncaught TypeError Illegal constr
  • Slim 3 - 斜杠作为路由参数的一部分

    我需要使用可以包含斜杠 的参数来编写 URL 例如 经典的 hello username 路线 默认情况下 hello Fabien将匹配此路线 但不匹配 hello Fabien Kris 我想问你如何在 Slim 3 框架中做到这一点
  • 如果文件名减去扩展名,.htaccess url 重写行为将被覆盖。与网址相同

    我正在尝试整理 URL 并从中删除 php 扩展名等 我位于网站的基本文件夹中 因此没有可以优先处理的父 htaccess 文件或其他文件 这是我的 htaccess 代码 RewriteEngine On RewriteRule give
  • 准备好的语句需要 0 个参数,给定 1 个参数..,使用 php 手册示例 [重复]

    这个问题在这里已经有答案了 我直接从 php 手册示例中获取了这个 它几乎与我需要的相同 但我仍然收到此错误 有人可以告诉我我错过了什么吗 stmt link gt prepare SELECT obitBody Photo FROM tn
  • PHP 用星号替换所有字符

    假设我有一个字符串形式的密码 password thisisaplaintextpassword 我怎样才能把它变成下面的样子 password 我想通过电子邮件向用户发送他们的帐户详细信息 但不想发送整个内容 Use 字符串重复 http
  • __callStatic():从静态上下文实例化对象?

    我对 PHP 中的 静态 和 动态 函数和对象如何协同工作感到困惑 特别是在 callStatic 方面 callStatic 的工作原理 您可以有一个普通的班级 MyClass 在班级内您可以 放置一个名为 callStatic 的静态函
  • 使用 md5 加密的 PHP 和 Mysql 查询出现问题

    我使用普通的 php mysql 插入查询并使用 md5 加密密码 这是插入查询 sql mysql query INSERT INTO user username password role approved values usernam

随机推荐