wordpress - 删除结帐页面上特定类别的帐户用户名和密码
问题描述
希望每一个人都做得很好。我正在尝试从结帐页面中删除帐户密码和帐户用户字段。这段代码运行完美。
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
unset($fields['account']['account_username']);
return $fields;
}
但问题是,我只为某些类别删除了这个。所以我试图按类别删除。所以我的代码有些人这样认为,但它不起作用。
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
global $product;
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->slug;
}
if ($product_cat='age-defying-skincare') {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
unset($fields['account']['account_username']);
return $fields;
}
}
解决方案
您正在使用动作挂钩。它不能更新或删除该字段。
do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
如果要在用户字段中进行更改。
woocommerce_new_customer_data这是一个过滤钩子。
然后试试这个钩子:
$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
) );
您将在此处获得有关此内容的更多详细信息。 http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-user-functions/
更新: 对于 WooCommerc 结帐字段
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$cart_items = WC()->cart->get_cart();
// Categories Array
$categories = array('age-defying-skincare');
foreach( $cart_items as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
unset($fields['account']['account_username']);
}
}
return $fields;
}
试试上面的代码。
推荐阅读
- amazon-s3 - 具有过期日期的 Cloudfront 签名 URL 在我的应用程序中有效,但在浏览器中无效
- xml - libxml2 是否有可用的 XPath 语法错误描述 (XML_XPATH_EXPR_ERROR)
- graphql - NestJS GraphQL 确保标量类型
- node.js - Express:在静态文件夹上添加中间件
- if-statement - 与 ATR 密切相关的 2 根蜡烛的价格变动
- javascript - 如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript
- excel - 隐藏 Excel 工作簿上所有工作表上的空白行
- javascript - 什么是 jquery 的 on() &trigger() 的 javascript 版本(具有捕获参数的能力)
- python - 从熊猫的数据框中排除值
- reactjs - onClick 事件未在功能元素上触发