Php 根据产品ID有条件地添加到购物车重定向
我试图让所有产品重定向到一个自定义页面,除了3个产品去结帐(这部分工作)Php 根据产品ID有条件地添加到购物车重定向,php,wordpress,woocommerce,cart,product,Php,Wordpress,Woocommerce,Cart,Product,我试图让所有产品重定向到一个自定义页面,除了3个产品去结帐(这部分工作) 要使您的代码按预期工作,非常简单。下面我对您的代码做了一些更改: add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 ); function my_custom_add_to_cart_redirect( $url ) { $product_id = apply_filters( 'wooc
要使您的代码按预期工作,非常简单。下面我对您的代码做了一些更改:
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 );
function my_custom_add_to_cart_redirect( $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( 999, 997, 872) ) ) {
// This is more correct to get the checkout URL
$url = get_permalink( get_option('woocommerce_checkout_page_id') );
} else {
// All other products that are not in your array will be redirected to this URL
$url = get_permalink( 16 ); // URL page ID to redirect for all pages but below mentioned
}
return $url;
}
这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中
这段代码经过测试并正常工作。要使代码按预期工作,非常简单。下面我对您的代码做了一些更改:
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 );
function my_custom_add_to_cart_redirect( $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( 999, 997, 872) ) ) {
// This is more correct to get the checkout URL
$url = get_permalink( get_option('woocommerce_checkout_page_id') );
} else {
// All other products that are not in your array will be redirected to this URL
$url = get_permalink( 16 ); // URL page ID to redirect for all pages but below mentioned
}
return $url;
}
这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中
此代码经过测试并正常工作