user11111035
user11111035

Reputation:

Add to cart and redirect to checkout for variable products in WooCommerce

I got help in this answer thread that allow to add an additional add-to-cart button that redirects to checkout. It works fine for simple products.

But how to make it work for variable products as well?

I've been trying myself, but no matter what I do, I break the site. I simply do not understand how to make this work with/ for variable products.

Here's the lightly changed code that works for simple products and which takes the quantity field into consideration:

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {
    global $product;

    $addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
    $button_class  = 'single_add_to_cart_button button alt custom-checkout-btn';
    $button_text   = __("Buy & Checkout", "woocommerce");

    if( $product->is_type( 'simple' )) :
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';

        // On input/change quantity event
        $(qty).on('input change', function() {
            $(button).attr('href', url + '&quantity=' + $(this).val() );
        });
    });
    </script>
    <?php
    echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
    endif;
}

Does anyone know how to get this working for variable products too?

Upvotes: 3

Views: 4531

Answers (2)

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7

  //Add content after addtocart
add_action( 'woocommerce_after_add_to_cart_button', 'amit_add_content_after_addtocart' );
function amit_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 );
    $quantity = absint( $_REQUEST['quantity'] );
    // get the "Checkout Page" URL
    $checkout_url = WC()->cart->get_checkout_url();

    // run only on simple products
    if( $product->is_type( 'simple' ) ) {
        echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" data-id ="'.$current_product_id.'" class="amit_single_add_to_cart_button quick-purchase-btn button">'.__('buy now','woocommerce').'</a>';
    }elseif( $product->is_type( 'variable' ) ) {
        echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" data-id ="'.$current_product_id.'" class="amit_single_add_to_cart_button quick-vari-purchase-btn button">'.__('buy now','woocommerce').'</a>';
    }
}


add_action('wp_footer', 'script');
function script()
{
    ?>
    <script>
        jQuery( "body" ).on( "click", ".quick-purchase-btn", function(e) {
            e.preventDefault();
            var site_url = window.location.origin;
            var product_id =jQuery(this).attr('data-id');
            var quantity = jQuery(this).closest('form').find('.quantity input[name="quantity"]').val();
            console.log(quantity);
            window.location= site_url + "/checkout?add-to-cart="+ product_id + "&quantity="+ quantity;
            // console.log(site_url + "/checkout?add-to-cart="+ product_id + "&quantity="+ quantity);
        });
        jQuery( "body" ).on( "click", ".quick-vari-purchase-btn", function(e) {
            e.preventDefault();
            var site_url = window.location.origin;
            var variation_id =jQuery(this).closest('form').find('input[name="variation_id"]').val();
            if(variation_id > 0 ) {
                var quantity = jQuery(this).closest('form').find('.quantity input[name="quantity"]').val();
                window.location= site_url + "/checkout?add-to-cart="+ variation_id + "&quantity="+ quantity;
            }
        });
    </script>
<?php }

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254383

Update 3

The following code will handle simple and variable products adding an additional Add to cart button that redirects to cart (with synchronized quantity).

The code works for simple and variable products as well.

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {
    global $product;

    $addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
    $button_class  = 'single_add_to_cart_button button alt custom-checkout-btn';
    $button_text   = __("Buy &amp; Checkout", "woocommerce");

    if( $product->is_type( 'simple' )) :
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';

        // On input/change quantity event
        $(qty).on('input change', function() {
            $(button).attr('href', url + '&quantity=' + $(this).val() );
        });
    });
    </script>
    <?php

    elseif( $product->is_type( 'variable' ) ) : 

    $addtocart_url = wc_get_checkout_url().'?add-to-cart=';
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            vid    = 'input[name="variation_id"]',
            pid    = 'input[name="product_id"]',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';

        // Once DOM is loaded
        setTimeout( function(){
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        }, 300 );

        // On input/change quantity event
        $(qty).on('input change', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(this).val() );
            }
        });

        // On select attribute field change event
        $('.variations_form').on('change blur', 'table.variations select', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        });
    });
    </script>
    <?php
    endif;
    echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 3

Related Questions