• Resolved fabson

    (@fabson)


    After installing redirect my script for a second “buy now” button doesn’t work anymore

    Can anybody help?

    / Redirect to checkout upon add to cart
    add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );
    
    function bbloomer_redirect_checkout_add_cart() {
       return wc_get_checkout_url();
    }
    
    // Change "Add to cart" text to "Jetzt Buchen"
    add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_button_text' );
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_single_add_to_cart_button_text' );
    
    function custom_add_to_cart_button_text( $text ) {
        return __( 'Jetzt Buchen', 'woocommerce' );
    }
    
    function custom_single_add_to_cart_button_text( $text ) {
        return __( 'Jetzt Buchen', 'woocommerce' );
    }
    
    // Display second "? Person hinzufügen" button
    add_action( 'woocommerce_after_add_to_cart_button', 'custom_display_second_add_to_cart_button' );
    
    function custom_display_second_add_to_cart_button() {
        global $product;
        ?>
        <form class="cart" action="" method="post" enctype="multipart/form-data" id="second-add-to-cart-form">
            <button type="button" class="button alt" id="second-add-to-cart-button"><?php echo esc_html( __( '+ Person hinzufügen', 'woocommerce' ) ); ?></button>
            <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>">
        </form>
        <script>
            (function($) {
                $(document).on('click', '#second-add-to-cart-button', function(event) {
                    event.preventDefault(); // Prevent the default button click behavior
                    var button = $(this);
                    var form = button.closest('form');
                    var formData = new FormData(form[0]);
                    $.ajax({
                        type: form.attr('method'),
                        url: '<?php echo esc_url( wc_get_cart_url() ); ?>',
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function(response) {
                            // Reload the current page with anchor link #buchen
                            var url = window.location.href.split('#')[0]; // Get the current URL without the anchor
                            window.location.href = url + '#buchen'; // Add the anchor to the URL and reload
                        },
                        error: function(error) {
                            console.log('An error occurred');
                        }
                    }).done(function() {
                        location.reload();
                    });
                });
            })(jQuery);
        </script>
        <?php
    }

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author John Godley

    (@johnny5)

    How does this relate to Redirection? Have you created any redirects?

    Thread Starter fabson

    (@fabson)

    indeed, I did a redirect from the cart page to the frontpage, because I didn’t use the cart page

    now I deleted the cart page and wrote a script for redirecting 404s to the frontpage and I tested it with other redirects and the plugin and script worked, there was just a conflict because I redirected this specific page

    so my problem is solved, great plugin!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘woocommerce script’ is closed to new replies.