Skip to content

Instantly share code, notes, and snippets.

@woogist
Last active March 21, 2024 13:36
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 woogist/4da28f6de6182f2e2f080164a693b461 to your computer and use it in GitHub Desktop.
Save woogist/4da28f6de6182f2e2f080164a693b461 to your computer and use it in GitHub Desktop.
Display a product custom field within single product pages after the short description
<?php
// Display a product custom field within single product pages after the short description
function woocommerce_custom_field_example() {
if ( ! is_product() ) {
return;
}
global $product;
if ( ! is_object( $product ) ) {
$product = wc_get_product( get_the_ID() );
}
$custom_field_value = get_post_meta( $product->get_id(), 'woo_custom_field', true );
if ( ! empty( $custom_field_value ) ) {
echo '<div class="custom-field">' . esc_html( $custom_field_value ) . '</div>';
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_custom_field_example', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment