Answer the question
In order to leave comments, you need to log in
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:
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question