php - 重新排列和自定义 woocommerce 结帐字段
问题描述
我需要修改默认的 woocommerce 计费和运输字段。我需要移动其中一些,设置不同的表单行类并在删除标签时添加占位符。我的代码正在运行,但我想知道是否有更简洁的解决方案,也就是代码是否可以缩短和/或优化。我的代码是
add_filter('woocommerce_default_address_fields', 'override_address_fields');
function override_address_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'yxz';
$address_fields['last_name']['placeholder'] = 'yxz';
$address_fields['address_1']['placeholder'] = 'yxz';
$address_fields['company']['placeholder'] = 'yxz';
$address_fields['postcode']['placeholder'] = 'yxz';
$address_fields['city']['placeholder'] = 'yxz';
return $address_fields;
}
add_filter( "woocommerce_checkout_fields", "reordering_checkout_fields", 15, 1 );
function reordering_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['placeholder'] = 'yxz';
unset($fields['order']['order_comments']);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'rearrange_checkout_fields' );
function rearrange_checkout_fields( $checkout_fields ) {
$checkout_fields['billing']['billing_country']['priority'] = 80;
$checkout_fields['shipping']['shipping_country']['priority'] = 80;
return $checkout_fields;
}
解决方案
您可以在第一个函数中包含第三个函数,因此您的代码将如下所示:
add_filter('woocommerce_default_address_fields', 'customize_default_address_fields', 20, 1 );
function customize_default_address_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'yxz';
$address_fields['last_name']['placeholder'] = 'yxz';
$address_fields['address_1']['placeholder'] = 'yxz';
$address_fields['company']['placeholder'] = 'yxz';
$address_fields['postcode']['placeholder'] = 'yxz';
$address_fields['city']['placeholder'] = 'yxz';
// Reorder billing and shipping "country" fields (on checkout page)
if ( is_checkout() )
$address_fields['country']['priority'] = 80; // Country
return $address_fields;
}
add_filter( "woocommerce_checkout_fields", "customize_other_checkout_fields", 20, 1 );
function customize_other_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['class'] = array('form-row-first'); // HERE
$fields['billing']['billing_phone']['class'] = array('form-row-last'); // HERE
unset($fields['order']['order_comments']);
return $fields;
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。
推荐阅读
- powerbi - PowerBI:根据偶尔的消息计算包含当前固件的列
- java - 超过 40 次测试的 JdbcSQLException
- ios - 无法在 iPhone 上安装开发应用程序
- openstack - ubuntu 上的 microstack 没有注册 cinder 卷服务
- opengl - 如何从前向矢量计算俯仰和偏航值
- .net-core - 在 identotyserver 4 中的 API(资源)中引发错误
- react-native - GraphQl 生成语法错误:预期 $,找到 {]
- php - 如何向 ElasticSearch Aggregation (min/max) 添加条件?
- python - Apache/mod_wsgi ModuleNotFoundError
- android - 可绘制文件夹中的图片不起作用 - 为什么?