• This is my function:

    function newProduct( $idERP, $titulo, $conteudo, $preco, $precoDesconto, $sku, $peso, $comprimento, $largura, $altura, $estoque, $variacoes, $categorias ){
    
            global $woocommerce;
    
            $titulo = utf8_encode($titulo);
            $conteudo = utf8_encode($conteudo);
    
            $my_post = array(
              'post_title'    => $titulo,
              'post_content'  => $conteudo,
              'post_status'   => 'pending',
              'post_author'   => 6,
              'post_type'     =>'product'
            );
            $product_ID = wp_insert_post( $my_post );
    
            if ( $product_ID ){
    
              if( $preco == $precoDesconto){
                  add_post_meta($product_ID, '_price',         $precoDesconto );
              }else{
                  add_post_meta($product_ID, '_regular_price', $preco );
                  add_post_meta($product_ID, '_price',         $precoDesconto );
                  add_post_meta($product_ID, '_sale_price',    $precoDesconto );
              }
    
              add_post_meta($product_ID, '_sku',           $sku );
              add_post_meta($product_ID, '_weight',        $peso );
              add_post_meta($product_ID, '_length',        $comprimento );
              add_post_meta($product_ID, '_width',         $largura );
              add_post_meta($product_ID, '_height',        $altura );
              add_post_meta($product_ID, '_stock',         $this->estoque($estoque) );
              add_post_meta($product_ID, '_idERP',         $idERP ); //ID from erp
              wp_set_object_terms( $product_ID,  'variable', 'product_type' );
              //wp_set_object_terms( $product_ID, $categorias, 'product_cat');
    
              $guardaVariacoes = array();$i = 1;
              foreach($variacoes as $v){
                $vid = wp_insert_post(
                    array(
                      'post_title'    => 'Variacao #' . $i,
                      'post_name'     => 'product-' . $product_ID . '-variation-' . $i,
                      'post_status'   => 'publish',
                      'post_parent'   => $product_ID,
                      'post_type'     => 'product_variation',
                      'guid'          =>  home_url() . '/?product_variation=product-' . $product_ID . '-variation-' . $i
                    )
                );
    
                if( $v[1] == $v[2] ){
                    update_post_meta( $vid, '_regular_price', $v[1]);
                }else{
                    update_post_meta( $vid, '_price', $v[2] );
                    update_post_meta( $vid, '_regular_price', $v[1]);
                    update_post_meta( $vid, '_sale_price',  $v[2] );
                }
    
                update_post_meta( $vid, '_stock', $this->estoque($v[0]) );
                update_post_meta( $vid, '_weight', $v[3]);
                update_post_meta( $vid, '_width', $v[4]);
                update_post_meta( $vid, '_height', $v[5]);
                update_post_meta( $vid, '_length', $v[6]);
                update_post_meta( $vid, '_sku', $v[8]);
                update_post_meta( $vid, '_idERP', $v[9]);
    
                foreach($v[7] as $nAtr=>$vAtr){
                    $nomeAtributo = strtolower(trim($nAtr));
    
                     $tt = $nomeAtributo;
                     if(strtolower($tt) == 'tamanho')$tt = 'pa_tamanho';
                     if(strtolower($tt) == 'cor')$tt = 'pa_cor';
    
                    update_post_meta( $vid, 'attribute_'.$tt, trim($vAtr));
                    $temp = $tt.'|'.$nAtr;
                    if( !array_key_exists($temp,$guardaVariacoes) ) $guardaVariacoes[$temp] = array();
                    $guardaVariacoes[$temp][] = $vAtr;
                }
                $i++;
              }
    
              $product_attributes = array();
              foreach($guardaVariacoes as $r=>$s){
    
                  $r = explode('|',$r);
    
                  $temp = array();
                  foreach($s as $w){
    
                      $w = trim($w);
    
                      if( strtoupper($w) == 'UNICA'  ){
                          $w = 'Única';
                      }
                      if( strtoupper($w) == 'UNICO'  ){
                          $w = 'Único';
                      }
    
                      if( array_search( $w,$temp)===false){
                         if( strtoupper(trim($r[1])) == 'COR'){
                             $temp[] = ucfirst(strtolower($w));
                          }else{
                              $temp[] = $w;
                        }
                      }
                  }
                  $s = array_unique($temp);
    
                 $tt = $r[1];
                 if(strtolower($tt) == 'tamanho')$tt = 'pa_tamanho';
                 if(strtolower($tt) == 'cor')$tt = 'pa_cor';
    
                  $product_attributes[$r[0]] = array(
                    'name' => $tt,
                    'value' => implode(' | ', $s ),
                    'position' => 1,
                    'is_visible' => 1,
                    'is_variation' => 1,
                    'is_taxonomy' => 1  //MY PROBLEM IS HERE
                  );
              }
              update_post_meta($product_ID, '_product_attributes', $product_attributes);
            }
            return $product_ID;
        }

    When I set that line “is_taxonomy” to “0” (zero).

    Ok, it register great. But the product doesnt show in that.
    “Layered filter search Widget”, when you search something in sidebar you can select an attribute of result item.. like color or size and it filter the search result.

    If I set “is_taxonomy” to “1” as the code. I dont see the attributes when editing the product.

    So since there are my first days with woocommerce, I still didnt understand.
    How can I make it import right the attributes (like when I set is_taxonomy” as zero.
    But this attributes being used by sidebar search filter results.

    https://wordpress.org/plugins/woocommerce/

  • The topic ‘Help addProduct() from other system’ is closed to new replies.