Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
functions.php
add_filter( 'body_class', 'add_preorder_class' );
add_filter( 'post_class', 'add_preorder_class' );
function add_preorder_class( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
if ( $product && 'yes' === get_post_meta( $product->get_id(), '_wc_pre_orders_enabled', true ) ) {
$classes[] = 'pre-order';
}
return $classes;
}
add_action( 'woocommerce_before_shop_loop_item_title', 'show_preorder_badge' );
add_action( 'woocommerce_before_single_product_summary', 'show_preorder_badge' );
function show_preorder_badge() {
global $post, $product;
?>
<?php if ( 'yes' === get_post_meta( $product->get_id(), '_wc_pre_orders_enabled', true ) ) : ?>
<?php echo '<span class="onsale pre-order">' . esc_html__( 'Pre-Order!', 'domain' ) . '</span>'; ?>
<?php endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.