简体   繁体   English

WooCommerce 添加到购物车针对特定产品 ID 的自定义重定向

[英]WooCommerce Add to Cart custom redirection for specific product IDs

I am trying to hook in on the Add to cart redirect, changing the URL for specific product-ID As a starting point, I found the following code (Source: https://jeroensormani.com/redirect-users-after-add-to-cart/ )我正在尝试加入添加到购物车重定向,更改特定产品 ID 的 URL 作为起点,我找到了以下代码(来源: https://jeroensormani.com/redirect-users-after-add-到购物车/ )

   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 the product IDs in the array to the checkout
    if ( in_array( $product_id, array( 1, 16, 24) ) ) {
            $url = "https://mywebsite.com/";
        }
        
        return $url;
    
    }
    add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

Which seems approved by others.这似乎得到了其他人的认可。 For me, it seems to ignore the redirect infusion when a condition is applied to it.对我来说,当应用条件时,它似乎忽略了重定向注入。 It works when the condition is left out:它在条件被忽略时起作用:

function my_custom_add_to_cart_redirect( $url ) {
    $url = "https://mywebsite.com/";
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

My conclusion/guess is, that there is something in the condition or the filter, that is outdated, but I cannot figure out what's wrong.我的结论/猜测是,条件或过滤器中有些东西已经过时,但我无法弄清楚出了什么问题。 I have researching other simuler code, based on the woocommerce_add_to_cart_redirect hook, and tried countless snippets, and combinations of functions, but I cannot get any of them working, whenever a condition is applied to it.我正在研究其他基于 woocommerce_add_to_cart_redirect 钩子的模拟器代码,并尝试了无数片段和函数组合,但无论何时应用条件,我都无法让它们中的任何一个工作。

[Edit] Now that I think about it, It could very well be a query problem. [编辑] 现在我想起来,这很可能是一个查询问题。 But not too sure how to go about that.但不太清楚如何 go 关于这一点。

Any help will be greatly appreciated.任何帮助将不胜感激。

First There are 2 available arguments for woocommerce_add_to_cart_redirect filter hook.首先有 2 个可用的 arguments 用于woocommerce_add_to_cart_redirect过滤器挂钩。 So yes the code you found is a bit outdated.所以是的,你发现的代码有点过时了。

For everyone, this hook works if you have enabled "Redirect to the cart page after successful addition" option on WooCommerce Settings > Products (tab) .对于每个人来说,如果您在 WooCommerce Settings > Products (tab)上启用了“成功添加后重定向到购物车页面”选项,则此挂钩有效。

Now, there are 2 kinds of add to cart in WooCommerce:现在,WooCommerce 中有 2 种添加到购物车:

  • Ajax add to cart, Ajax添加到购物车,
  • and Normal add to cart.正常添加到购物车。

The hook woocommerce_add_to_cart_redirect only works for Normal add to cart to target specific products, as you cant handle the product ID with Ajax add to cart for the redirection (see below) .钩子woocommerce_add_to_cart_redirect仅适用于Normal add to cart 以定位特定产品,因为您无法使用Ajax add to cart 处理产品 ID 以进行重定向(见下文)

For Ajax add to cart the hook is defined in here like:对于Ajax 添加到购物车,钩子在这里定义如下:

'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url(), null ),

where the 2nd argument is set to null .其中第二个参数设置为null


For Normal Add to cart the hook is defined in here like:对于普通添加到购物车,钩子在这里定义如下:

$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );

where $adding_to_cart (2nd argument) is the product object defined in here like:其中$adding_to_cart (第二个参数)此处定义的产品 object ,如:

$adding_to_cart = wc_get_product( $product_id );

Here is a correct example of working code for Normal Add to cart handling specific product Ids custom redirection:这是正常添加到购物车处理特定产品 ID 自定义重定向的工作代码的正确示例:

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2 );
function custom_add_to_cart_redirect( $redirect_url, $product ) {
    $product_ids = array( 37, 22, 53, 40 ); // Here set your product Ids in the array

    // Only redirect the product IDs in the array to the checkout
    if ( is_object($product) && in_array( $product->get_id(), $product_ids ) ) {
        $redirect_url = wc_get_checkout_url();
    }

    return $redirect_url;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works for normal add to cart.经过测试并适用于正常添加到购物车。

To handle Ajax add to cart redirection to a custom Url for specific products , Some jQuery code will be required…要处理Ajax 添加到购物车重定向特定产品的自定义 Url,需要一些 jQuery 代码......

Ok, been trying a lot to figure this error out.好的,一直在努力解决这个错误。 After adding the working snippet suggested by LoicTheAztec, I get this error, when loading the "Cart" page, regardless of it being empty or not.添加 LoicTheAztec 建议的工作片段后,在加载“购物车”页面时出现此错误,无论它是否为空。

Fatal error: Uncaught ArgumentCountError: Too few arguments to function pza_custom_add_to_cart_redirect(), 1 passed in /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php on line 287 and exactly 3 expected in /home/ DOMAIN /public_html/wp-content/themes/generatepress_child/functions.php: 149 Stack trace: #0 /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php(287): pza_custom_add_to_cart_redirect('https:// DOMAIN ...') #1 /home/ DOMAIN /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('https:// DOMAIN ...', Array) #2 /home/ DOMAIN /public_html/wp-content/plugins/uni-woo-custom-product-options-premium/includes/class-uni-cpo-frontend-scripts.php(959): apply_filters('woocommerce_add...', 'https:// DOMAIN ...') #3 /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php(287): Uni_Cpo_Frontend_Scripts::load_scripts('') #4 /home/ DOMAIN /public_html/wp-incl Fatal error: Uncaught ArgumentCountError: Too few arguments to function pza_custom_add_to_cart_redirect(), 1 passed in /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php on line 287 and exactly 3 expected in /home/ DOMAIN /public_html /wp-content/themes/generatepress_child/functions.php: 149 堆栈跟踪: #0 /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php(287): pza_custom_add_to_cart_redirect('https:// DOMAIN . ..') #1 /home/ DOMAIN /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('https:// DOMAIN ...', Array) #2 /home/ DOMAIN /public_html /wp-content/plugins/uni-woo-custom-product-options-premium/includes/class-uni-cpo-frontend-scripts.php(959): apply_filters('woocommerce_add...', 'https:// DOMAIN ...') #3 /home/ DOMAIN /public_html/wp-includes/class-wp-hook.php(287): Uni_Cpo_Frontend_Scripts::load_scripts('') #4 /home/ DOMAIN /public_html/wp-incl udes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #5 /home/ DOMAIN /public_ in /home/ DOMAIN /public_html/wp-content/themes/generatepress_child/functions.php on line 149 udes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #5 /home/ DOMAIN /public_ in /home/ DOMAIN /public_html/wp-content/themes/generatepress_child/functions.php on第 149 行

It seems to be a conflict with the UNI-CPO Plugin.这似乎与 UNI-CPO 插件有冲突。 When disabling the plugin, the issue is gone.禁用插件后,问题就消失了。 I am having a hard time figurering out, if it is the fault of the snippet or the plugin, and how to get around it, if at all possible.我很难弄清楚,如果它是片段或插件的错,以及如何解决它,如果可能的话。

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

相关问题 在特定产品 ID woocommerce 上添加自定义字段 - add a custom field on a specific product IDs woocommerce WooCommerce 3中定义的产品类别的自定义添加到购物车重定向 - custom add to cart redirection for a defined product category in WooCommerce 3 在WooCommerce中将自定义产品添加到购物车 - Add custom product to cart in WooCommerce 在 WooCommerce 特定 ID 产品页面上的多个产品中添加自定义内容 - Add custom content into multiple products on WooCommerce specific IDs product pages 如果产品出现在 WooCommerce 已完成的订单中,则为某些产品 ID 自定义添加到购物车按钮 - Custom add to cart button for certain product IDs if product appears in WooCommerce completed orders Woocommerce 如果购物车中有特定产品,则将特定产品添加到购物车 - Woocommerce add to cart a specific product if a specific product is in the cart Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products WooCommerce使用相同的自定义参数将产品添加到购物车中 - WooCommerce add product in cart with the same custom parameter Woocommerce 添加到自定义产品的购物车按钮 - Woocommerce Add to cart button for custom product WooCommerce 购物车上的目标特定产品 ID 显示数量 - Target specific product IDs on WooCommerce cart displayed quantity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM