A
A
Andrey Plyuta2020-11-30 18:58:51
WordPress
Andrey Plyuta, 2020-11-30 18:58:51

How to make a loop to get the characteristics of a woocomerce product?

There is a plugin for creating xml for unloading goods to the outlet, I decided to modify it a bit. But ran into a problem.
The plugin generates duplicate Feature Names when compiling a feed.
5fc515e289cad263788728.png

And it is necessary that it would be like this for fast attributes.
5fc515f659943995086890.png

Here is the code that is responsible for getting the Characteristic and its parameters:

foreach ( $general_atts as $general_att ) {
            $general_att_array = $general_att->get_data();
            //var_dump($general_att_array);
            if ( true === $general_att_array['variation'] ) {
              continue;
            }
            if ( 1 === $general_att_array['is_taxonomy'] ) {
            $tax_att = get_taxonomy( $general_att_array['name'] );
              if ( ! empty( $tax_att ) ) {
                foreach ( $general_att_array['options'] as $general_att_value ) {
                  $products .= "\n\t\t\t<param name=\"". $tax_att->labels->singular_name ."\">";
                  $att_term = get_term($general_att_value);
                  $products .="\n\t\t\t" . $att_term->name . "</param>";
                }
              }
            } else {
              $products .= "\n\t\t\t<param name=\"". $general_att_array['name'] ."\"><![CDATA[" . str_replace( ' | ', '<br> ', $general_att_array['value'] ) . "]]></param>";
            }
          }
          
        }


Help to make sure that the names of the characteristics are not duplicated, and all parameters are written separated by commas, for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2020-12-01
@motor4ikkk

this piece

foreach ( $general_att_array['options'] as $general_att_value ) {
                  $products .= "\n\t\t\t<param name=\"". $tax_att->labels->singular_name ."\">";
                  $att_term = get_term($general_att_value);
                  $products .="\n\t\t\t" . $att_term->name . "</param>";
                }

change to
$attr = [];
  foreach ( $general_att_array[ 'options' ] as $general_att_value ) {
    $att_term = get_term( $general_att_value );
    $attr[]   = $att_term->name;
  }
  $products .= "\n\t\t\t" . sprintf('<param name="%s">%s</param>',$tax_att->labels->singular_name,implode( ',', $attr ));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question