Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@cosXsinX cosXsinX commented Nov 14, 2020

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