Woocommerce PMPro skip cart for free products

By , last updated July 15, 2019

With memebrships on Woocommerce websites you have probably run into a problem of too long checkout processes for products that become free. It is especially the case for digital downloads.

With Woocommerce you can’t just make downloadable products free after your visitors buy a membership.

Here’s what you can do to make the experience of your users better in a Woocommerce store:

  1. Skip Cart and proceed directly to the Checkout page.
  2. Change the text of the “Add to cart” buttons to something like “Download”.

Here is how to implement it in a WordPress website with Woocommerce and Paid Membership Pro plugins:

Step 1. Uncheck “Redirect to the cart page” option for all your products under Woocommerce -> Products -> General

Step 2. Redirect users with membership to the Checkout page after they are finished shopping. Skip the Cart page.

Add the following function to your functions.php file:

add_filter( 'woocommerce_get_script_data', 'redirect_view_cart',10,2 );
function redirect_view_cart( $params, $handle )
{
	 if(function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()) {
		switch ($handle) {
			case 'wc-add-to-cart':
				$params['i18n_view_cart'] = "Show order"; 
				$params['cart_url'] = wc_get_checkout_url();
			break;
		}
	}
    return $params;
}

In this function you will redirect users directly to checkout only if the user has an active membership level. You can check it by calling a paid membership pro function pmpro_hasMembershipLevel().

Once we are sure that the user has a membership, we set a new URL for the parameter ‘cart_url’ in woocommerce. You can get the cart URL by calling a woocommerce function wc_get_checkout_url().

Step 3. Change the text of the “Add to cart” button to something more suitable, for example “Download”:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'change_add_to_cart_button', 20, 2 ); 
add_filter( 'woocommerce_product_add_to_cart_text', 'change_add_to_cart_button', 20, 2 ); 

function change_add_to_cart_button( $button_text, $product ) {
    if(function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()) {
        $button_text = __("Download", "woocommerce");
    }
    return $button_text;
}

You would need to change the button text on both single product pages and in posts. Thus, you need to override button text in 2 woocommerce filters: “woocommerce_product_single_add_to_cart_text” and “woocommerce_product_add_to_cart_text“.

Now, the ideal would be to make users download directly.

In Woocommerce nothing is simple. At least in this case you will get “orders” for all the free products your members download. This will also give you stats on which products are the most popular ones.