Where to add Woocommerce Code Snippets

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.

Hi @zagency,

The two easiest options are to use the functions.php in the Cwicly child theme or to use a plugin like code snippets.

You may also be interested in the following feature request to make the Cwicly child theme easier to install:

1 Like

Thank you @StrangeTech I went with the code snippets plugin. If i install the child theme will i loose any settings or work?

Good choice for a site with existing customisations to the theme.

When changing themes the theme and template customisations can be lost unless you explicitly export them and add them to the new theme.