Snippet

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

How to add quick add to cart button in woocommerceWoocommerce quick purchase button setupEnable buy now button on product page woocommerceWoocommerce add to cart button customizationAdd quick checkout button to woocommerce product pageWoocommerce direct checkout button on product pageCustomize add to cart button woocommerceWoocommerce quick add to cart button pluginHow to change add to cart button text in woocommerceWoocommerce 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

1<?php 2// Add 'Quick Add to Cart' button on WooCommerce product pages 3 4// Hook to display the 'Quick Add to Cart' button on the product page 5add_action('woocommerce_after_shop_loop_item', 'wp_dudecom_quick_add_to_cart_button', 15); 6 7function wp_dudecom_quick_add_to_cart_button() { 8 global $product; 9 10 // Check if the product is purchasable and in stock 11 if ($product->is_purchasable() && $product->is_in_stock()) { 12 // Get the product ID 13 $product_id = $product->get_id(); 14 15 // Create the 'Quick Add to Cart' button 16 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>'; 17 } 18} 19 20// Redirect to checkout page after adding product to cart 21add_action('template_redirect', 'wp_dudecom_redirect_to_checkout'); 22 23function wp_dudecom_redirect_to_checkout() { 24 if (isset($_GET['add-to-cart'])) { 25 // Redirect to the checkout page 26 wp_safe_redirect(wc_get_checkout_url()); 27 exit; 28 } 29} 30 31// Change the 'Add to Cart' button text on the product page 32add_filter('woocommerce_product_single_add_to_cart_text', 'wp_dudecom_custom_add_to_cart_text'); 33 34function wp_dudecom_custom_add_to_cart_text() { 35 return __('Quick Purchase', 'woocommerce'); 36} 37 38// Change the 'Add to Cart' button text on the shop page 39add_filter('woocommerce_product_add_to_cart_text', 'wp_dudecom_custom_add_to_cart_text_shop'); 40 41function wp_dudecom_custom_add_to_cart_text_shop() { 42 return __('Quick Purchase', 'woocommerce'); 43} 44?>

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.