'Woocommerce empty cart and add a new product on specific page template

I want to empty the cart before adding a new product and it works with this code:

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);

function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

Now I need to do this on a specific page template. Is that possible?

I tried this but it's not working

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);

function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {

    global $woocommerce;
    if (is_page_template('templates/checkout.php')){
        $woocommerce->cart->empty_cart();
    }
    
    return $cart_item_data;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source