• Resolved admintest123

    (@admintest123)


    Hi,

    I need to add custom checkout button near ‘Add to Cart’ button in single product page.
    i used the following code and button is showing in product page. But the issue is when we selects the variations and clicks custom checkout button, shows the error ‘Please choose product options…’ in the checkout page. i think variations are not set.
    Please help with this js

    
    function add_content_after_addtocart() {
         global $woocommerce;
        // 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 );
    
        // get the 'Checkout Page' URL
        $checkout_url = WC()->cart->get_checkout_url();
    
        // run only on simple products 
        if( $product->is_type( 'variable' ) ){
    
            ?>
            <script>
        jQuery(function($) {
        <?php /* if our custom button is clicked, append the string '&quantity=', and also the quantitiy number to the URL */ ?>
    
            // if our custom button is clicked
            $('.custom-checkout-btn').on('click', function() {
    
                // get the value of the 'href' attribute 
                $(this).attr('href', function() {
                    // return the 'href' value + the string '&quantity=' + the current selected quantity number
                    return this.href + '&quantity=' + $('input.qty').val() + gh;
                });
    
            });
    
    $( '.variations_form' ).on( 'woocommerce_variation_select_change', function () {
    
      alert( 'Options changed' );
    
    } );
    
        });
        </script>
            <?php
    
            echo '<div class='col-sm-6'><div class='buy_now'><a>Buy Now</a></div></div><div class='clearfix'></div>';
    
    ?>
    
    <?php
        }
        else if( $product->is_type( 'simple' ) ){
    
           echo '</div><div class='col-sm-6'><div class='p-t-35'></div><div class='buy_now'><a>Buy Now</a></div></div><div class='clearfix'></div>';
        }
    
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
    
    • This topic was modified 6 years, 5 months ago by Jose Castaneda. Reason: added backticks for code
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Need to add Custom checkout button on single product page’ is closed to new replies.