Add ‘Quick Add to Cart’ Button on WooCommerce Product Pages

How to add quick add to cart button in woocommerce; Woocommerce quick purchase button setup; Enable buy now button on product page woocommerce; Woocommerce add to cart button customization; Add quick checkout button to woocommerce product page; Woocommerce direct checkout button on product page; Customize add to cart button woocommerce; Woocommerce quick add to cart button plugin; How to change add to cart button text in woocommerce; Woocommerce quick purchase button on shop page;

Explanation

Here's how you can add a 'Quick Add to Cart' button on your WooCommerce product pages:

  • Display the Button: The code hooks into the product display area and adds a 'Quick Add to Cart' button. This button appears if the product is available for purchase and in stock.
  • Button Functionality: When clicked, the button adds the product to the cart and redirects the user directly to the checkout page. This makes the purchase process faster and more convenient.
  • Custom Button Text: The text on the 'Add to Cart' button is changed to 'Quick Purchase' both on individual product pages and the shop page, making it clear to customers that they can quickly buy the product.

This setup is perfect for encouraging quick purchases by reducing the steps needed to complete a transaction.

Code

<?php
// Add 'Quick Add to Cart' button on WooCommerce product pages

// Hook to display the 'Quick Add to Cart' button on the product page
add_action('woocommerce_after_shop_loop_item', 'wp_dudecom_quick_add_to_cart_button', 15);

function wp_dudecom_quick_add_to_cart_button() {
    global $product;

    // Check if the product is purchasable and in stock
    if ($product->is_purchasable() && $product->is_in_stock()) {
        // Get the product ID
        $product_id = $product->get_id();

        // Create the 'Quick Add to Cart' button
        echo '<a href="' . esc_url( add_query_arg('add-to-cart', $product_id) ) . '" class="button wp-dudecom-quick-add-to-cart">' . esc_html__('Quick Add to Cart', 'woocommerce') . '</a>';
    }
}

// Redirect to checkout page after adding product to cart
add_action('template_redirect', 'wp_dudecom_redirect_to_checkout');

function wp_dudecom_redirect_to_checkout() {
    if (isset($_GET['add-to-cart'])) {
        // Redirect to the checkout page
        wp_safe_redirect(wc_get_checkout_url());
        exit;
    }
}

// Change the 'Add to Cart' button text on the product page
add_filter('woocommerce_product_single_add_to_cart_text', 'wp_dudecom_custom_add_to_cart_text');

function wp_dudecom_custom_add_to_cart_text() {
    return __('Quick Purchase', 'woocommerce');
}

// Change the 'Add to Cart' button text on the shop page
add_filter('woocommerce_product_add_to_cart_text', 'wp_dudecom_custom_add_to_cart_text_shop');

function wp_dudecom_custom_add_to_cart_text_shop() {
    return __('Quick Purchase', 'woocommerce');
}
?>

Instructions

To add a 'Quick Add to Cart' button on your WooCommerce product pages, follow these steps:

File Location: You will need to add the code to your theme's functions.php file or create a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.
  • Backup your site before making changes to the code.

Implementation Steps:

  1. Access your WordPress dashboard and navigate to Appearance > Theme Editor or use an FTP client to access your theme files.
  2. Open the functions.php file of your active theme. Alternatively, if you prefer using a plugin, create a new PHP file in your plugins directory and open it for editing.
  3. Copy the provided code snippet and paste it at the end of the functions.php file or your custom plugin file.
  4. Save the changes to the file.
  5. Visit your WooCommerce product pages to verify that the 'Quick Add to Cart' button appears and functions as expected.

By following these steps, you will have successfully added a 'Quick Add to Cart' button to your WooCommerce product pages, enhancing the shopping experience by allowing customers to quickly purchase products.

If you need help with implementation or require more advanced functionality, consider using the services of wp-dude.com.