'Woocommerce skip checkout for free products

I'm building webpage on WP and Woocommerce - I would like to skip cart and also checkout page for free products (or products which ID-s I can specify). These products are free and virtual (no payment needed, no shipping needed). The webpage is only used by registered users - so all the customer info is present.

The result I would like to have is that if you press ORDER button on product page - the order is done and customer is redirected to Thank-You page.

BR, Kaspar



Solution 1:[1]

I applied the same concept, but found a major issue when processing order on the checkout; fields were still required.

The primary issue was processing the order via AJAX ( was using is_ajax() ), and even though it was on the checkout page, it wasn't returning as true. It's possible there was a recent change, or it could be the site's environment (theme).

Here are some of the conditional tags: https://docs.woocommerce.com/document/conditional-tags/

Seeing how things change, the answer can be edited here, but the original concept is located at: https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/

function wc_free_checkout_fields() {
    // Bail we're not at checkout, or if we're at checkout OR AJAX (payment process) but payment is needed.
    if ( function_exists( 'is_checkout' ) && ( ! ( is_checkout() || is_ajax() ) || ( ( is_checkout() || is_ajax() ) && WC()->cart->needs_payment() ) ) ) {
        return;
    }

    // Remove coupon forms since it's irrelevant with a free cart?
    remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

    // Remove the "Additional Info" order notes.
    add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

    // Unset the fields we don't want in a free checkout.
    function wc_unset_unwanted_checkout_fields( $fields ) {
        // Add or remove billing fields you do not want.
        // @link http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
        $billing_keys = array(
            'billing_company',
            'billing_phone',
            'billing_address_1',
            'billing_address_2',
            'billing_city',
            'billing_postcode',
            'billing_country',
            'billing_state',
        );

        // For each unwanted billing key, unset.
        foreach( $billing_keys as $key ) {
            unset( $fields['billing'][$key] );
        }

        return $fields;
    }
    add_filter( 'woocommerce_checkout_fields', 'wc_unset_unwanted_checkout_fields' );

    // A tiny CSS tweak for the account fields; this is optional.
    function wc_print_custom_css() {
        ?>
        <style>
            .create-account {
                margin-top: 6em;
            }
        </style>
        <?php
    }
    add_action( 'wp_head', 'wc_print_custom_css' );
}
add_action( 'wp', 'wc_free_checkout_fields' );

Solution 2:[2]

Check if the checkout has no cost with the WC()->cart->needs_payment() check. see this for more info: https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 iazaran