避免 WooCommerce 购物车中的虚拟和实体产品组合
Avoid virtual and physical products combination in WooCommerce cart
我已经搜索了很长时间,但似乎从未找到可能的解决方案。
有没有人看到 如何在一个 Woocommerce 购物车中只允许实体产品或虚拟产品的任何解决方案? 可能,当客户尝试添加组合时弹出通知虚拟和物理并不允许组合 - 或者确保组合不能出现在一个购物车中。
顺序中的组合总是导致错误..
请帮忙。
以下将避免在购物车中合并实物和虚拟产品:
// Check cart items and avoid add to cart
add_filter( 'woocommerce_add_to_cart_validation', 'filter_wc_add_to_cart_validation', 10, 3 );
function filter_wc_add_to_cart_validation( $passed, $product_id, $quantity ) {
$is_virtual = $is_physical = false;
$product = wc_get_product( $product_id );
if( $product->is_virtual() ) {
$is_virtual = true;
} else {
$is_physical = true;
}
// Loop though cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Check for specific product categories
if ( ( $cart_item['data']->is_virtual() && $is_physical )
|| ( ! $cart_item['data']->is_virtual() && $is_virtual ) ) {
wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
return false;
}
}
return $passed;
}
// For security: check cart items and avoid checkout
add_action( 'woocommerce_check_cart_items', 'filter_wc_check_cart_items' );
function filter_wc_check_cart_items() {
$cart = WC()->cart;
$cart_items = $cart->get_cart();
$has_virtual = $has_physical = false;
// Loop though cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Check for specific product categories
if ( $cart_item['data']->is_virtual() ) {
$has_virtual = true;
} else {
$has_physical = true;
}
}
if ( $has_virtual && $has_physical ) {
// Display an error notice (and avoid checkout)
wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我已经搜索了很长时间,但似乎从未找到可能的解决方案。
有没有人看到 如何在一个 Woocommerce 购物车中只允许实体产品或虚拟产品的任何解决方案? 可能,当客户尝试添加组合时弹出通知虚拟和物理并不允许组合 - 或者确保组合不能出现在一个购物车中。
顺序中的组合总是导致错误..
请帮忙。
以下将避免在购物车中合并实物和虚拟产品:
// Check cart items and avoid add to cart
add_filter( 'woocommerce_add_to_cart_validation', 'filter_wc_add_to_cart_validation', 10, 3 );
function filter_wc_add_to_cart_validation( $passed, $product_id, $quantity ) {
$is_virtual = $is_physical = false;
$product = wc_get_product( $product_id );
if( $product->is_virtual() ) {
$is_virtual = true;
} else {
$is_physical = true;
}
// Loop though cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Check for specific product categories
if ( ( $cart_item['data']->is_virtual() && $is_physical )
|| ( ! $cart_item['data']->is_virtual() && $is_virtual ) ) {
wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
return false;
}
}
return $passed;
}
// For security: check cart items and avoid checkout
add_action( 'woocommerce_check_cart_items', 'filter_wc_check_cart_items' );
function filter_wc_check_cart_items() {
$cart = WC()->cart;
$cart_items = $cart->get_cart();
$has_virtual = $has_physical = false;
// Loop though cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Check for specific product categories
if ( $cart_item['data']->is_virtual() ) {
$has_virtual = true;
} else {
$has_physical = true;
}
}
if ( $has_virtual && $has_physical ) {
// Display an error notice (and avoid checkout)
wc_add_notice( __( "You can't combine physical and virtual products together.", "woocommerce" ), 'error' );
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。