Scriptfeeds Blog
  • Blog
  • WordPress
    • Woocommerce
    • WordPress Errors
    • WordPress Plugins
  • Development
    • WordPress Themes
  • Hire us
Cart / $0.00

No products in the cart.

No Result
View All Result
Scriptfeeds Blog
  • Blog
  • WordPress
    • Woocommerce
    • WordPress Errors
    • WordPress Plugins
  • Development
    • WordPress Themes
  • Hire us
Cart / $0.00

No products in the cart.

No Result
View All Result
Scriptfeeds Blog
No Result
View All Result
Home Wordpress Woocommerce

How to add buy now button in WooCommerce Variable Product

Scriptfeeds by Scriptfeeds
May 14, 2022
in Woocommerce, Wordpress
0
84
SHARES
Share on FacebookShare on TwitterWhatsapp this Blog

WooCommerce has vast of customization by which web shop owner can build the online web shop the way they want. By default WooCommerce has “ADD TO CART” button on all products. In this article we going to guide you how you can add the “BUY NOW” button with the “ADD TO CART” button on single and variable WooCommerce products.

But if you want to change “ADD TO CART” button to “ADDED TO CART” when product added in cart then you can click here.

Step by step guide to add “BUY NOW” button on Single and Variable Product.

  • Create child theme and click on theme editor in “Appearance” option on left menu.
  • Add the below code on your functions.php file. You can replace text “Buy now” which another text you want.
/* Dynamic Button for Simple & Variable Product */

/**
 * Main Functions
*/

function sbw_wc_add_buy_now_button_single()
{
    global $product;
    printf( '<button id="sbw_wc-adding-button" type="submit" name="sbw-wc-buy-now" value="%d" class="single_add_to_cart_button buy_now_button button alt">%s</button>', $product->get_ID(), esc_html__( 'Buy Now', 'sbw-wc' ) );
}

add_action( 'woocommerce_after_add_to_cart_button', 'sbw_wc_add_buy_now_button_single' );



/*** Handle for click on buy now ***/

function sbw_wc_handle_buy_now()
{
    if ( !isset( $_REQUEST['sbw-wc-buy-now'] ) )
    {
        return false;
    }

    WC()->cart->empty_cart();

    $product_id = absint( $_REQUEST['sbw-wc-buy-now'] );
    $quantity = absint( $_REQUEST['quantity'] );

    if ( isset( $_REQUEST['variation_id'] ) ) {

        $variation_id = absint( $_REQUEST['variation_id'] );
        WC()->cart->add_to_cart( $product_id, 1, $variation_id );

    }else{
        WC()->cart->add_to_cart( $product_id, $quantity );
    }

    wp_safe_redirect( wc_get_checkout_url() );
    exit;
}

add_action( 'wp_loaded', 'sbw_wc_handle_buy_now' );

/* Dynamic Button for Simple & Variable Product Closed */
  • Once code added, click on SAVE and check on website.

To change the “SELECT OPTIONS” text on shop page for variable products to “ADD TO CART“, you can follow below steps.

  • Add below code in same functions.php file.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'change_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'change_cart_button_text', 10, 2 );
function change_cart_button_text( $button_text, $product ) {
    if ( $product->is_type( 'variable' ) )
        $button_text = __('Buy Now', 'woocommerce');
    return $button_text;
}

Above code is tested and Worked perfectly

So that’s all you need to do to get WooCommerce works for you. Let us know how it works for you. For instant support ping us on whatsapp and comment in comment box.

Happy coding!

[caldera_form id=”CF60ca2e3ccf64f”]

Connect with Developer
Loading
Tags: Add buy now button in woocommercechange selected option to buy now in shop page
Share34Tweet21Send

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I agree to the Terms & Conditions and Privacy Policy.

Scriptfeeds Blog

© 2022 Scriptfeeds

Blog Categories

  • WordPress
  • Development
  • Reviews
  • Marketing

Connect us on

No Result
View All Result
  • Blog
  • WordPress
    • Woocommerce
    • WordPress Errors
    • WordPress Plugins
  • Development
    • WordPress Themes
  • Hire us

© 2022 Scriptfeeds

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.