Last active
April 16, 2023 18:06
-
-
Save bekarice/aebd817dfe60c22c95f5 to your computer and use it in GitHub Desktop.
Add WooCommerce product meta to the shop loop
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
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
}