需要在 WooCommerce 中为特定产品注册 [英] Require registration in WooCommerce for specific product
问题描述
在 WooCommerce 设置中要求全球注册很简单,但对于我们销售的大多数产品,这不是必需的,我宁愿限制登录用户.
It's straightforward to required registration globally in WooCommerce settings but for most of the products we sell, it's not necessary and I would rather restrict the logged in users.
但是对于一种产品,我想要求注册.
However for one product I would like to require registration.
类似的东西
$product_id = $product->get_id();
if ($product_id !== 1024) {
remove_action( 'woocommerce_before_checkout_registration_form', 'action_woocommerce_checkout_registration_form', 10, 1 );
}
但显然没那么简单.有什么想法吗?
but it's obviously not that simple. Any ideas?
推荐答案
这个答案假设 WooCommerce >设置 >帐户和隐私 >允许客户在没有帐户的情况下下订单
This answer assumes that WooCommerce > Settings > Accounts & privacy > Allow customers to place orders without an account is enabled
然后您可以使用以下代码,以确保当购物车中有1个或多个productID's
时,结帐时需要注册
You can then use the following code, that will ensure that when there is 1 or more productID's
in the shopping cart, registration during checkout will be required
function filter_woocommerce_checkout_registration_required( $bool_value ) {
// Several can be added, separated by a comma
$product_ids = array ( 30, 813 );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Product ID from cart item in specific array
if ( in_array( $cart_item['product_id'], $product_ids ) ) {
// Registration_required
$bool_value = true;
// Break loop
break;
}
}
return $bool_value;
}
add_filter( 'woocommerce_checkout_registration_required', 'filter_woocommerce_checkout_registration_required', 10, 1 );
这篇关于需要在 WooCommerce 中为特定产品注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!