O
O
Oviplokos2020-04-13 18:29:26
PHP
Oviplokos, 2020-04-13 18:29:26

How to replace return to make the code work?

Help me to understand. I add the ability to display attributes in the product catalog, but I can not understand what my mistake is.

The code itself:

spoiler
public function the_attributes( $product = null, $element, $show_weight = null, $show_dimensions = null, $skip_atts = null, $single_product = null ) {

    $out = '';
    $out_middle = '';

    if ( isset( $product ) ) {
      if ( is_object( $product ) ) {

        if ( $show_weight ) {
          if ( $product->has_weight() ) {
            $weight = $product->get_weight();
            $unit = esc_attr( get_option( 'woocommerce_weight_unit' ) );
          }
        }
$out_middle .= '	<table><tbody>';
        if ( $show_dimensions ) {
          if ( $product->has_dimensions() ) {
            $dimensions = wc_format_dimensions( $product->get_dimensions( false ) );
          }
        }

       		$colon = get_option( 'wcsa_remove_semicolon' ) == 'yes' ? ' ' : ': ';
        $hide_labels = get_option( 'woocommerce_show_attributes_hide_labels' );
        // check if they choose span element over li
        if ( get_option( 'woocommerce_show_attributes_span' ) == 'yes' ) {
          $element = 'span';
        }

       		if ( ! $skip_atts ) {

          $attributes = $this->get_attributes( $product, $single_product );

          if ( ! $attributes ) {
            return;
          }

          foreach ( $attributes as $attribute ) {

            $class_string = sanitize_title( strip_tags( $attribute['value'] ) );

            $out_middle .= '<' . esc_attr( $element ) . ' class="' . esc_attr( sanitize_title( $attribute['label'] ) ) . ' ' . esc_attr( $class_string ) . '">';


            // Hide labels if they want to
            if ( 'yes' != $hide_labels ) {
                
              // PolyLang translation support
              if ( function_exists( 'pll__' ) ) {
                $translation = pll__( $attribute['label'] );
                $attribute['label'] = ( ! empty( $translation ) ) ? $translation : $attribute['label'];
              }
              // WPML translation support
              if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
                $translation = apply_filters( 'wpml_translate_single_string', $attribute['label'], 'WordPress', 'taxonomy singular name: '. $attribute['label'] );
                  $attribute['label'] = ( ! empty( $translation ) ) ? $translation : $attribute['label'];
                  
              }	
            }
     
{
 
$out_middle .= '<tr><td><span class="attribute-label"> ' .
sprintf( __( '%s', '<b>woocommerce-show-attributes</b>' ), esc_html( $attribute['label'] ) )
. '</td></span>' . $colon  ;
                  
            $out_middle .= '<td><span class="attribute-value">';
            $out_middle .= $attribute['value'];

            $out_middle .= '</span></td></tr></' . esc_attr( $element ) . '>';
            
} 
  

          } // ends foreach attribute 
        
        }

        // Add weight and dimensions if they opted in

        if ( ! empty( $weight ) ) {
          $unit = empty( $unit ) ? '' : $unit;
          // weight
          $out_middle .= '<' . esc_attr( $element ) . ' class="show-attributes-weight">';
          // Hide labels if they want to
          if ( $hide_labels != 'yes' ) {
            $out_middle .= '<span class="attribute-label">' . __( 'Weight', 'woocommerce-show-attributes' ) . $colon . ' </span> ';
          }
          $out_middle .= '<span class="attribute-value">' . esc_html( $weight ) . ' ' . esc_html( $unit ) . ' </span></' . esc_attr( $element ) . '>';
          if ( 'span' == $element ) {
            $out_middle .= '<br />';
          }
        }

        if ( ! empty( $dimensions ) ) {
          // dimensions
          $out_middle .= '<' . esc_attr( $element ) . ' class="show-attributes-dimensions">';
          // Hide labels if they want to
          if ( $hide_labels != 'yes' ) {
            $out_middle .= '<span class="attribute-label">' . __( 'Dimensions', 'woocommerce-show-attributes' ) . $colon . ' </span> ';
          }
          $out_middle .= '<span class="attribute-value">' . esc_html( $dimensions ) . '</span></' . esc_attr( $element ) . '>';
          if ('span' == $element) {
            $out_middle .= '<br />';
          }
        }
$out_middle .= (' </tbody></table>');
        
if ( $out_middle ) {
          $out = ('li' == $element) ? '<ul ' : '<div ';
          $out .= 'class="attribut">' . $out_middle;
          $out .= ('li' == $element) ? '</ul>' : '</div>';
}
    

      }
    }
    return $out;
  }


I take a function from the plugin, add it to function.php woocommerce to change the display order, because The plugin displays only at the very end of the card. But the code does not work due to the fact that it ends with a return and it discards all subsequent codes. If return is replaced by echo, then the code itself does not work. What could be the way out?

I am attaching the visual. Link to the page is here .

spoiler
5e9484c21e2d2100209345.jpeg


spoiler
5e9484d88d07f281165447.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question