Snippets Collections
// Change the "Add to Cart" text to "Buy Now"
2
add_filter( 'woocommerce_product_single_add_to_cart_text', 'codewp_custom_cart_button_text' );
3
add_filter( 'woocommerce_product_add_to_cart_text', 'codewp_custom_cart_button_text' );
4
function codewp_custom_cart_button_text() {
5
    return __( 'Purchase', 'codewp' );
6
}
7
​
8
// Redirect users to the checkout page after clicking the "Buy Now" button
9
add_filter( 'woocommerce_add_to_cart_redirect', 'codewp_redirect_checkout_add_cart' );
10
function codewp_redirect_checkout_add_cart() {
11
    global $woocommerce;
12
    $checkout_url = wc_get_checkout_url();
13
    return $checkout_url;
14
}
15
​
16
// Remove the "Add to Cart" button and functionality
17
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
18
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
19
​
20
// Limit to one product in the cart
21
function codewp_restrict_cart_to_one_item($cart_item_data, $product_id) {
22
    global $woocommerce;
23
​
24
    // If product is being added from the shop page, redirect to checkout
25
    if (is_shop() && isset($_REQUEST['add-to-cart'])) {
26
        $checkout_url = wc_get_checkout_url();
27
        wp_safe_redirect($checkout_url);
28
        exit;
29
    }
30
​
31
    // Empty the cart before adding a new item to ensure only one product is in the cart
32
    $woocommerce->cart->empty_cart();
33
    return $cart_item_data;
34
}
35
add_filter('woocommerce_add_cart_item_data', 'codewp_restrict_cart_to_one_item', 10, 2);
36
​
37
star

Sun Feb 11 2024 21:16:44 GMT+0000 (Coordinated Universal Time)

#woocommerce #buynowbutton

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension