Woocommerce按钮继续购物至“最后产品类别”页面

时间:2018-09-29 00:59:59

标签: php wordpress woocommerce

我要制作-购物车页面中的“继续购物”按钮,以使商店中的顾客返回到上次查看的产品类别页面。 我使用此代码,但是button仅使我返回到类别的第一页。

    <?php

/* 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 back to store button after cart
// =============================================================================
add_action('woocommerce_cart_actions', 'themeprefix_back_to_store');
function themeprefix_back_to_store () {
if( isset( $_SESSION['wp_last_cat'] ) ){
    $shop_page_url = get_term_link( $_SESSION['wp_last_cat'], 'product_cat' );
} else {
    $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
}

echo '<div class="woocommerce-message">';
echo ' <a href="'.$shop_page_url.'" class="button wc-forwards">Continue 
Shopping →</a> Would you like some more prints?';
echo '</div>';
}

0 个答案:

没有答案