如何在WooCommerce单品页价格下方显示指定产品属性及相关方法
Hey there! Let's break down your two WooCommerce questions with practical, actionable code—this stuff comes up all the time when tweaking product pages, so I’ve got you covered.
WooCommerce handles two types of product attributes: global (pre-registered via Products > Attributes) and custom (added directly to a product). Either way, checking for a value is straightforward using the product object’s built-in methods.
Here’s a quick snippet to check if an attribute has a non-empty value:
// Grab the current product object (works on single product pages) global $product; // Replace with your target attribute's slug (lowercase, hyphens instead of spaces) $target_attr_slug = 'material'; // Check if the attribute exists and has a value if ( $product->get_attribute( $target_attr_slug ) ) { // Do something if the attribute has a value echo 'This attribute has a valid value!'; } else { // Handle cases where the attribute is empty or doesn't exist echo 'No valid value found for this attribute.'; }
Note: For global attributes, the slug is the one you set when registering the attribute (e.g., if your attribute is "Material", the slug is likely material). For custom attributes, the slug is the attribute name converted to lowercase with hyphens (e.g., "Product Origin" becomes product-origin).
To show the attribute’s name and value below the price area, we’ll hook into a WooCommerce action that targets the product summary section. We’ll also make sure to handle both global and custom attributes correctly.
Add this code to your theme’s functions.php file (or a custom plugin—never edit core WooCommerce files!):
// Hook into the single product summary, right after the "Add to Cart" button (priority 25) add_action( 'woocommerce_single_product_summary', 'display_target_product_attribute', 25 ); function display_target_product_attribute() { global $product; // Replace with your attribute's slug $target_attr_slug = 'material'; // Get the attribute's value $attr_value = $product->get_attribute( $target_attr_slug ); // Exit early if there's no value if ( empty( $attr_value ) ) return; // Get the attribute's friendly name $attr_name = ''; // First, check if it's a global attribute (uses pa_ prefix for taxonomy) $global_attr_taxonomy = 'pa_' . $target_attr_slug; if ( taxonomy_exists( $global_attr_taxonomy ) ) { $attr_name = wc_attribute_label( $global_attr_taxonomy ); } // If it's a custom attribute, loop through the product's attributes to find the label else { $product_attributes = $product->get_attributes(); foreach ( $product_attributes as $attribute ) { if ( $attribute->get_name() === $target_attr_slug ) { $attr_name = $attribute->get_label(); break; } } } // Fallback to slug if name isn't found if ( empty( $attr_name ) ) { $attr_name = ucwords( str_replace( '-', ' ', $target_attr_slug ) ); } // Display the attribute on the page echo '<div class="custom-attribute-section" style="margin: 15px 0;">'; echo '<strong>' . esc_html( $attr_name ) . ':</strong> ' . esc_html( $attr_value ); echo '</div>'; }
What this does:
- Uses the
woocommerce_single_product_summaryhook with priority 25 (runs after the "Add to Cart" button, which uses priority 20—perfect for placing it below the price/add-to-cart area). - Safely escapes output with
esc_html()to prevent XSS vulnerabilities. - Handles both global and custom attributes, so it works no matter how you set up your attribute.
- Includes a fallback name if the label can’t be retrieved (converts the slug to readable text).
You can tweak the HTML and CSS (the inline style is just a starting point) to match your theme’s design.
内容的提问来源于stack exchange,提问作者Tommy

