Updated:添加到购物车重定向不适用于商店和档案页面上的 ajax 添加到购物车。
你将会拥有在这两个选项之间进行选择对于商店和档案页面:
- 在商店和档案页面上禁用 ajax 添加到购物车(WC 设置 > 产品 > 显示)。
- 添加以下代码,用链接到产品的按钮替换“添加到购物车”按钮:
第二个选项似乎是最好的:
// Replacing add to cart button link on shop and archives
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( $product->is_type( 'variable-subscription' ) || $product->is_type( 'variable' ) ) return $button;
$button_text = __( 'View product', 'woocommerce' );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
自定义条件重定向(对于已定义的产品类别):
现在,您可以使用挂钩的自定义函数woocommerce_add_to_cart_redirect
过滤器挂钩,当产品添加到购物车时将重定向客户(对于定义的产品分类):
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 99, 1 );
function add_to_cart_checkout_redirection( $url ) {
// HERE define your category
$category = 'clothing';
if ( ! isset( $_REQUEST['add-to-cart'] ) ) return $url;
// Get the product id when it's available (on add to cart click)
$product_id = absint( $_REQUEST['add-to-cart'] );
// Checkout redirection url only for your defined product category
if( has_term( $category, 'product_cat', $product_id ) ){
// Clear add to cart notice (with the cart link).
wc_clear_notices();
$url = wc_get_checkout_url();
}
return $url;
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
代码在 Woocommerce 3+ 上经过测试并且可以运行