woocommerce offtopic – I can’t implement an if statement with is_page($pageId), is_single() and $post->ID

Question

I am struggeling with getting the ID of the current page when a button is clicked. The button has an “add to cart” function for woocommerce. Basically I want to redirect the user to the cart page if he adds the product from a certain page. In my case the page id is 99.
I tried the following codes but none of them works.

add_action('woocommerce_add_to_cart', 'bottel_add_to_cart', 999);

function bottel_add_to_cart(){
    if(is_single(99)){
        $checkout = wc_get_checkout_url();
        header("Location: " . $checkout);
        exit();
    }
}

add_action('woocommerce_add_to_cart', 'bottel_add_to_cart', 999);

function bottel_add_to_cart(){
    if(is_page(99)){
        $checkout = wc_get_checkout_url();
        header("Location: " . $checkout);
        exit();
    }
}

add_action('woocommerce_add_to_cart', 'bottel_add_to_cart', 999);

function bottel_add_to_cart(){
    if($post->ID == 99){
        $checkout = wc_get_checkout_url();
        header("Location: " . $checkout);
        exit();
    }
}

add_action('woocommerce_add_to_cart', 'bottel_add_to_cart', 999);

function bottel_add_to_cart(){
    global $post;
    if($post->ID == 99){
        $checkout = wc_get_checkout_url();
        header("Location: " . $checkout);
        exit();
    }
}

0
vancoder 2 months 2022-12-13T12:10:42-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse