Support » Plugin: WooCommerce » set attribute as variation via csv

  • I am importing products and attribute/variations via csv file.

    Everything is importing how I want it to work, except the products that use variations are not showing up on the main shop page. But they will show up when you go to products and then view that product.

    I did something similar to this

    // get attribute values from csv
          $attr_values = $fileop[13];
          if (!empty($attr_values)) {
          //Gets the attribute name
          $string = $attr_values;
          $string = preg_replace('/^([^:]*).*$/', '$1', $string); //split string into array seperated by ', '
          $stringTitle = strtolower($string);
          $attr_pa = 'attribute_pa_'.$stringTitle.'';
          $attr_names = 'pa_'.$stringTitle.'';
          $stringValues = $fileop[13];
          $stringValues = preg_replace('/^[^:]*:\s*/', '', $stringValues);
          $array = explode('|', $stringValues); //split string into array seperated by ', '
          //insert variations post_type
          wp_set_object_terms ($prodID, 'variable', 'product_type');
          wp_set_object_terms( $prodID, $array, $attr_names );
          $thedata = array(
            $attr_names=> array(
                            'is_visible' => '1',
                            'is_variation' => '1',
                            'is_taxonomy' => '1'
            update_post_meta( $prodID,'_product_attributes',$thedata);
            update_post_meta( $prodID, '_visibility', 'search' );
            update_post_meta( $prodID, '_stock_status', 'instock');
          foreach($array as $value) //loop over values
              $my_post = array(
                'post_title'    => 'Variation #' . $i . ' of ' .$ptitle,
                'post_name'     => 'product-' . $prodID . '-variation-' . $i,
                'post_status'   => 'publish',
                'post_parent'   => $prodID,
                'post_type'     => 'product_variation',
                'guid'          =>  home_url() . '/?product_variation=product-' . $prodID . '-variation-' . $i
              wp_insert_post( $my_post );
              $variable_id = $prodID + $i;
              $stringLower = strtolower($value);
              update_post_meta( $variable_id, $attr_pa, $stringLower);
              update_post_meta( $variable_id, '_price', $regularprice );
              update_post_meta( $variable_id, '_regular_price', $regularprice);

    For the attributes to show up I had to

    delete_transient( 'wc_attribute_taxonomies' );

    after importing the attributes csv template. Otherwise they would not show up right away, they would show up after editing one of the attributes, or manually creating a new attribute. However if I deleted the transient they would then show up right away.

    So this has me wondering if the product variations also use a transient?

    Anyone have any ideas?

    Screenshot of a product variation after importing.

    Here is a screenshot of the attributes

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘set attribute as variation via csv’ is closed to new replies.