too long

So, found this snippet that clears the basket and it works well when added to functions.php:

function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart(); 
}
add_action('init', 'my_empty_cart');

How can I modify this and make it empty the cart only when certain pages are loaded? I played around with if ( is_page( 'pageID' ) but couldn't get it working properly!

Try this,

global $post;
if($post->ID == 'something'){
add_action('init', 'my_empty_cart');
}


function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart(); 
}

for more readings.

check this link it have all the page / function codes in woocommerce

Hope its works..

WordPress conditional didnt worked for me either so i did something that dont relies on WordPress conditionals:

/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ($_SERVER['REQUEST_URI'] === '/') { 
    $woocommerce->cart->empty_cart(); 
 }
}

if u want to add to certain pages u can edit the conditional exp: (didnt try it)

if ($_SERVER['REQUEST_URI'] === get_permalink('post_id'))

if the get_permalink doest work use the exact url exp: 'https://www.domain.name/post-75'