1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** * WooCommerce: Show only one custom product attribute above Add-to-cart button on single product page. */ function isa_woo_get_one_pa(){ // Edit below with the title of the attribute you wish to display $desired_att = 'Some Attribute Title'; global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $out = ''; foreach ( $attributes as $attribute ) { if ( $attribute['is_taxonomy'] ) { // sanitize the desired attribute into a taxonomy slug $tax_slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '_', $desired_att))); // if this is desired att, get value and label if ( $attribute['name'] == 'pa_' . $tax_slug ) { $terms = wp_get_post_terms( $product->id, $attribute['name'], 'all' ); // get the taxonomy $tax = $terms[0]->taxonomy; // get the tax object $tax_object = get_taxonomy($tax); // get tax label if ( isset ($tax_object->labels->name) ) { $tax_label = $tax_object->labels->name; } elseif ( isset( $tax_object->label ) ) { $tax_label = $tax_object->label; } foreach ( $terms as $term ) { $out .= $tax_label . ': '; $out .= $term->name . '<br />'; } } // our desired att } else { // for atts which are NOT registered as taxonomies // if this is desired att, get value and label if ( $attribute['name'] == $desired_att ) { $out .= $attribute['name'] . ': '; $out .= $attribute['value']; } } } echo $out; } add_action('woocommerce_single_product_summary', 'isa_woo_get_one_pa'); |