Redirect to custom URL after user clicks 'Proceed to checkout' at the cart page (wordpress)

南笙酒味 提交于 2021-02-18 18:50:31

问题


When user clicks 'Proceed to checkout' at the cart page, I would like to send him to custom URL. I used this filter in functions.php

add_filter('woocommerce_get_checkout_url', 'dj_redirect_checkout');

function dj_redirect_checkout($url) {
     global $woocommerce;
     $checkout_url = 'http://my-custom-url.com';
     return  $checkout_url; 
}

However, in this case this filter is triggered on the checkout page as well and I would like to trigger it on the Cart page and after the click 'Proceed to checkout' only.

Please, advise.

Thank you,


回答1:


What you can do is, use conditional tags in your code :

    add_filter('woocommerce_get_checkout_url', 'dj_redirect_checkout');

    function dj_redirect_checkout($url) {
         global $woocommerce;
         if(is_cart()){
              $checkout_url = 'http://my-custom-url.com';
         }
         else { 
         //other url or leave it blank.
         }
         return  $checkout_url; 
    }

EDITED :

If you want to redirect users to custom URL after click on Add to card button then you can use below code:

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = 'http://my-custom-url.com';
        return $checkout_url;
    }

Second Edit :

What you can do for that is simply make on template for check out page :

    <?php 
    /**
    * Template Name: Custom Template
    *
    *Custom Template for the woocommerce checkout page
    *
    */

    get_header();?>

    <a href="?page_id=6">Click Me! </a>

    <?php 
    get_footer();
    ?>

Here in above code, I have made template with name of custom Template and save it with the wtemplate.php in the theme folder.

Next step is to Create New Page. I have created new page with the name of Simple Template(You can name it whatever you want). And in sidebar, there is option of template(you can see below image for reference) where I have selected Custom Template as a template.

enter image description here

Now Go to the Dashboard > WooCommerce > Settings > Checkout ** and in that, there is option of selecting checkout page(You can find in below picture). Select the page that you have made(I have made **Simple Template as you can see in above code) and save it.

enter image description here

Now whenever user will click on Proceed to Checkout, they will redirect to that custom template.In custom template, you can link to the woocommerce default checkout page(In my case, It is having page_id=6).

Hope it helps you.If you have any doubt related to this, you can give comment.



来源:https://stackoverflow.com/questions/27355896/redirect-to-custom-url-after-user-clicks-proceed-to-checkout-at-the-cart-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!