[英]Skip cart page for a few products redirecting to checkout
I would like to bypass cart page and redirect user to checkout page for a few products. 我想绕过购物车页面并将用户重定向到一些产品的结帐页面。
I have created the add to cart link for the product <a href="http://example.net/?add-to-cart=1461">Product Name</a>
我已为
<a href="http://example.net/?add-to-cart=1461">Product Name</a>
创建了产品的添加到购物车链接
And I have the code below 我有下面的代码
add_filter( 'woocommerce_add_to_cart_redirect', 'woo_redirect_checkout' );
function woo_redirect_checkout() {
global $woocommerce;
$desire_product = 1461;
//Get product ID
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
//Check if current product is subscription
if ( $product_id == $desire_product ){
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
exit;
} else {
$cart_url = $woocommerce->cart->get_cart_url();
return $cart_url;
exit;
}
}
from How to skip cart page on woocomerce for certain products only? 来自如何仅针对某些产品跳过woocomerce上的购物车页面? .
。 But the url redirect me to homepage instead.
但该网址改为将我重定向到主页。 Just wondering where is the issue,
只是想知道问题在哪里,
I have unchecked the add to cart behaviour at woocommerce settings as well. 我也在woocommerce设置中取消选中了添加到购物车的行为。
Thanks in advance. 提前致谢。
I chose another approach and a WordPress hook instead of woocommerce. 我选择了另一种方法和WordPress钩子而不是woocommerce。 This is based on this answer: WooCommerce - Skip cart page redirecting to checkout page
这是基于这个答案: WooCommerce - 跳过购物车页面重定向到结帐页面
This is that code: 这是代码:
function skip_cart_page_redirection_to_checkout() {
// desired product id redirection
$product_id = 1461;
$items_ids = array();
// Get all items IDs that are in cart
foreach( WC()->cart->get_cart() as $item ) {
$items_ids[] = $item['product_id'];
}
// If is cart page and the desired peoduct is in cart => redirect to checkout.
if( is_cart() && in_array($product_id, $items_ids) )
// WooCommerce 3.0 compatibility added
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
wp_redirect( WC()->cart->get_checkout_url() ); // Older than 3.0
} else {
wp_redirect( wc_get_checkout_url() ); // 3.0+ (Thanks to helgatheviking)
}
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file. 此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。
The code is tested and fully functional. 代码经过测试并且功能齐全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.