-
-
Save contemplate/2adc7be2c72d585a07ac6f90b1f1e1b4 to your computer and use it in GitHub Desktop.
/*-------------------------------------- | |
Woocommerce - Allow Guest Checkout on Certain products | |
----------------------------------------*/ | |
// Display Guest Checkout Field | |
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); | |
function woo_add_custom_general_fields() { | |
global $woocommerce, $post; | |
echo '<div class="options_group">'; | |
// Checkbox | |
woocommerce_wp_checkbox( | |
array( | |
'id' => '_allow_guest_checkout', | |
'label' => __('Checkout', 'woocommerce' ), | |
'description' => __('Allow Guest Checkout', 'woocommerce' ) | |
) | |
); | |
echo '</div>'; | |
} | |
// Save Guest Checkout Field | |
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); | |
function woo_add_custom_general_fields_save( $post_id ){ | |
$woocommerce_checkbox = isset( $_POST['_allow_guest_checkout'] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, '_allow_guest_checkout', $woocommerce_checkbox ); | |
} | |
// Enable Guest Checkout on Certain products | |
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'enable_guest_checkout_based_on_product' ); | |
function enable_guest_checkout_based_on_product( $value ) { | |
if ( is_checkout() && WC()->cart ) { | |
$cart = WC()->cart->get_cart(); | |
foreach ( $cart as $item ) { | |
if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) == 'yes' ) { | |
$value = "yes"; | |
} else { | |
$value = "no"; | |
break; | |
} | |
} | |
} | |
return $value; | |
} |
Hello,
love this code, works great. Can it be modified to support WooCommerce variable products? I'm not clear about how/if the line 'wrapper_class' => 'show_if_simple' can be modified.
Thanks
Is there any chance this could be updated to add the "allow guest checkout" checkbox to show up on variable products, too?
As luck would have it, all of my simple products are the ones that should not allow guest checkout and all of my variable products (mostly merch) are the ones that should allow it.
TIA!
Amazing! Works as expected. I can now sell courses to users with an account (or creating one) and other products to guests. Thanks so much for this.
If you want the guest checkout button to appear on all types of products rather than just simple, just remove this line:
'wrapper_class' => 'show_if_simple',
Hi. Everything works as expected, but on my checkout page for products with guest registration allowed still appear fields for registration: username, email, password, and also checkbox with question: "register?"
How can i remove this fields for certain products, with guest registration allowed?
I've removed the
'wrapper_class' => 'show_if_simple',
line so that this will work on all product types now.
Guest Registration enabled does not remove the "Create an Account" option on the checkout page.
Since some have asked I've created the opposite scenario here to disable guest checkout on certain products when it is globally enabled:
https://gist.github.com/contemplate/5c1909bd1bfcd485cc1bdaf8786f3a80
This helped me a lot!
Here are the instructions on how to install this plugin using only the wordpress dashboard:
Just add the php tags (<?php {plugin code} ?>
) and the metainformation comment. The comment is necessary for wordpress to recognize the plugin.
Here is an example:
<?php
/**
* Plugin Name: Your Plugin Name (mandatory)
* Plugin URI: Your Plugin URI
* Description: A brief description of what your plugin does.
* Version: 1.0 (mandatory)
* Author: Your Name (mandatory)
* Author URI: Your Website (mandatory)
* License: GPL2
*/
...
?>
Save this as a .php
file, store it in a folder and zip the folder. Then select this zipped folder, when you upload your plugin in the wordpress dashboard.
Adjusted script to only run on checkout by adding is_checkout() &&
to the condition. We'll see if this still produces desired results.
Thank you!