Changing WooCommerce “Place Order”/”Donate now” Button Text based on Cart Contents

Sometimes, you may want to customize the text of the “Place Order”/”Donate now” button in WooCommerce based on the products present in the cart. For instance, if a specific product, such as the one with ID 91, is in the cart, you might want to display a different call-to-action like “Support Us” instead of the usual “Place Order”.

To achieve this customization, you can use WordPress hooks and filters provided by WooCommerce. The woocommerce_order_button_text filter allows you to change the text of the “Place Order” button during the checkout process.

Here’s a code snippet to achieve this:

add_filter('woocommerce_order_button_text', 'change_order_button_text', 15, 1);

function change_order_button_text($button_text) {
    // Check if product with ID 91 is in the cart
    if (is_product_in_cart(91)) {
        $button_text = 'Support Us';
    }

    return $button_text;
}

function is_product_in_cart($product_id) {
    // Check if the product is in the cart
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $product_in_cart = $cart_item['product_id'];
        if ($product_in_cart === $product_id) {
            return true;
        }
    }

    return false;
}

In this code, we define a function to check if a specific product is in the cart (is_product_in_cart()). We then utilize this function within another function (change_order_button_text()) which filters the text displayed on the “Place Order” button.

To apply this code, replace 91 with the actual product ID you want to monitor. Insert this code into your WordPress theme’s functions.php file or within a custom plugin.

Always remember to test any customizations in a staging or development environment to ensure they work as expected before implementing them on a live website.