定制添加到购物车重定向WooCommerce中定义的产品类别3

问题描述:

在WooCommerce网站上,我希望在产品添加到购物车后立即在结账页面上重定向客户。但是,我们希望它适用于特定的产品类别,而不是所有添加到购物车按钮。定制添加到购物车重定向WooCommerce中定义的产品类别3

我试图寻找过去的解决方案,但他们要么只是当我更新functions.php或他们已经过时,不再工作时崩溃我的网站。

我真的很感激任何帮助。

+0

在,所以我们提供的编程问题的支持。通常这包括[mcve]。你能否更详细地解释你所尝试的以及为什么你认为它不起作用? –

+0

我已经更新了我的答案,测试了最后的WooCommerce版本上的所有内容,并且工作正常。请看看它并尝试一下。谢谢 – LoicTheAztec

更新时间:添加到购物车重定向不会与阿贾克斯的工作添加到购物车在商店和档案网页。

您将有那2个选项的商店和档案页面之间进行选择:

  1. 禁用AJAX添加到购物车在店,档案页面(WC设置>产品>显示)。
  2. 添加以下代码替换添加到购物车按钮,通过链接到该产品的按钮:

这第二个选项似乎是最好的:

// 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; 
} 

代码放在功能.php文件的活动儿童主题(或主题)或任何插件文件。


定制有条件REDIRECTION(对于一个定义的产品类别):

现在,您可以使用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+测试和工程