Woocommerce add to cart button redirect to checkout
In WooCommerce 3.6 or later you can use woocommerce_add_to_cart_redirect
(props @roman)
add_filter ('woocommerce_add_to_cart_redirect', function( $url, $adding_to_cart ) {
return wc_get_checkout_url();
}, 10, 2 );
Original answer:
you can use a filter in functions.php:
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
it doesn't seem to work with ajax, but it works from the single product pages, which I think is what you use
On WooCommerce (>= 2.1) the function can be simplified as:
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
Additional Add To Cart button redirecting to checkout on WooCommerce Loop
The following will do the trick, adding a custom add to cart button on WooCommerce archives that redirect to checkout after adding the product to cart:
add_action( 'woocommerce_after_shop_loop_item', 'buy_checkout_on_archive', 20 );
function buy_checkout_on_archive(){
global $product;
if ( $product->is_type('simple') ){
$product_id = $product->get_id();
$button_url = '?addtocart='.$product_id;
$button_class = 'button loop-checkout-btn';
$button_text = __('Buy & Checkout', 'woocommerce');
echo '<a href="'.$button_url.'" class="'.$button_class.'">'.$button_text.'</a>';
}
}
add_action( 'template_redirect', 'addtocart_on_archives_redirect_checkout' );
function addtocart_on_archives_redirect_checkout(){
if( isset( $_GET['addtocart'] ) && $_GET['addtocart'] > 0 ) {
WC()->cart->add_to_cart( intval($_GET['addtocart']) );
// Checkout redirection
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart
this snippet does what you asked for, and it supports ajax calls.
some points about with your code:
1- 'woocommerce_add_to_cart_redirect' fires when product is already added to cart.
2- WC()->cart->get_cart_url() is deprecated
3- will not redirect if ajax add to cart is enabled
add_filter('woocommerce_add_to_cart_validation' , 'shalior_flag_already_added_products_as_invalid' , 10 , 2);
function shalior_flag_already_added_products_as_invalid ($result , $product_id){
if (false === $result){
return $result;
}
$url = wc_get_cart_url();
foreach ( WC()->cart->get_cart() as $cart_item ) {
$cart_item_product_id = $cart_item['product_id'];
if($product_id === $cart_item['product_id'] ) {
$result = false;
}
}
if (false === $result){
add_filter('woocommerce_cart_redirect_after_error' , function (){
return wc_get_cart_url();
} , 999 );
}
return $result;
}
Additional Woocommerce product Add To Cart button that redirect to checkout
There are some mistakes in your code… Try the following:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {
global $product;
$addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
$button_class = 'single_add_to_cart_button button alt custom-checkout-btn';
$button_text = __("Buy & Checkout", "woocommerce");
if( $product->is_type( 'simple' )) :
?>
<script>
jQuery(function($) {
var url = '<?php echo $addtocart_url; ?>',
qty = 'input.qty',
button = 'a.custom-checkout-btn';
// On input/change quantity event
$(qty).on('input change', function() {
$(button).attr('href', url + '&quantity=' + $(this).val() );
});
});
</script>
<?php
echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
Add to cart and redirect to checkout for variable products in WooCommerce
Update 3
The following code will handle simple and variable products adding an additional Add to cart button that redirects to cart (with synchronized quantity).
The code works for simple and variable products as well.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {
global $product;
$addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
$button_class = 'single_add_to_cart_button button alt custom-checkout-btn';
$button_text = __("Buy & Checkout", "woocommerce");
if( $product->is_type( 'simple' )) :
?>
<script>
jQuery(function($) {
var url = '<?php echo $addtocart_url; ?>',
qty = 'input.qty',
button = 'a.custom-checkout-btn';
// On input/change quantity event
$(qty).on('input change', function() {
$(button).attr('href', url + '&quantity=' + $(this).val() );
});
});
</script>
<?php
elseif( $product->is_type( 'variable' ) ) :
$addtocart_url = wc_get_checkout_url().'?add-to-cart=';
?>
<script>
jQuery(function($) {
var url = '<?php echo $addtocart_url; ?>',
vid = 'input[name="variation_id"]',
pid = 'input[name="product_id"]',
qty = 'input.qty',
button = 'a.custom-checkout-btn';
// Once DOM is loaded
setTimeout( function(){
if( $(vid).val() != '' ){
$(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
}
}, 300 );
// On input/change quantity event
$(qty).on('input change', function() {
if( $(vid).val() != '' ){
$(button).attr('href', url + $(vid).val() + '&quantity=' + $(this).val() );
}
});
// On select attribute field change event
$('.variations_form').on('change blur', 'table.variations select', function() {
if( $(vid).val() != '' ){
$(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
}
});
});
</script>
<?php
endif;
echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
How to make a redirection to checkout on a custom add to cart button in WooCommerce
Based on your comment, to get a redirection to checkout page for a custom add to cart button, you could change the link to something like https://example.com/checkout/?add-to-cart=99
where 99
is the product Id that you want to add to cart.
So the code for your custom add to cart button link will be like (where 99
is the product Id):
$product_id = 99;
$url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
Related Topics
MySQL Alter Table Add Field Before or After a Field Already Present
Execute a PHP Script from Another PHP Script
I Have a Base64 Encoded Png, How to Write the Image to a File in PHP
Codeigniter - File Upload Required Validation
Codeigniter Assets Folder Best Practice
Arabic Characters in JSON Decoding
How to Convert Object into String in PHP
How to Select Rows Where Column Value Is Not Null Using Codeigniter's Activerecord
Difference Between Eloquent\Model::Get() and All()
Change Name of Laravel's Created_At and Updated_At
Php: Get List of All Filenames Contained Within My Images Directory
Move All Files in a Folder to Another
How to Create a New Joomla User Account from Within a Script
Differencebetween Null and Empty
Message: Trying to Access Array Offset on Value of Type Null