Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created November 7, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/75349aa250af754261961ab1f8d85cae to your computer and use it in GitHub Desktop.
Save helgatheviking/75349aa250af754261961ab1f8d85cae to your computer and use it in GitHub Desktop.
Clicking checkout in the cart auto creates an order. Must have Cash On Delivery gateway enabled.
<?php
/*
* Plugin Name: Auto Checkout for WooCommerce
* Plugin URI: https://www.paypal.me/helgatheviking
* Description: Clicking checkout in the cart auto creates an order. Must have Cash On Delivery gateway enabled.
* Version: 1.0.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 5.3.0
* WC requires at least: 3.8.0
* Tested up to: 5.3.0
* WC tested up to: 3.8.0
*
* Copyright: © 2019 Kathy Darling.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
*/
/**
* Add a query arg to checkout url.
*/
function kia_auto_checkout( $url ) {
return add_query_arg(
array(
'auto-checkout' => 'true',
'woocommerce-process-checkout-nonce' => wp_create_nonce( 'woocommerce-process_checkout' )
),
$url
);
}
add_filter( 'woocommerce_get_checkout_url', 'kia_auto_checkout' );
/*
* Remove all checkout fields.
*/
function kia_remove_fields_for_auto_checkout() {
add_filter( 'woocommerce_checkout_fields', '__return_empty_array' );
add_filter( 'woocommerce_default_address_fields', '__return_empty_array' );
add_filter( 'woocommerce_billing_fields', '__return_empty_array' );
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false' );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
add_action( 'woocommerce_init', 'kia_remove_fields_for_auto_checkout' );
/**
* Process the checkout link.
*/
function kia_auto_checkout_action() {
if ( isset( $_GET['auto-checkout'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
wc_nocache_headers();
if ( WC()->cart->is_empty() ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
WC()->checkout()->process_checkout();
}
}
add_action( 'wp_loaded', 'kia_auto_checkout_action', 20 );
/**
* Manually defind some missing checkout form data.
* I think payment_method may be the only one you actually need.
*/
function kia_auto_set_checkout_data( $data ) {
return array(
'terms' => 1,
'createaccount' => 0,
'payment_method' => 'cod',
'shipping_method' => '',
'ship_to_different_address' => 0,
'woocommerce_checkout_update_totals' => 0,
);
}
add_filter( 'woocommerce_checkout_posted_data', 'kia_auto_set_checkout_data' );
/**
* Remove the auto-checkout so we don't get redirected back to an empty cart.
*/
function kia_remove_auto_checkout_query_args( $url ) {
return remove_query_arg(
array(
'auto-checkout',
'woocommerce-process-checkout-nonce'
),
$url
);
}
add_filter( 'woocommerce_get_return_url', 'kia_remove_auto_checkout_query_args' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment