Woocommerce有条件地重定向添加到购物车按钮


Woocommerce Redirecting Add to Cart button conditionally

在将某个产品添加到购物车后,我希望将用户重定向到另一个页面。我已经尝试了几个片段从各地的网络,但我所有的条件逻辑返回null或0当我在functions.php中输入它。我尝试了基于页面、产品和类别的条件。这是我从这里的另一个答案中找到的最新片段,它不适合我,知道为什么吗?

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;
            //Get product ID
            $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);
            //Check if product ID is in a certain taxonomy
            if( has_term( 'sidestix', 'product_cat', $product_id ) ){
                        //Get cart URL
                        $checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
                        //Return the new URL
                        return $checkout_url;
             }
    }

我终于在一个不同的论坛上找到了一个工作片段。我能看出区别,但我不确定为什么它能起作用。

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;
            //Get product ID
            $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'] ) );
            //Check if product ID is in a certain taxonomy
            if( has_term( 'Philanthropy', 'product_cat', $product_id ) ){
                        //Get cart URL
                        $checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
                        //Return the new URL
                    return $checkout_url;
             };
    }