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
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)