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". Added cart button functionalities with perfect height & padding control. Now includes independent styling and border controls for both buttons. Version: 1.0.5 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 (Only Shop/Loop now per request) add_action('woocommerce_after_shop_loop_item', [ $this, 'add_buy_now_button' ], 15); // 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() { // If you have a separate style.css, it will load here. wp_enqueue_style('buy-now-button-style', plugins_url('style.css', __FILE__)); } // Add Buy Now/Cart button to shop/archive pages public function add_buy_now_button() { global $product; $display_mode = get_option('buy_now_display_mode', 'only_buy_now'); $cart_style = get_option('buy_now_cart_style', 'style_1'); $button_text = esc_html(get_option('buy_now_button_text', 'Buy Now')); $cart_text = esc_html(get_option('buy_now_cart_text', 'Cart')); // Buy Now Logic if ( $product->is_type('variable') ) { $buy_now_url = $product->get_permalink(); $cart_url = $product->get_permalink(); $cart_class = 'button product_type_variable add_to_cart_button'; } else { $buy_now_url = add_query_arg('buy_now', $product->get_id(), home_url()); $cart_url = '?add-to-cart=' . $product->get_id(); // Added WooCommerce default AJAX classes $cart_class = 'button product_type_simple add_to_cart_button ajax_add_to_cart'; } if ( $display_mode === 'buy_now_with_cart' ) { // Cart Icon SVG $cart_icon = '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:block; margin:auto;"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>'; $cart_content = ($cart_style === 'style_2') ? $cart_icon : $cart_text; $cart_btn = sprintf( '<a href="%s" data-product_id="%s" class="advaxe-cart-btn %s">%s</a>', esc_url($cart_url), esc_attr($product->get_id()), esc_attr($cart_class), $cart_content ); // "in-group" class added for group styling $buy_now_btn_group = '<a href="' . esc_url($buy_now_url) . '" class="buy-now-button in-group">' . $button_text . '</a>'; echo '<div class="advaxe-btn-group advaxe-' . esc_attr($cart_style) . '">'; echo $cart_btn; echo $buy_now_btn_group; echo '</div>'; } else { // Only Buy Now $buy_now_btn_single = '<a href="' . esc_url($buy_now_url) . '" class="buy-now-button">' . $button_text . '</a>'; echo $buy_now_btn_single; } } // 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; } } } // 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() { // General register_setting('buy_now_settings_group', 'buy_now_display_mode'); register_setting('buy_now_settings_group', 'buy_now_cart_style'); register_setting('buy_now_settings_group', 'buy_now_button_padding'); register_setting('buy_now_settings_group', 'buy_now_button_margin'); register_setting('buy_now_settings_group', 'buy_now_button_text_size'); // Buy Now Button Settings register_setting('buy_now_settings_group', 'buy_now_button_text'); 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_border_toggle'); register_setting('buy_now_settings_group', 'buy_now_button_border_color'); register_setting('buy_now_settings_group', 'buy_now_button_border_width'); // Cart Button Settings register_setting('buy_now_settings_group', 'buy_now_cart_text'); register_setting('buy_now_settings_group', 'buy_now_cart_button_color'); register_setting('buy_now_settings_group', 'buy_now_cart_button_text_color'); register_setting('buy_now_settings_group', 'buy_now_cart_button_border_toggle'); register_setting('buy_now_settings_group', 'buy_now_cart_button_border_color'); register_setting('buy_now_settings_group', 'buy_now_cart_button_border_width'); } // 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'); ?> <h2 style="margin-top: 30px;">General Settings</h2> <table class="form-table"> <tr valign="top"> <th scope="row">Display Mode</th> <td> <select name="buy_now_display_mode"> <option value="only_buy_now" <?php selected(get_option('buy_now_display_mode', 'only_buy_now'), 'only_buy_now'); ?>>Only Buy Now</option> <option value="buy_now_with_cart" <?php selected(get_option('buy_now_display_mode'), 'buy_now_with_cart'); ?>>Buy Now with Cart</option> </select> </td> </tr> <tr valign="top"> <th scope="row">Cart Button Style (If Cart enabled)</th> <td> <select name="buy_now_cart_style"> <option value="style_1" <?php selected(get_option('buy_now_cart_style', 'style_1'), 'style_1'); ?>>Style 1 - Flex Left Cart & Right Buy Now</option> <option value="style_2" <?php selected(get_option('buy_now_cart_style'), 'style_2'); ?>>Style 2 - Left Cart Icon Only & Right Buy Now</option> </select> </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')); ?>" /> <p class="description">Example: 10px 20px (Controls both buttons identically)</p> </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> </table> <hr style="margin: 30px 0;"> <h2>Buy Now Button Styles</h2> <table class="form-table"> <tr valign="top"> <th scope="row">Buy Now 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">Background 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">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">Border Toggle</th> <td> <select name="buy_now_button_border_toggle"> <option value="off" <?php selected(get_option('buy_now_button_border_toggle', 'off'), 'off'); ?>>Off</option> <option value="on" <?php selected(get_option('buy_now_button_border_toggle'), 'on'); ?>>On</option> </select> </td> </tr> <tr valign="top"> <th scope="row">Border Color</th> <td><input type="color" name="buy_now_button_border_color" value="<?php echo esc_attr(get_option('buy_now_button_border_color', '#000000')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Border Width</th> <td> <input type="text" name="buy_now_button_border_width" value="<?php echo esc_attr(get_option('buy_now_button_border_width', '1px')); ?>" /> <p class="description">Example: 1px, 2px</p> </td> </tr> </table> <hr style="margin: 30px 0;"> <h2>Cart Button Styles</h2> <table class="form-table"> <tr valign="top"> <th scope="row">Cart Button Text</th> <td><input type="text" name="buy_now_cart_text" value="<?php echo esc_attr(get_option('buy_now_cart_text', 'Cart')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Background Color</th> <td><input type="color" name="buy_now_cart_button_color" value="<?php echo esc_attr(get_option('buy_now_cart_button_color', '#eeeeee')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Text Color</th> <td><input type="color" name="buy_now_cart_button_text_color" value="<?php echo esc_attr(get_option('buy_now_cart_button_text_color', '#333333')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Border Toggle</th> <td> <select name="buy_now_cart_button_border_toggle"> <option value="off" <?php selected(get_option('buy_now_cart_button_border_toggle', 'off'), 'off'); ?>>Off</option> <option value="on" <?php selected(get_option('buy_now_cart_button_border_toggle'), 'on'); ?>>On</option> </select> </td> </tr> <tr valign="top"> <th scope="row">Border Color</th> <td><input type="color" name="buy_now_cart_button_border_color" value="<?php echo esc_attr(get_option('buy_now_cart_button_border_color', '#cccccc')); ?>" /></td> </tr> <tr valign="top"> <th scope="row">Border Width</th> <td> <input type="text" name="buy_now_cart_button_border_width" value="<?php echo esc_attr(get_option('buy_now_cart_button_border_width', '1px')); ?>" /> <p class="description">Example: 1px, 2px</p> </td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php } } new BuyNowButtonPlugin(); // Inline styles add_action('wp_head', function() { // General Settings $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')); // Buy Now Colors & Border $bn_color = esc_attr(get_option('buy_now_button_color', '#000000')); $bn_text_color = esc_attr(get_option('buy_now_button_text_color', '#ffffff')); $bn_border_toggle = get_option('buy_now_button_border_toggle', 'off'); $bn_border_color = esc_attr(get_option('buy_now_button_border_color', '#000000')); $bn_border_width = esc_attr(get_option('buy_now_button_border_width', '1px')); $bn_border_css = ($bn_border_toggle === 'on') ? "border: {$bn_border_width} solid {$bn_border_color} !important;" : "border: none !important;"; // Cart Colors & Border $cart_color = esc_attr(get_option('buy_now_cart_button_color', '#eeeeee')); $cart_text_color = esc_attr(get_option('buy_now_cart_button_text_color', '#333333')); $cart_border_toggle = get_option('buy_now_cart_button_border_toggle', 'off'); $cart_border_color = esc_attr(get_option('buy_now_cart_button_border_color', '#cccccc')); $cart_border_width = esc_attr(get_option('buy_now_cart_button_border_width', '1px')); $cart_border_css = ($cart_border_toggle === 'on') ? "border: {$cart_border_width} solid {$cart_border_color} !important;" : "border: none !important;"; echo "<style> /* Base styles for both buttons to guarantee identical padding and height */ .buy-now-button, .advaxe-btn-group .advaxe-cart-btn { padding: {$padding} !important; font-size: {$text_size} !important; border-radius: 5px !important; text-decoration: none !important; display: inline-flex !important; align-items: center !important; justify-content: center !important; box-sizing: border-box !important; line-height: normal !important; min-height: 0 !important; /* Resets theme default min-heights */ text-align: center !important; vertical-align: middle !important; transition: all 0.3s ease; } /* Buy Now specific styles */ .buy-now-button { background-color: {$bn_color} !important; color: {$bn_text_color} !important; {$bn_border_css} cursor: pointer; font-weight: 600 !important; } .buy-now-button:hover { opacity: 0.85; } /* Cart specific styles */ .advaxe-btn-group .advaxe-cart-btn { background-color: {$cart_color} !important; color: {$cart_text_color} !important; {$cart_border_css} } .advaxe-btn-group .advaxe-cart-btn:hover { opacity: 0.85; } /* Single Buy Now margin (when NOT in the flex group) */ .buy-now-button:not(.in-group) { margin: {$margin} !important; } /* Flex Group Container */ .advaxe-btn-group { display: flex !important; flex-direction: row !important; gap: 4px !important; margin: {$margin} !important; align-items: stretch !important; /* Ensures equal heights */ width: 100% !important; box-sizing: border-box !important; } /* Reset margins inside group */ .advaxe-btn-group .buy-now-button, .advaxe-btn-group .advaxe-cart-btn { margin: 0 !important; } /* Style 1: Both buttons take exactly 50% width */ .advaxe-style_1 .advaxe-cart-btn, .advaxe-style_1 .buy-now-button { flex: 1 1 0% !important; width: 100% !important; } /* Style 2: Cart icon auto width, Buy Now takes remaining */ .advaxe-style_2 .buy-now-button { flex: 1 1 auto !important; } .advaxe-style_2 .advaxe-cart-btn { flex: 0 0 auto !important; padding-left: 10px !important; padding-right: 10px !important; /* Keeps vertical padding same as Buy Now, but limits horizontal to make it square-ish */ } </style>"; });