• I’m using woocommerce and I’m trying to create a function, when clicking the ‘add to cart’ button, the visitor goes directly to the checkout page.

    The button is a product link that automatically adds the product to the cart and the page is not a product page, it’s a custom template called landing-page.php which is inside my child theme folder.

    I only want this function to work on the landing-page.php, every other template should have a normal checkout process.

    Here is the code I’ve found that will execute the function I need; however, it will execute the function on every page template:

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    
    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }

    Here is the code I’m trying to use:

    if (is_page_template( 'landing-page.php' )) {
    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    
    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }
    }

    But it doesn’t work. Why isn’t this working?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hello ClintonLee83,

    Is you page template “landing-page.php” in your theme folder ? If not use like

    Page template in subdirectory

    If the page template is located in a subdirectory of the theme
    e.g.:

    is_page_template( ‘templates/about.php’ );

    Thanks

    Thread Starter ClintonLee83

    (@clintonlee83)

    Hi there,

    Yes, as I mentioned above, the landing-page.php is directly inside the child theme folder.

    Do I need to make a subdirectory? I’m able to select this template from the page editor inside wordpress and it shows up fine.

    The only problem is that I’m unable to apply the function (redirect straight to checkout using an add to cart button) on this template only.

    try using filter hook inside if condition.
    other solution is that get the page slug for the page you using this Template and then check for condition and apply filter

    Thread Starter ClintonLee83

    (@clintonlee83)

    Thanks Creativepaddy,

    I’m terrible with this stuff, can you show me how to make a filter hook?

    I’ve been searching for an answer to this and came across a post that provided this solution:

    function checkPageTemplate(){
    
    	$currentPost = get_post();
            $pageTemplate = get_post_meta( $currentPost->ID, '_wp_page_template', true );
    
    	if ( $pageTemplate == "landing-page.php") {
    		add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    	}
    }
    add_action( 'the_post', 'checkPageTemplate' );
    
    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }

    But it’s still not working

    other solution is that get the page slug for the page you using this Template and then check for condition and apply filter

    I need this to work on all of the pages I use the template on, not just one. Would I have to add a page slug to this function for each page I create and want to use this on? If that’s the case, I’d rather find a solution that doesn’t require me to mess with the code because this is for someone who knows nothing about code.

    Ohhh,
    I missed it, you are using filter hook add_filter (‘add_to_cart_redirect‘, ‘redirect_to_checkout’);
    which should be,
    add_filter( ‘woocommerce_add_to_cart_redirect‘, ‘my_function_name’ );
    The code will be like the same as above with changes,

    unction checkPageTemplate(){

    $currentPost = get_post();
    $pageTemplate = get_post_meta( $currentPost->ID, ‘_wp_page_template’, true );

    if ( $pageTemplate == “landing-page.php”) {
    add_filter (‘woocommerce_add_to_cart_redirect’, ‘redirect_to_checkout’);
    }
    }
    add_action( ‘the_post’, ‘checkPageTemplate’ );

    function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
    }

    Hope this help you reply must… ??

    Thread Starter ClintonLee83

    (@clintonlee83)

    I’ve just replaced my code with yours in the child theme’s functions, it’s still not working…

    Your ‘f’ got cut off from ‘function’ at the top of your code, I fixed it when I added it.

    function checkPageTemplate(){
    
    $currentPost = get_post();
    $pageTemplate = get_post_meta( $currentPost->ID, '_wp_page_template', true );
    
    if ( $pageTemplate == "landing-page.php") {
    add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
    }
    }
    add_action( 'the_post', 'checkPageTemplate' );
    
    function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
    }

    Here’s the original sources I’m using to figure this out:
    https://www.remicorson.com/woocommerce-skip-product-cart-pages/
    https://www.ads-software.com/support/topic/apply-addfilter-to-a-specific-page-template?replies=7

    Thread Starter ClintonLee83

    (@clintonlee83)

    I can add:

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    
    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }

    directly into the landing-page.php template, correct? Wouldn’t this solve the problem? I’m just not sure how to add the code into the template, I’ve tried adding it and I’m getting errors.

    Here’s the template, how can I add the filter directly into this?

    <?php /* Template Name: Landing Page */ ?>
    
    <?php
    get_header ( "landing" );
    ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php while ( have_posts() ) : the_post();
    
    				do_action( 'storefront_page_before' );
    
    				get_template_part( 'content', 'page' );
    
    				/**
    				 * Functions hooked in to storefront_page_after action
    				 *
    				 * @hooked storefront_display_comments - 10
    				 */
    				do_action( 'storefront_page_after' );
    
    			endwhile; // End of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php
    get_footer();
    Moderator bcworkz

    (@bcworkz)

    I’m afraid that will not work. For anyone following along, I suggested this idea in another thread.

    Upon a more in depth investigation, this idea will not work in this case because the add to cart button involves javascript. If it were a simple hard link, my idea would be fine, but with javascript, the filter is added too late to affect the related javascript.

    Thus we are back to conditionally adding the filter only when the landing page is requested. One way to do this is to hook the ‘pre_get_posts’ action and only add the filter when the page_id matches the landing page ID. This is assuming the landing page is set as the site’s static home page, this is the only time we can be sure page_id is actually used for the request query. The following could go on your theme’s functions.php. Ideally your theme should be a child theme so your code is not overwritten during theme updates.

    add_action('pre_get_posts', 'cl_change_cart_redirect');
    function cl_change_cart_redirect( $query ) {
       if ( *LANDING_ID* == $query->get('page_id')) {
          add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
       }
    }

    Change *LANDING_ID* to the actual page ID of the landing page. Be sure the redirect_to_checkout() declaration is part of the code you add, assuming it is not declared somewhere else already.

    Thread Starter ClintonLee83

    (@clintonlee83)

    Thanks bcworkz!

    I saw this solution while digging around and I don’t think I can use it. I’m designing this site as a template for a few people who know nothing about coding so I don’t want them to go into the functions and add an id everytime they create a new landing page for a new product.

    The filter should only work with the page template, not individual pages.

    I am however using a hard link to add the item to the cart. For example, the link on our landing pages look something like this:

    https://thebohemian.boutique/product/feather-necklace/?add-to-cart=9&variation_id=17&attribute_pa_product-color=gold

    I’m not sure if that would solve the js problem you mentioned. And maybe linking the product like this is the reason I’m having a hard time getting this to work in the first place since I’m only using a page template, not a product page template.

    Moderator bcworkz

    (@bcworkz)

    The hook to ‘woocommerce_add_to_cart_redirect’ works despite the JS right? It’s just that it is applied to all pages, not just a specific template.

    So we need to selectively add the filter based on the template loaded. Instead of adding the filter in ‘pre_get_posts’, let’s try ‘posts_results’. This is the earliest possible hook after the requested page is determined, meaning the post/page ID is available and we can thus determine what template it is based on. It doesn’t matter if it’s a page template or a product page template, only that the template name is available in post meta.

    I think the only problem with Creativepaddy’s script is that hooking ‘the_post’ is also too late to affect the JS code. I’m hoping ‘posts_results’ is early enough that the modified URL makes it into the localized JS variables.

    add_action('posts_results', 'cl_change_cart_redirect', 10, 2 );
    function cl_change_cart_redirect( $posts, $query ) {
       if ( $query->is_main_query() && 'landing-page.php' == get_post_meta( $posts[0]->ID, '_wp_page_template', true )) {
          add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
       }
    }

    Untested, but that is basically it. If it proves to not be early enough, you probably need to look at disabling the JS add to cart function somehow. If you can identify that function, you could override it with a function that doesn’t do anything, so the action reverts to the button’s anchor href attribute.

    Thread Starter ClintonLee83

    (@clintonlee83)

    I love how persistent you are with this bcworkz,

    I’ve tried adding this code to the child functions and it turns the landing-page.php into a 404 with the error message:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘redirect_to_checkout’ not found or invalid function name in /home/myspoona/public_html/thebohemian/wp-includes/plugin.php on line 235

    Moderator bcworkz

    (@bcworkz)

    “persistent” ?? I like solving puzzles, persistence goes with the territory.

    You still need the redirect_to_checkout() function declaration:

    function redirect_to_checkout() {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }

    in addition to my previously suggested code.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Apply function to specific page template’ is closed to new replies.