Skip to content

Instantly share code, notes, and snippets.

@MrVibe
Created January 8, 2021 16:15
Show Gist options
  • Save MrVibe/400924205156fe32245713092f84dc08 to your computer and use it in GitHub Desktop.
Save MrVibe/400924205156fe32245713092f84dc08 to your computer and use it in GitHub Desktop.
Direct checkout in WooCommerce for non-WPLMS theme.
add_action( 'template_redirect', 'vibe_product_woocommerce_direct_checkout');
function vibe_product_woocommerce_direct_checkout(){
if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) || (function_exists('vibe_check_plugin_installed') && vibe_check_plugin_installed( 'woocommerce/woocommerce.php')) || function_exists('WC')){
if( is_single() && get_post_type() == 'product' && isset($_GET['redirect'])){
global $woocommerce;
$found = false;
$product_id = get_the_ID();
$courses = vibe_sanitize(get_post_meta(get_the_ID(),'vibe_courses',false));
if(isset($courses) && is_array($courses) && count($courses)){
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
$cart_url = esc_url( wc_get_cart_url() );
wp_redirect( $cart_url);
}else{
WC()->cart->add_to_cart( $product_id );
$cart_url = esc_url( wc_get_cart_url() );
wp_redirect( $cart_url);
}
exit();
}
}
} // End if WooCommerce Installed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment