How to Password Protect Proceed to Checkout button in WooCommerce

You can hide the Proceed to Checkout (PC) button when either Cart page is password protected or some products in cart are password protected.

  1. When Cart page is protected
  2. When some product pages are protected

Requirements:

1. When Cart page is protected

Once a WooCommerce Cart page is password protected with our PPWP Pro plugin, the whole page will be hidden under a password form by default.

If you want to hide the Proceed to Checkout button only, simply add the following code snippet to your (child) theme’s functions.php file.

/**
 * Hide Proceed to Checkout button if Cart page is password protected.
 */
function ppwp_replace_proceed_to_checkout()
{
    echo get_the_password_form();
}

add_filter(
    'post_password_required',
    function ($required, $post) {
        global $post;
        if (!$post->ID) {
            return $required;
        }

        if (!function_exists('is_cart')) {
            return $required;
        }

        if ($required && is_cart()) {
            remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
            add_action('woocommerce_proceed_to_checkout', 'ppwp_replace_proceed_to_checkout');

            return false;
        }

        return $required;
    },
    200,
    2
);

Your WooCommerce cart page will display as below.

2. When some product pages are protected

To hide Proceed to Checkout button when there is at least one password protected product in the customer’s cart, simply use the following code snippet instead.

// Ensure protected products can still be added to the cart
add_action('init', 'removeRequiredFilterOnProducts');

function removeRequiredFilterOnProducts()
{
    remove_filter('woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart');
}

function ppwp_replace_proceed_to_checkout()
{
    echo get_the_password_form();
}

add_filter(
    'post_password_required',
    function ($required, $post) {
        global $post;
        if (!$post->ID) {
            return $required;
        }

        if (!function_exists('is_cart')) {
            return $required;
        }
        if ($required && is_cart()) {

            if (class_exists('PPW_Pro_Password_Services')) {
                global $woocommerce;
                $items = $woocommerce->cart->get_cart();
                $passRequired = false;
                $password_service = new PPW_Pro_Password_Services();
                foreach ($items as $item => $values) {
                    // Retrieve WC_Product object from the product-id:
                    $_woo_product = wc_get_product($values['product_id']);
                    if ($password_service->is_protected_content($_woo_product->get_id())) {
                        $passRequired = true;
                    }
                }
            }

            if ($passRequired === true) {
                remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);
                add_action('woocommerce_proceed_to_checkout', 'ppwp_replace_proceed_to_checkout');
                return false;
            } else {
                return false;
            }
        }
        return $required;
    },
    200,
    2
);
Lasted updated on May 17, 2021