2
votes

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

3
So what have you tried in order to accomplish that?Manuel Mannhardt
Honestly I haven't came up with any of working solutions. In theory it would work, if there was a button on the product page that adds the product to cart and confirms checkout at the same time...Kaspar
If someone could help me with some coding. If there was function that would do so on the checkout page: Check if cart need payment - if no (it means cart is free), then fire action that is fired when customer presses place order button. If yes , (cart need payment), then do nothing.Kaspar

3 Answers

3
votes

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' );
2
votes

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/

-1
votes

There are zillions of WooCommerce plugins available allows you to just do that. One such (free) is this one: https://wordpress.org/plugins/download-now-for-woocommerce/

You can also build a membership site where you will have more control for individual users or products.