Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • WooCommerce- how to redirect product add to cart?

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 372
    Comment on it

    Hello Readers!

    If you want to redirect users after adding a specific product to their cart. Below is the example, just pasted this code into into your themes functions.php file

    <?php
    
        add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_add_to_cart' );
        function redirect_add_to_cart() {
    
         //Get product ID
         if ( isset( $_POST['add-to-cart'] ) ) {
    
          $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
    
          //Check's the product ID is in the proper taxonomy and return the URL to the redirect product
          if ( has_term( 'posters', 'product_cat', $product_id ) )
           return get_permalink( 10 );
    
         }
    
        }
    
        ?>
    

    In above example, the code will get the id of the product added to the cart and checks if the product is in the correct product category of woocommerce. and at the end it will gets the permalink of redirected product abd returns it to the add_to_cart_redirect filter. Gets the permalink of the redirect product and returns it to the WooCommerce add_to_cart_redirect filter.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: