My Courses > CustomizeWoo > Module 6 > Lesson 06: Edit Return to Shop URL

Edit Return to Shop URL

MARK LESSON AS COMPLETE

You can use this exercise as a test. Stop the video after the project description and see if you can manage to find the PHP solution. Then continue watching the video to watch me code the task.

Video

Please note: English captions are enabled by default (click on “CC” in the video player to disable). Also, you can click on the “gear” icon to manage quality and playback speed. Enjoy!

Sorry, this video is visible to logged in and fully registered students only.
If you already enrolled in this course, please LOG IN
Otherwise, please enroll in the FREE / PRO COURSE
For any other queries, feel free to get in touch

2 thoughts on “Edit Return to Shop URL

  1. Rodolfo, I discovered. This is the code to return to the last visited product category, that is convenable for me. Please check the code I wrote, I tested and it works on my website. Should I clean something here?

    /* Set up session */
    add_action( 'init', 'wp_check_for_product_cat_sess');
    function wp_check_for_product_cat_sess(){
        if(!session_id()) {
            session_start();
        }
    }
    
    /* Set session variable when on Category or Product page */
    add_action( 'wp', 'wp_check_for_product_cat');
    function wp_check_for_product_cat(){
            global $wp_query;
            if( is_product_category() ){ // This is Category; use my ID
                    $_SESSION['wp_last_cat'] = $wp_query->get_queried_object()->term_id;
            }
            if( is_product() ){ // This is Product; use my ID to get my Categories
                    $cats = get_the_terms( $wp_query->get_queried_object(), 'product_cat' ) ;
                    if( count( $cats ) > 0 ){
                            foreach($cats as $one_cat ){
                                    $_SESSION['wp_last_cat'] = $one_cat->term_id;
                            }
                    }
            }
    
            if( is_cart() ){
                    // Here we output only on Cart page, as debug tool */
                    var_dump( $_SESSION['wp_last_cat'] );
            }
    }
    
    add_filter( 'woocommerce_return_to_shop_redirect', 'mytest_change_return_shop_url' );
    function mytest_change_return_shop_url() {
    return     $shop_page_url = get_term_link( $_SESSION['wp_last_cat'], 'product_cat' );
    
    }
    1. Looks very good to me. Well done ๐Ÿ™‚

Questions? Feedback? Support? Leave your comment now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]

Your email address will not be published. Required fields are marked *