title: Woocommerce linking attributes and terms
body: <p>I am creating a search form for woocommerce, such that I can choose some of the options pulled from attributes, and then when I submit that I'll get a refined selection of products. I have created a selection for attributes, but I cannot get the terms from the specific attributes. How can I link those two?</p>
  
  <pre><code>global $woocommerce, $post, $product;
  
      $class = ' dnd_service_box_' . $class;
  
      $attribute_array = array();
      $attribute_taxonomies = wc_get_attribute_taxonomies();
  
      if ( $attribute_taxonomies ){
          foreach ( $attribute_taxonomies as $tax ){
              if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax-&gt;attribute_name ) ) ){
                  $attribute_array[ $tax-&gt;attribute_name ] = $tax-&gt;attribute_name;                                    
              }
          }
      }
  
  
      $options = array();
      $options[] = "&lt;option value=''&gt;--?--&lt;/option&gt;";
      foreach ( $attribute_taxonomies as $tax ) {
          $options[] = "&lt;option value='{$tax-&gt;attribute_name}'&gt;{$tax-&gt;attribute_label}&lt;/option&gt;";
      }   
  
      $return = '&lt;div class="dnd_sentence_search'.$class.'"&gt;
                      &lt;h3&gt;'._e('Attributes','theme').'&lt;/h3&gt;
                      &lt;select class="" id="attributes" size="1" name="attributes"&gt;
                          '.implode("\n", $options).'
                      &lt;/select&gt;
  
                  &lt;/div&gt;';
  
      return $return;
  </code></pre>
  
  <p>This will return the attributes. And when I do <code>print_r($attribute_taxonomies);</code> I get out this</p>
  
  <pre><code>Array
  (
  [0] =&gt; stdClass Object
      (
          [attribute_id] =&gt; 2
          [attribute_name] =&gt; product-year
          [attribute_label] =&gt; Year
          [attribute_type] =&gt; select
          [attribute_orderby] =&gt; menu_order
      ) ...
  </code></pre>
  
  <p>How can I access all the terms from these taxonomies and how to make it so that when I choose one (like Year), in the other selection I'll only see the terms from the Year attribute?</p>
  
  <p>EDIT: </p>
  
  <p>I added another code that will give me terms</p>
  
  <pre><code>foreach ($attribute_taxonomies as $tax) {
      if (taxonomy_exists(wc_attribute_taxonomy_name($tax-&gt;attribute_name))){
          $taxonomy_terms[$tax-&gt;attribute_name] = get_terms( wc_attribute_taxonomy_name($tax-&gt;attribute_name));
      }
  }
  </code></pre>
  
  <p>Now my <code>$attribute_taxonomies</code> produces this kind of array:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [attribute_id] =&gt; 2
              [attribute_name] =&gt; product-year
              [attribute_label] =&gt; Year
              [attribute_type] =&gt; select
              [attribute_orderby] =&gt; menu_order
          ) ...
  </code></pre>
  
  <p>From with I can get all the names and labels. The $taxonomy_terms will give mi this kind of array:</p>
  
  <pre><code>Array
  (
      [product-year] =&gt; Array
          (
              [0] =&gt; stdClass Object
                  (
                      [term_id] =&gt; 77
                      [name] =&gt; 1989
                      [slug] =&gt; 1989
                      [term_group] =&gt; 0
                      [term_taxonomy_id] =&gt; 82
                      [taxonomy] =&gt; pa_product-year
                      [description] =&gt; 
                      [parent] =&gt; 0
                      [count] =&gt; 6
                      [meta_id] =&gt; 22
                      [woocommerce_term_id] =&gt; 77
                      [meta_key] =&gt; order_pa_product-year
                      [meta_value] =&gt; 0
                  )
  
              [1] =&gt; stdClass Object
                  (
  </code></pre>
  
  <p>So for each <code>attribute-name</code> I'm getting corresponding terms. But these are different arrays. First one is a simple array, and I thing that the second is multidimensional array. </p>
  
  <p>Is there a way of checking if the attribute_name from the first one matches the key from the second one, and then I just need to get the values from the second array. This sounds very complicated...</p>
  
  <p>EDIT2: Ok I figured out how to display the terms, but I still would like to link it:</p>
  
  <pre><code>$attribute_array = array();
  $taxonomy_terms = array();
  $attribute_taxonomies = wc_get_attribute_taxonomies();
  
  if ( $attribute_taxonomies ){
      foreach ( $attribute_taxonomies as $tax ){
          if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax-&gt;attribute_name ) ) ){
              $attribute_array[ $tax-&gt;attribute_name ] = $tax-&gt;attribute_name;                                    
          }
      }
  }
  
  foreach ($attribute_taxonomies as $tax) {
      if (taxonomy_exists(wc_attribute_taxonomy_name($tax-&gt;attribute_name))){
          $taxonomy_terms[$tax-&gt;attribute_name] = get_terms( wc_attribute_taxonomy_name($tax-&gt;attribute_name));
      }
  }
  
  
  $options = array();
  $options_terms = array();
  $options[] = "&lt;option value=''&gt;--?--&lt;/option&gt;";
  $options_terms[] = "&lt;option value=''&gt;--?--&lt;/option&gt;";
  foreach ( $attribute_taxonomies as $tax ) {
      $options[] = "&lt;option value='{$tax-&gt;attribute_name}'&gt;{$tax-&gt;attribute_label}&lt;/option&gt;";
      foreach ($attribute_array as $key ) {
          if ($key == $tax-&gt;attribute_name) {
              foreach ($taxonomy_terms[$key] as $term) {
                  $options_terms[] = "&lt;option value='{$term-&gt;name}'&gt;{$term-&gt;name}&lt;/option&gt;";
              }
          }
      }
  }
  </code></pre>
  
  <p>Can I do this part with jquery? Or can I link them with php?</p>
  
  <p>EDIT3:</p>
  
  <p>I edited the option value for the terms options so that the value corresponds the attribute name</p>
  
  <pre><code>$options_terms[] = "&lt;option value='{$tax-&gt;attribute_name}'&gt;{$term-&gt;name}&lt;/option&gt;";
  </code></pre>
  
  <p>Now I could link them somehow if the values are the same, no?</p>