Answer the question
In order to leave comments, you need to log in
Display attributes in Woocommerce for unregistered users?
Good afternoon!
Faced such a problem
. I need to display the attributes on woocommerce, so that they would be seen by non-registered users.
I paste in function.php the following code:
add_action('init', 'my_theme_hide_price_not_authorized');
function my_theme_hide_price_not_authorized() {
if ( !is_user_logged_in() ) {
add_filter( 'woocommerce_attribute', 'my_theme_hide_attribute');
}
}
function my_theme_hide_attribute() {
$subheadingvalues = get_the_terms( $product->id, 'pa_color');
foreach ( $subheadingvalues as $subheadingvalue ) {
echo $subheadingvalue->name; }
}
Answer the question
In order to leave comments, you need to log in
yes it is a function
add_action('init', 'my_theme_hide_price_not_authorized');
function my_theme_hide_price_not_authorized() {
if ( !is_user_logged_in() ) {
add_filter( 'woocommerce_attribute', 'my_theme_hide_attribute');
}
}
<table class="shop_attributes">
<?php if ( $product->enable_dimensions_display() ) : ?>
<?php if ( $product->has_weight() ) : $has_row = true; ?>
<tr class="<?php if ( ( $alt = $alt * -1 ) === 1 ) echo 'alt'; ?>">
<th><?php _e( 'Weight', 'woocommerce' ) ?></th>
<td class="product_weight"><?php echo wc_format_localized_decimal( $product->get_weight() ) . ' ' . esc_attr( get_option( 'woocommerce_weight_unit' ) ); ?></td>
</tr>
<?php endif; ?>
<?php if ( $product->has_dimensions() ) : $has_row = true; ?>
<tr class="<?php if ( ( $alt = $alt * -1 ) === 1 ) echo 'alt'; ?>">
<th><?php _e( 'Dimensions', 'woocommerce' ) ?></th>
<td class="product_dimensions"><?php echo $product->get_dimensions(); ?></td>
</tr>
<?php endif; ?>
<?php endif; ?>
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) ) {
continue;
} else {
$has_row = true;
}
?>
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<th><?php echo wc_attribute_label( $attribute['name'] ); ?></th>
<td><?php
if ( $attribute['is_taxonomy'] ) {
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
</tr>
<?php endforeach; ?>
</table>
You are
requesting only color attributes in this line, so you get Blue everywhere. Paste the code into the question in a human way so that everything can be read, along with the
UPD markup. Better like this.. just remove the tab with the characteristics for logged in users
This code is in functions.php
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
if ( is_user_logged_in() ) {
unset( $tabs['additional_information'] );
}
return $tabs;
}
<?php $names = array ('один атрибут', 'второй атрибут') //имена атрибутов, которые не будем показывать
foreach ( $attributes as $attribute ) :
if ( !is_user_logged_in() && in_array($attribute['name'], $names ) {
continue; //пропускаем атрибут
}
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) ) {
continue;
} else {
$has_row = true;
}
?>
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<th><?php echo wc_attribute_label( $attribute['name'] ); ?></th>
//далее остальной файл
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question