Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active April 16, 2023 18:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bekarice/aebd817dfe60c22c95f5 to your computer and use it in GitHub Desktop.
Save bekarice/aebd817dfe60c22c95f5 to your computer and use it in GitHub Desktop.
Add WooCommerce product meta to the shop loop
/**
* Will add information to the shop loop above the "Add to cart" button
* This example will use post meta (custom fields) and display them if set
* Tutorial: http://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
**/
function skyverge_shop_display_post_meta() {
global $product;
// replace the custom field name with your own
$location = get_post_meta( $product->id, 'location', true );
$points = get_post_meta( $product->id, 'points', true );
// (optional) cleans up custom field names: replace underscores with spaces
$location = str_replace( '_', ' ', $location );
// Add these fields to the shop loop if set
if ( ! empty( $location ) ) {
echo '<div class="product-meta"><span class="product-meta-title">Location:</span> ' . ucwords( $location ) . '</div>';
}
if ( ! empty( $points ) ) {
echo '<div class="product-meta"><span class="product-meta-title">SkyPoints:</span> ' . $points . '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'skyverge_shop_display_post_meta', 9 );
@cosXsinX
Copy link

more versatile implementation =>
function get_product_meta_value($meta_identifier) {
global $product;
$meta_value = get_post_meta( $product->id, $meta_identifier, true );
return $meta_value;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment