WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
set attribute as variation via csv (2 posts)

  1. bowenac
    Member
    Posted 5 months ago #

    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 http://stackoverflow.com/questions/21667292/how-to-set-attribute-as-variation-in-woocommerce

    // 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(
                            'name'=>$attr_names,
                            'value'=>'',
                            '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');
          $i=1;
          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);
              $i++;
          }

    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.
    http://i880.photobucket.com/albums/ac7/bowenac/productvariation_zps4bd12c38.png

    Here is a screenshot of the attributes
    http://i880.photobucket.com/albums/ac7/bowenac/attributes_zpsc3b0e99d.png

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

  2. bowenac
    Member
    Posted 5 months ago #

    NM I got it... had to change search to visible from the other example I was going off.

Reply

You must log in to post.

About this Plugin

About this Topic