0

I'm trying to create two product attributes "width" and "height" programmatically for a variable product:

  • "Width" product attribute will have 13 values (24,30,36.....96)
  • "Height" product attribute will have 18 values (18,24,30....120)

So the variable product will have as total variations:
13 (width) x 18 (height) = 234 variations with different price values that should be create automatically (auto fill) in front end on products that belongs to specific product category.

For example (in single products page) when user select:

  • option value 24 in "width" drop-down
  • option value 18 in "height" drop-down

the price should be $38 for this variation

In same way when user choose:

  • width = 30
  • height = 18
  • price = $45

Attributes Some person provide me code but as newbie couldn't figure out the code:

//this will assign attribute and variable to category "ABC" only
  $target_products = array(
     'post_type' => 'product',
     'post_status' => 'publish',
     'product_cat' => 'ABC',
     'posts_per_page'=>-1
);

// This will display attribute and variable auto fill in front end
$variation_data =  array(
    'attributes' => array(
        'width'  => '24',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '38.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

$variation_data =  array(
    'attributes' => array(
        'width'  => '30',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '45.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);


$variation_data =  array(
    'attributes' => array(
        'width'  => '36',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '52.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

I was told that the above code will do the job, when user will select a product from a specific product category ("ABC") while creating new products, attributes width and height will be created along with 234 variable (I don't have create attribute and variable manually)

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
syner
  • 51
  • 10
  • You should try to reword your question as it's not really clear. You should also try to keep only in it the necessary. Also we don't understand what help do you need on it… – LoicTheAztec Apr 02 '18 at 18:26
  • as you can see width and height with price in picture, what i want is that when user select category "ABC", this width and height attributes should be create in front end with price value automatically on selection of category "ABC" – syner Apr 02 '18 at 18:47
  • @LoicTheAztec made changes to my question and point direct to my need – syner Apr 02 '18 at 19:30

1 Answers1

0

i found link where it say Programmatically Add an Attribute in Woocommerce to a Product code i as below but its not adding attributes on creating new products (put this code inside function.php)

  $target_products = array(
     'post_type' => 'product',
     'post_status' => 'publish',
     'product_cat' => 'Living Room Light Balls',
     'posts_per_page'=>-1
);

$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {

   while ($my_query->have_posts()) : $my_query->the_post(); 

     $term_taxonomy_ids = wp_set_object_terms( get_the_ID(), '0.65m', 'pa_diameter', true );
     $thedata = Array('pa_diameter'=>Array(
       'name'=>'pa_diameter',
       'value'=>'0.65m',
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));
     update_post_meta( get_the_ID(),'_product_attributes',$thedata); 

   endwhile;
}

wp_reset_query();
syner
  • 51
  • 10
  • i followed the below link and add all code by @LoicTheAztec in function.php but its doesn't create attributes and variable in fronted https://stackoverflow.com/questions/47518333/create-programmatically-a-variable-product-and-two-new-attributes-in-woocommerce/47844054#47844054 – syner Apr 03 '18 at 04:02