• Resolved emanuelhud

    (@emanuelhud)


    Using hello theme from elementor , trying to add a buy now button . with the php code below . The buy now button works . The only problem is that it's not redirecting to checkout page as it should. Any help will be much appricated .
    
    /* 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 */
    
    

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Roxy

    (@roxannestoltz)

    Hi @emanuelhud ,

    Thanks for reaching out!

    I understand that the code you have shared does not redirect to the checkout page, yes?

    Although custom code is outside our scope of support, I have added this exact code to my test site, and it appears to be working as expected.

    I went ahead and activated the Hello Elementor theme to be sure that the issue was not a result of the theme, and the results were the same, it redirected to the Checkout page without any issues. Here is a screencast for visual reference.

    It’s possible that you have conflicting custom code, or otherwise plugins, on your site that may be overriding the behavior of this code.

    I’d suggest checking this and perhaps performing a conflict test to see if any plugins are causing the issue here.

    Additionally, you can try clearing your browser cache and refreshing your Permalinks by heading to Dashboard Settings Permalinks, and resave your existing settings, without making any changes to the settings.

    Hope this helps!

    Thread Starter emanuelhud

    (@emanuelhud)

    Thank you, the plugin that makes this conflict is Quantity Plus Minus Button for WooCommerce by CodeAstrology. any suggestion how to achieve -1+ on the quantity side on WooCommerce

    Like in the attached below:

    https://pasteboard.co/T838GrwLsn4e.png

    • This reply was modified 1 year, 12 months ago by emanuelhud.
    • This reply was modified 1 year, 12 months ago by emanuelhud.
    • This reply was modified 1 year, 12 months ago by emanuelhud.
    Thread Starter emanuelhud

    (@emanuelhud)

    https://pasteboard.co/T838GrwLsn4e.png

    • This reply was modified 1 year, 12 months ago by emanuelhud.
    • This reply was modified 1 year, 12 months ago by emanuelhud.
    Thread Starter emanuelhud

    (@emanuelhud)

    https://pasteboard.co/T838GrwLsn4e.png

    • This reply was modified 1 year, 12 months ago by emanuelhud.
    Roxy

    (@roxannestoltz)

    Hi @emanuelhud ,

    Glad to hear you have identifed the culprit!

    Regarding the quantity button, this seems to display, by default, with the Hello Elementor theme and no plugin should be required.

    This being said, since this forum is dedicated to support related to the WooCommerce core plugin, you’ll need to reach out to Elementor support for further help with this specific issue.

    Here is a direct link to their dedicated forum.

    Hope this points you in the right direction!

    Thread Starter emanuelhud

    (@emanuelhud)

    Ok, thank you very much!

    Hi @emanuelhud

    You are most welcome and we’re glad that worked! ??

    Meanwhile, if you have a moment to spare, we would love it if you could share your thoughts with us by leaving a review or feedback. Your experience and feedback are important to help us improve and ensure we’re always providing the best possible support.

    Thanks!

    Thread Starter emanuelhud

    (@emanuelhud)

    After adding the code below, I and trying?to use css on Buy Now Button for design, can’t?make sense?of it. Can you show me which class is it?

    I have tried?a few?classes?but they didn’t?do the job:

    button#sbw_wc-adding-button.single_add_to_cart_button.buy_now_button.button.alt

    I am using Hello Theme and elementor page builder.

    The website page: https://jor-d.com/product/white-tabbit-13/

    PHP?code:

    /* Dynamic Button for Simple & Variable Product */

    /**
    ?* Main Functions
    */<gwmw style=”display:none;”></gwmw>

    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 */

    <gwmw style=”display:none;”></gwmw>

    Hi @emanuelhud

    After adding the code below, I and trying to use css on Buy Now Button for design, can’t make sense of it. Can you show me which class is it?

    I have tried a few classes but they didn’t do the job:

    button#sbw_wc-adding-button.single_add_to_cart_button.buy_now_button.button.alt

    Have you tried using #sbw_wc-adding-button only?

    Meanwhile, this is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘The buy now button not redirecting’ is closed to new replies.