woocommerce button to add two products to cart at once

Viewed 37

So basically I am just finishing the build of an online course website sing wordpress/woo and trying to find a way I can bundle these product courses together so that one button will add multiple products to the basket.

I'm using Tutor LMS 2.0 and I have found some code that almost does exactly what I need, just with one caveat...it redirects to the homepage once the button is clicked...

My landing page is here (https://my.betterlifedesign.com/ultimate-bundle/) and the current button is at the bottom with the redirect to homepage.

Would anyone know how I can tweak the below code to have it redirect to the checkout page instead of the homepage after button click? Help would be so much appreciated!

private $prevent_redirect = false; //used to prevent WC from redirecting if we have more to process

function __construct() {
    if ( ! isset( $_REQUEST[ 'add-more-to-cart' ] ) ) return; //don't load if we don't have to
    $this->prevent_redirect = 'no'; //prevent WC from redirecting so we can process additional items
    add_action( 'wp_loaded', [ $this, 'add_more_to_cart' ], 21 ); //fire after WC does, so we just process extra ones
    add_action( 'pre_option_woocommerce_cart_redirect_after_add', [ $this, 'intercept_option' ], 9000 ); //intercept the WC option to force no redirect
}

function intercept_option() {
    return $this->prevent_redirect;
}

function add_more_to_cart() {
    $product_ids = explode( ',', $_REQUEST['add-more-to-cart'] );
    $count       = count( $product_ids );
    $number      = 0;

    foreach ( $product_ids as $product_id ) {
        if ( ++$number === $count ) $this->prevent_redirect = false; //this is the last one, so let WC redirect if it wants to.
        $_REQUEST['add-to-cart'] = $product_id; //set the next product id
        WC_Form_Handler::add_to_cart_action(); //let WC run its own code
    }
} 

new add_more_to_cart;
1 Answers

First adjust the following setting in WooCommerce:

enter image description here

Then add the following code to your functions.php

add_filter('woocommerce_add_to_cart_redirect', function () {
    return wc_get_checkout_url();
});
Related