如果购物车数量不能被特定数字整除,请禁用 WooCommerce Checkout
Disable WooCommerce Checkout if cart count is not divisible by a certain number
我有一些代码可以检查 WooCommerce 中的产品数量是否可以被 6 整除 - 但是即使显示了通知,客户仍然可以使用不正确的数量结账。
如果不满足条件,如何禁用 WooCommerce 'Place Order' 结帐?
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
global $woocommerce;
$multiples = 6;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}
钩子 woocommerce_check_cart_items
需要一个“错误”通知来禁用结帐 (事实并非如此)。我还简化了代码:
// Check cart items conditionally displaying an error notice and avoiding checkout
add_action( 'woocommerce_check_cart_items', 'check_cart_items_conditionally' );
function check_cart_items_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiple_of ), 'error' );
}
}
可选: 为确保无法访问结帐,您可以添加以下内容,如果购物车商品总数不是 N 的倍数,则将结帐重定向到购物车页面:
// To be sure: redirect checkout to cart page if cart items count is not a multiples of N
add_action( 'template_redirect', 'redirect_checkout_to_cart_conditionally' );
function redirect_checkout_to_cart_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( is_checkout() && ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wp_redirect( wc_get_cart_url() );
exit();
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我有一些代码可以检查 WooCommerce 中的产品数量是否可以被 6 整除 - 但是即使显示了通知,客户仍然可以使用不正确的数量结账。
如果不满足条件,如何禁用 WooCommerce 'Place Order' 结帐?
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
global $woocommerce;
$multiples = 6;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}
钩子 woocommerce_check_cart_items
需要一个“错误”通知来禁用结帐 (事实并非如此)。我还简化了代码:
// Check cart items conditionally displaying an error notice and avoiding checkout
add_action( 'woocommerce_check_cart_items', 'check_cart_items_conditionally' );
function check_cart_items_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiple_of ), 'error' );
}
}
可选: 为确保无法访问结帐,您可以添加以下内容,如果购物车商品总数不是 N 的倍数,则将结帐重定向到购物车页面:
// To be sure: redirect checkout to cart page if cart items count is not a multiples of N
add_action( 'template_redirect', 'redirect_checkout_to_cart_conditionally' );
function redirect_checkout_to_cart_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( is_checkout() && ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wp_redirect( wc_get_cart_url() );
exit();
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。