JezK
Edit File: advaxe-buy-now.php
<?php /* Plugin Name: Buy Now Button for WooCommerce Plugin URI: https://advaxe.com Description: Redirects to checkout with the selected product when clicking "Buy Now". Variable products open their single product page for option selection. Version: 1.0.2 Author: Advaxe Author URI: https://advaxe.com */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } class BuyNowButtonPlugin { public function __construct() { // Admin menu and settings add_action('admin_menu', [ $this, 'add_plugin_menu' ]); add_action('admin_init', [ $this, 'register_plugin_settings' ]); // Add Buy Now button to shop and product pages add_action('woocommerce_after_shop_loop_item', [ $this, 'add_buy_now_button' ], 15); add_action('woocommerce_single_product_summary', [ $this, 'add_buy_now_button_single' ], 30); // Enqueue styles add_action('wp_enqueue_scripts', [ $this, 'enqueue_styles' ]); // Handle Buy Now action add_action('template_redirect', [ $this, 'handle_buy_now_action' ]); } // Enqueue custom styles public function enqueue_styles() { wp_enqueue_style('buy-now-button-style', plugins_url('style.css', __FILE__)); } // Add Buy Now button to shop/archive pages public function add_buy_now_button() { global $product; $button_text = esc_html(get_option('buy_now_button_text', 'Buy Now')); if ( $product->is_type('variable') ) { // For variable products, go to product page $url = $product->get_permalink(); } else { // For simple products, go directly to checkout $url = add_query_arg('buy_now', $product->get_id(), home_url()); } echo '<a href="' . esc_url($url) . '" class="buy-now-button">' . $button_text . '</a>'; } // Add Buy Now button to single product pages public function add_buy_now_button_single() { if (!get_option('buy_now_show_single_product', true)) { return; } global $product; $button_text = esc_html(get_option('buy_now_button_text', 'Buy Now')); if ( $product->is_type('variable') ) { // Variable product: Let WooCommerce handle variation selection echo '<button type="submit" name="buy_now_single" value="1" class="buy-now-button">' . $button_text . '</button>'; // JS to change form action for variable products add_action('wp_footer', function() { if (is_product()) { ?> <script> document.addEventListener('DOMContentLoaded', function(){ const buyNowBtn = document.querySelector('button[name="buy_now_single"]'); if(buyNowBtn){ buyNowBtn.addEventListener('click', function(){ const form = buyNowBtn.closest('form.cart'); if(form){ const action = '<?php echo esc_url(wc_get_checkout_url()); ?>'; form.insertAdjacentHTML('beforeend','<input type="hidden" name="is_buy_now" value="1">'); form.setAttribute('action', action); } }); } }); </script> <?php } }); } else { // Simple product: Direct to checkout $url = add_query_arg('buy_now', $product->get_id(), home_url()); echo '<a href="' . esc_url($url) . '" class="buy-now-button">' . $button_text . '</a>'; } } // Handle Buy Now redirect for simple products public function handle_buy_now_action() { if (isset($_GET['buy_now']) && is_numeric($_GET['buy_now'])) { $product_id = intval($_GET['buy_now']); if (wc_get_product($product_id)) { WC()->cart->empty_cart(); WC()->cart->add_to_cart($product_id); wp_safe_redirect(wc_get_checkout_url()); exit; } } // For variable products when coming from single product page if (isset($_POST['is_buy_now']) && $_POST['is_buy_now'] == '1' && isset($_POST['add-to-cart'])) { WC()->cart->empty_cart(); } } // Add admin menu public function add_plugin_menu() { add_menu_page( 'Buy Now Settings', 'Buy Now Button', 'manage_options', 'buy-now-settings', [ $this, 'plugin_settings_page' ], 'dashicons-cart' ); } // Register settings public function register_plugin_settings() { register_setting('buy_now_settings_group', 'buy_now_button_padding'); register_setting('buy_now_settings_group', 'buy_now_button_text_size'); register_setting('buy_now_settings_group', 'buy_now_button_color'); register_setting('buy_now_settings_group', 'buy_now_button_text_color'); register_setting('buy_now_settings_group', 'buy_now_button_text'); register_setting('buy_now_settings_group', 'buy_now_button_margin'); register_setting('buy_now_settings_group', 'buy_now_show_single_product'); } // Plugin settings page public function plugin_settings_page() { ?> <div class="wrap"> <h1>Buy Now Button Settings</h1> <form method="post" action="options.php"> <?php settings_fields('buy_now_settings_group'); ?> <?php do_settings_sections('buy_now_settings_group'); ?> <table class="form-table"> <tr valign="top"> <th scope="row">Button Text</th> <td><input type="text" name="buy_now_button_text" value="<?php echo esc_attr(get_option('buy_now_button_text', 'Buy Now')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Button Padding</th> <td><input type="text" name="buy_now_button_padding" value="<?php echo esc_attr(get_option('buy_now_button_padding', '10px 20px')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Button Margin</th> <td><input type="text" name="buy_now_button_margin" value="<?php echo esc_attr(get_option('buy_now_button_margin', '10px 0')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Text Size</th> <td><input type="text" name="buy_now_button_text_size" value="<?php echo esc_attr(get_option('buy_now_button_text_size', '16px')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Button Color</th> <td><input type="color" name="buy_now_button_color" value="<?php echo esc_attr(get_option('buy_now_button_color', '#000000')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Button Text Color</th> <td><input type="color" name="buy_now_button_text_color" value="<?php echo esc_attr(get_option('buy_now_button_text_color', '#ffffff')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Show on Single Product Page</th> <td> <input type="checkbox" name="buy_now_show_single_product" value="1" <?php checked(get_option('buy_now_show_single_product', true)); ?> /> Enable </td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php } } new BuyNowButtonPlugin(); // Inline styles add_action('wp_head', function() { $padding = esc_attr(get_option('buy_now_button_padding', '10px 20px')); $margin = esc_attr(get_option('buy_now_button_margin', '10px 0')); $text_size = esc_attr(get_option('buy_now_button_text_size', '16px')); $color = esc_attr(get_option('buy_now_button_color', '#000000')); $text_color = esc_attr(get_option('buy_now_button_text_color', '#ffffff')); echo "<style> .buy-now-button { padding: {$padding} !important; margin: {$margin} !important; font-size: {$text_size} !important; background-color: {$color} !important; color: {$text_color} !important; text-decoration: none !important; border: none; border-radius: 5px; display: inline-block; text-align: center; cursor: pointer; } .buy-now-button:hover { opacity: 0.85; } </style>"; });