Last active
March 21, 2024 13:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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