Woocommerce继续购物按钮链接到最后一个产品类别页面 [英] Woocommerce Continue Shopping Button Link to Last Product Category Page
问题描述
这里有关于自定义继续购物按钮的论坛主题,但是没有关于如何将自定义按钮链接到上次查看的产品类别页面的内容.有一个插件为此提供了选项.但是,存在一个缺陷,即在刷新购物车时,该按钮会消失.以下代码可用于创建自定义消息/按钮;但是,我不知道如何将链接从商店页面更改为上次查看的产品类别页面:
There are forum topics regarding custom continue shopping buttons, but there is nothing on how to link the custom button to the last product category page viewed. There is a plugin that gives the option for this; however, there is a flaw in that when the cart is refreshed, the button disappears. The following code works to create a custom message/button; however, I cannot figure out how to change the link from the shop page to the last product category page viewed:
/* Add Continue Shopping Button on Cart Page & Checkout page*/
add_action( 'woocommerce_before_cart_table',
'woo_add_continue_shopping_button_to_cart' );
add_action( 'woocommerce_before_checkout_form',
'woo_add_continue_shopping_button_to_cart' );
function woo_add_continue_shopping_button_to_cart() {
$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>';
}
先谢谢您!
这是插件用来链接的代码(我认为):
Here is the code that the plugin uses to link (I think):
add_action( 'woocommerce_cart_is_empty', 'hpy_cs_output_notice', 1 );
function hpy_cs_output_notice() {
$display_empty = get_option( 'hpy_cs_empty_cart_notice' );
if ( $display_empty == 'yes' ) {
$link = wc_custom_redirect_continue_shopping();
$message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s',
esc_url($link), esc_html__('Continue shopping', 'woocommerce'),
esc_html(get_option('hpy_cs_empty_cart_text', __('Your cart is empty.', '
$message = sprintf( \'<a href="%s" class="button wc-forward">%s</a> %s\',
esc_url( $link ), esc_html__( \'Continue shopping\', \'woocommerce\' ),
esc_html( get_option( \'hpy_cs_empty_cart_text\', __( \'Your cart is
empty.\', \'hpy_cshpy_cshpy_cs\' ) ) ) );
'))));
wc_print_notice($message);
}
}
推荐答案
没有内置函数可检索上次查看的类别页面.但是,我对如何解决这个问题有一些想法.
There is no built-in function to retrieve last category page viewed. But, I have some ideas on how this could be solved.
首先是创建自定义操作,检查页面是否为类别页面,如果是,则将类别ID设置为会话数据.然后在您的按钮上,使用它来获取正确的URL.
First is to create a custom action, check if page is category page, and set category ID to session data if so. Then on your button, use this to get proper URL.
/* 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'] );
}
}
然后将您的操作代码替换为:
Then you replace in your action code:
$shop_page_url = "get_permalink( wc_get_page_id( 'shop' ) )";
应成为:
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' ) );
}
第二个是检索添加到购物车中的最后一个项目,并使用该项目检索其类别URL.
Second is to retrieve last item added to cart, and use that item to retrieve its category URL.
这篇关于Woocommerce继续购物按钮链接到最后一个产品类别页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!