继续购物在woocommerce重定向到上一个类别


Continue Shopping redirect to previous category in woocommerce

我正在使用Woocommerce创建一个电子商务网站,我需要一些重定向的帮助。所以布局是类别1类别2等的图像

点击类别1,打开类别1a,打开类别1b等。点击类别1a,打开产品列表。

如果用户在类别1a的第3页,点击产品。它会转到单个产品页面,当他们点击添加到购物车时。它现在进入购物车页面,并提供继续购物的选项。当用户单击"继续购物"时,我希望它返回到category1a页面3,或者如果更简单的话,返回到用户所在的特定类别。

使用下面的代码,我成功地将其重定向到主商店页面,但客户端要求在点击产品之前转到用户所在的位置。

add_filter( 'woocommerce_add_to_cart_message', 'woocommrece_custom_add_to_cart_message' );

函数woocommrece_custom_add_to_cart_message() {

global $woocommerce;

//输出消息

if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
    $return_to = get_permalink( woocommerce_get_page_id( 'shop' ) ); // Give the url, you want to redirect which would be previous location before user clicked on product
    $message   = sprintf( '<a href="%s" class="button">%s</a> %s', $return_to, __( 'More Donation Options &rarr;', 'woocommerce' ), __( 'Donation successfully added to your cart.', 'woocommerce' ) );
} else {
    $message = sprintf( '<a href="%s">%s</a> %s', get_permalink( woocommerce_get_page_id( 'cart' ) ), __( 'View Cart &rarr;', 'woocommerce' ), __( 'added to cart.', 'woocommerce' ) );
}

这将适用于primary category…添加到functions.php

add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message', 10, 2 );
function custom_add_to_cart_message( $message, $product_id) {
    $terms = get_the_terms( $product_id, 'product_cat' );
    $return_to = get_term_link( $terms[0], 'product_cat');
    $message    = sprintf( '<a href="%s" class="button wc-forwards">%s</a> %s', $return_to, __( 'Continue Shopping?', 'woocommerce' ), __( 'Your cart was updated.', 'woocommerce' )  );
return $message;
}