我有这个密码
/*
* Redirect to checkout after adding to cart
*/
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter(\'add_to_cart_redirect\', \'themeprefix_add_to_cart_redirect\');
问题是单击以下链接
www.example.com/?add-to-cart=1234
不会触发
add_to_cart_redirect
如果车里已经有东西了,就钩住它。它认为车里已经有东西了,没有其他东西可以添加<一次只能购买我的一种产品
Where can I hook in to redirect at an earlier stage in the cart validation process?
或
Is there a way to empty the cart before adding the product 1234
?
谢谢
最合适的回答,由SO网友:italiansoda 整理而成
Q: Is there a way to empty the cart before adding the product 1234?
A: 在将产品添加到购物车之前,可以清空购物车,以便
add_to_cart_redirect
钩子将始终被调用。使用
woocommerce_add_to_cart_validation
钩子在添加新项目之前更改购物车。
/**
* First clear the cart of all products
*/
function clear_cart_before_adding( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return true;
}
add_filter( \'woocommerce_add_to_cart_validation\', \'clear_cart_before_adding\' );
/**
* Redirect to checkout after adding to cart
*/
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
add_filter(\'add_to_cart_redirect\', \'themeprefix_add_to_cart_redirect\');