特定产品 ID 的 WooCommerce 添加到购物车自定义重定向
WooCommerce Add to Cart custom redirection for specific product IDs
我正在尝试加入“添加到购物车”重定向,更改特定产品 ID 的 URL
作为起点,我找到了以下代码(来源:https://jeroensormani.com/redirect-users-after-add-to-cart/)
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' );
这似乎得到了其他人的认可。对我来说,当条件适用时,它似乎忽略了重定向注入。忽略条件时有效:
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' );
我的 conclusion/guess 是,条件或过滤器中有些内容已过时,但我无法弄清楚哪里出了问题。我研究了其他基于 woocommerce_add_to_cart_redirect 挂钩的模拟程序代码,并尝试了无数片段和函数组合,但无论何时应用条件,我都无法使它们工作。
[编辑]现在想想,很有可能是查询问题。但不太确定该怎么做。
任何帮助将不胜感激。
首先 woocommerce_add_to_cart_redirect
过滤器挂钩有 2 个可用参数。所以是的,您找到的代码有点过时了。
对于每个人来说,如果您在 WooCommerce 设置中启用 “成功添加后重定向到购物车页面” 选项 >产品 (制表符)。
现在,WooCommerce 中有两种添加到购物车的方式:
- Ajax加入购物车,
- 和普通加入购物车。
挂钩 woocommerce_add_to_cart_redirect
仅适用于 普通 添加到购物车以定位特定产品,因为您无法使用 [=75 处理产品 ID =] 添加到购物车以进行重定向 (见下文).
对于 Ajax 添加到购物车 the hook is defined in here 喜欢:
'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url(), null ),
其中第二个参数设置为 null
.
对于正常加入购物车the hook is defined in here喜欢:
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
其中 $adding_to_cart
(第二个参数) 是产品对象 defined in here 如:
$adding_to_cart = wc_get_product( $product_id );
这是 正常添加到购物车 处理特定产品 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;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并适用于正常添加到购物车。
To handle Ajax add to cart redirection to a custom Url for specific products, Some jQuery code will be required…
好的,一直在努力解决这个错误。
添加 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-includes/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
这似乎与 UNI-CPO 插件冲突。禁用插件后,问题就消失了。我很难弄清楚,如果这是代码段或插件的错误,以及如何解决它,如果可能的话。
我正在尝试加入“添加到购物车”重定向,更改特定产品 ID 的 URL 作为起点,我找到了以下代码(来源:https://jeroensormani.com/redirect-users-after-add-to-cart/)
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' );
这似乎得到了其他人的认可。对我来说,当条件适用时,它似乎忽略了重定向注入。忽略条件时有效:
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' );
我的 conclusion/guess 是,条件或过滤器中有些内容已过时,但我无法弄清楚哪里出了问题。我研究了其他基于 woocommerce_add_to_cart_redirect 挂钩的模拟程序代码,并尝试了无数片段和函数组合,但无论何时应用条件,我都无法使它们工作。
[编辑]现在想想,很有可能是查询问题。但不太确定该怎么做。
任何帮助将不胜感激。
首先 woocommerce_add_to_cart_redirect
过滤器挂钩有 2 个可用参数。所以是的,您找到的代码有点过时了。
对于每个人来说,如果您在 WooCommerce 设置中启用 “成功添加后重定向到购物车页面” 选项 >产品 (制表符)。
现在,WooCommerce 中有两种添加到购物车的方式:
- Ajax加入购物车,
- 和普通加入购物车。
挂钩 woocommerce_add_to_cart_redirect
仅适用于 普通 添加到购物车以定位特定产品,因为您无法使用 [=75 处理产品 ID =] 添加到购物车以进行重定向 (见下文).
对于 Ajax 添加到购物车 the hook is defined in here 喜欢:
'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url(), null ),
其中第二个参数设置为 null
.
对于正常加入购物车the hook is defined in here喜欢:
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
其中 $adding_to_cart
(第二个参数) 是产品对象 defined in here 如:
$adding_to_cart = wc_get_product( $product_id );
这是 正常添加到购物车 处理特定产品 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;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并适用于正常添加到购物车。
To handle Ajax add to cart redirection to a custom Url for specific products, Some jQuery code will be required…
好的,一直在努力解决这个错误。 添加 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-includes/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
这似乎与 UNI-CPO 插件冲突。禁用插件后,问题就消失了。我很难弄清楚,如果这是代码段或插件的错误,以及如何解决它,如果可能的话。