如何更改“添加到购物车"的重定向woocommerce商店中的按钮 [英] How to change the redirect of the "add to cart" button in woocommerce store

查看:337
本文介绍了如何更改“添加到购物车"的重定向woocommerce商店中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我首先对这个标题有些含糊不清表示抱歉.更具体地说,标题应为:

Let me start by saying sorry that this title is somewhat vague. More specifically the title should be:

如何在woocommerce店面主题(也可能是其他主题)产品存档页面(又称为商店页面)中选择添加到购物车"按钮的行为,以便将商品添加到购物车,然后要么停留在商店页面上,或重定向到购物车.

How to select the behavior of the "add to cart" button in a woocommerce Storefront theme (probably other themes as well) product archives page (aka shop page), such that it adds the item to the cart and then either stays on the shop page, or redirects to the cart.

但这是一个很长的标题...

But that is a long title...

推荐答案

重定向到自定义URL:

function my_custom_add_to_cart_redirect( $url ) {
    $url = get_permalink( 1 ); // URL to redirect to (1 is the page ID here)
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );


重定向到结帐:

function my_custom_add_to_cart_redirect( $url ) {
    $url = WC()->cart->get_checkout_url();
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );


重定向某些类别:

function my_custom_add_to_cart_redirect( $url ) {
    if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
        return $url;
    }
    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
    // Only redirect products that have the 't-shirts' category
    if ( has_term( 't-shirts', 'product_cat', $product_id ) ) {
        $url = WC()->cart->get_checkout_url();
    }
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

这篇关于如何更改“添加到购物车"的重定向woocommerce商店中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆