Home Forums WoodMart support forum Buy Now Button

Buy Now Button

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #574322

    14kabo
    Participant

    Hello i want to change the name and url of button “Buy now”
    i used loco translate plugin and change it didn’t work
    for change url i go to theme setting > single product > buy now i just see “checkout and cart page” only
    thanks .

    #574443

    Hello,

    Please try to add the code below in the functions.php file in your child theme.

    /* Create Buy Now Button dynamically after Add To Cart button */
    function add_content_after_addtocart() {
    
        // get the current post/product ID
        $current_product_id = get_the_ID();
    
        // get the product based on the ID
        $product = wc_get_product( $current_product_id );
    
        // run only on simple products
        if( $product->is_type( 'simple' ) ){
            echo '<a href="https://example.com?add-to-cart='.$current_product_id.'" class="buy-now button">Purchase Now</a>';
        }
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );

    Replace https://example.com with the URL you want the button to link to and Purchase Now with the desired button text. This code will dynamically generate the “Buy Now” button with the specified link and text for simple products.

    Best Regards.

    #574509

    14kabo
    Participant

    hello,
    i did it
    and it added new button like in screen shot
    also its appear in simple products only … i have grouped products didn’t appear in it there is screen shot also

    Attachments:
    You must be logged in to view attached files.
    #574543

    Hello,

    Please remove the previous code and try to add this code.

    function sbw_wc_add_buy_now_button_single()
    {
        global $product;
        $custom_button_text = 'Purchase Now'; // Custom button text
        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__( $custom_button_text, '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 );
        }
    
        // Redirect to custom URL
        $custom_url = 'https://example.com/custom-checkout'; // Custom URL
        wp_safe_redirect( $custom_url );
        exit;
    }
    
    add_action( 'wp_loaded', 'sbw_wc_handle_buy_now' );

    Best Regards.

    #574561

    14kabo
    Participant

    Hello,

    Please check screenshot + when i click on it (like add to cart) no redirect to custom url

    Attachments:
    You must be logged in to view attached files.
    #574605

    Hello,

    Sorry to hear about the inconvenience. Kindly, please share your Site WP-ADMIN Login details in the Private Content field so that we can check this concern on your Site and help you out accordingly.

    Best Regards.

    #574624

    14kabo
    Participant

    Hello please check

    #574691

    Hello,

    You are adding the custom url. You can do it without custom code. Navigate to layouts > Edit Single Product layout with Elementor and add the button widget next to the add to cart and add the custom link.
    https://ibb.co/sqBznmF

    Best Regards.

Viewing 8 posts - 1 through 8 (of 8 total)