Woocommerce skip checkout for free products
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
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
Oct 11 '16 at 19:26
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
Oct 12 '16 at 5:28
2 Answers
2
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).
is_ajax()
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() 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' );
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/
Thanks! During research for solution I have read this article too, but I (actually my customer) would like to skip the second button click on the checkout...
– Kaspar
Oct 11 '16 at 19:27
Do you see these 2 links: 1. wordpress.org/plugins/woocommerce-direct-checkout 2. chrislema.com/woocommerce-no-products-page
– Giliapps
Oct 11 '16 at 21:10
The first one skips cart, which I already use - it doesn't help me to skip the checkout.
– Kaspar
Oct 12 '16 at 5:39
The second one teaches how to use woocommerce without product page - you still need to use checkout. In my case - I have some custom fields on product page, which customer needs to fill in, so I can't skip the product page.
– Kaspar
Oct 12 '16 at 5:41
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So what have you tried in order to accomplish that?
– Manuel Mannhardt
Oct 10 '16 at 14:50