I would like to know the proper way and place to insert woocommerce code snippets like this:
/*Replace Cart Items to Keep 1 Item in per Time*/
add_filter( 'woocommerce_add_to_cart_validation', 'yith_wcbk_empty_cart', 30, 2 );
if( ! function_exists( 'yith_wcbk_empty_cart' ) ){
function yith_wcbk_empty_cart($valid, $product_id ){
global $woocommerce;
$product = wc_get_product( $product_id );
if( $product->is_type( 'booking' )){
foreach ( WC()->cart->get_cart() as $cart_item ) {
// If product categories is found
$cart_product = wc_get_product($cart_item['product_id']);
if( $cart_product->is_type( 'booking' )){
$woocommerce->cart->empty_cart();
wc_clear_notices();
$valid = true;
}
}
}
$message = 'You cannot add to cart more than one booking. Your cart has been empty and include only last booking product';
wc_add_notice( $message, 'notice' );
return $valid;
}
}
Putting it in the functions.php file works but i assume that will get removed whenever i update Cwicly.
Any help will be greatly appreciated.