A
A
Andrey2016-11-18 11:46:06
PHP
Andrey, 2016-11-18 11:46:06

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; }
}

But it does not work correctly, the function outputs one attribute and puts what is in all cells of other attributes.
Here's how it happens ->
5a22e2a376e44699ac3a797dac8ca11f.png
I don't understand what to do, because I'm still new to php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2016-11-18
@Andrey_Tech

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');
  }
}

it works correctly, it hides attributes, only all at once
. That is, non-registered users should see only some attributes, all the rest should be hidden for them
. Here is the product_attributes.ph code, by displaying attributes:
<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>

I don't know how to override it, I just haven't done it before

P
Pavel, 2016-11-18
@Palych_tw

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;

}

UPD 2 Copy product_attributes.php to your theme
And insert your condition into the attribute output loop:
<?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 question

Ask a Question

731 491 924 answers to any question