简体   繁体   English

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

[英]custom add to cart redirection for a defined product category in WooCommerce 3

On a WooCommerce website I would like to be redirect customers on checkout page just after a product is added to cart. 在WooCommerce网站上,我想在产品添加到购物车后立即在结帐页面重定向客户。 However we want it to be for that specific product category instead of all the add to cart buttons. 但是我们希望它适用于特定的产品类别,而不是所有添加到购物车按钮。

I have tried to look up past solutions but they either just crash my site when I update the functions.php or they are outdated and don't work anymore. 我试图查找过去的解决方案,但他们要么在我更新functions.php时崩溃我的网站,要么他们已经过时而且不再工作了。

I really appreciate any help. 我非常感谢任何帮助。

Updated: Add to cart redirection will not work with ajax add to cart on shop and archives pages. 更新:添加到购物车重定向将无法使用ajax添加到商店和档案页面上的购物车。

You will have to choose between that 2 options for shop and archives pages: 您必须商店和档案页面的2个选项之间进行选择

  1. Disable ajax add-to-cart on shop and archives pages ( WC settings > Products > Display ). 在商店和存档页面上禁用ajax add-to-cart(WC设置>产品>显示)。
  2. Add the following code to replace the add to cart button, by a button linked to the product: 添加以下代码以通过链接到产品的按钮替换“添加到购物车”按钮:

This 2nd option seems to be the best: 第二个选项似乎是最好的:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。


THE CUSTOM CONDITIONAL REDIRECTION (For a defined product category): 定制条件重定向 (对于定义的产品类别):

Now, you can use a custom function hooked in woocommerce_add_to_cart_redirect filter hook, that will redirect customer when a product is added to cart (for a defined product category ) : 现在,您可以使用挂钩在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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

Code is tested on Woocommerce 3+ and works 代码在Woocommerce 3+上测试并且有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 添加到购物车针对特定产品 ID 的自定义重定向 - WooCommerce Add to Cart custom redirection for specific product IDs 在WooCommerce中将自定义产品添加到购物车 - Add custom product to cart in WooCommerce WooCommerce条件自定义字段,如果购物车中的产品类别 - WooCommerce Conditional Custom Fields if Product Category in Cart WooCommerce重定向到添加到购物车上的产品类别 - WooCommerce redirect to product category on add to cart 根据WooCommerce中的产品自定义字段和产品类别隐藏添加到购物车按钮 - Hide Add To Cart Button based on product custom field and product category in WooCommerce 如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车 - Prevent add to cart if cart contains a specific Woocommerce product category 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category Woocommerce 添加到自定义产品的购物车按钮 - Woocommerce Add to cart button for custom product WooCommerce使用相同的自定义参数将产品添加到购物车中 - WooCommerce add product in cart with the same custom parameter 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM