Skip to content

Instantly share code, notes, and snippets.

@passatgt
Created May 28, 2020 11:38
Show Gist options
  • Save passatgt/0777d004bf817cdd3a86007258993175 to your computer and use it in GitHub Desktop.
Save passatgt/0777d004bf817cdd3a86007258993175 to your computer and use it in GitHub Desktop.
Use WooCommerce with only a checkout page
<?php
/*
I built custom product pages with a simple add to cart button(using the add_to_cart shortcode) for a webshop.
There was no need for category pages, shop pages, cart or anything like that, just a simple product page with an add to cart button that redirects to checkout directly.
Here is how you can do this:
1. Delete the My Account page
2. Delete the Shop page
*/
//Redirect cart to checkout or homepage(if empty)
add_action( 'template_redirect', function(){
if ( is_cart() ) {
if(WC()->cart->cart_contents_count > 0) {
wp_redirect( wc_get_checkout_url(), '301' );
} else {
wp_redirect( home_url() );
}
exit;
}
});
//Disable single product archives and category archives
add_filter('woocommerce_register_post_type_product', 'disable_wc_tax_publicly_queryable');
add_filter('woocommerce_taxonomy_args_product_cat', 'disable_wc_tax_publicly_queryable');
function disable_wc_tax_publicly_queryable($args) {
$args['publicly_queryable'] = false;
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment