mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
https://stackoverflow.com/questions/73017080/do-not-allow-free-and-paid-products-to-be-bought-together-in-woocommerce/73018259
45 lines
1.5 KiB
Text
45 lines
1.5 KiB
Text
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
|
|
// Get current product
|
|
$current_product = wc_get_product( $product_id );
|
|
|
|
// Get current product price
|
|
$current_product_price = $current_product->get_price();
|
|
|
|
// Initialize
|
|
$is_free = false;
|
|
$notice = false;
|
|
|
|
// When current product price = 0
|
|
if ( $current_product_price == 0 ) {
|
|
// Make true
|
|
$is_free = true;
|
|
}
|
|
|
|
// Loop through cart contents
|
|
foreach ( WC()->cart->get_cart_contents() as $item_key => $cart_item ) {
|
|
// Get price
|
|
$product_price = $cart_item['data']->get_price();
|
|
|
|
// Product price is NOT equal to 0 and current product is free
|
|
if ( $product_price != 0 && $is_free ) {
|
|
// Remove product from cart
|
|
WC()->cart->remove_cart_item( $item_key );
|
|
|
|
// Make true
|
|
$notice = true;
|
|
// Product price is equal to 0 and current product is NOT free
|
|
} elseif ( $product_price == 0 && ! $is_free ) {
|
|
// Remove product from cart
|
|
WC()->cart->remove_cart_item( $item_key );
|
|
|
|
// Make true
|
|
$notice = true;
|
|
}
|
|
}
|
|
|
|
// Optionaly displaying a notice
|
|
if ( $notice ) {
|
|
wc_add_notice( __( 'Some products have been removed from the cart because free and paid products cannot be bought together', 'woocommerce' ), 'notice' );
|
|
}
|
|
}
|
|
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 6 );
|