Home  >  Article  >  Backend Development  >  How to Programmatically Create WooCommerce Product Variations with New Attribute Values?

How to Programmatically Create WooCommerce Product Variations with New Attribute Values?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-16 09:21:02155browse

How to Programmatically Create WooCommerce Product Variations with New Attribute Values?

Create a WooCommerce Product Variation with New Attribute Values

When creating variable products in WooCommerce, you may want to add new attribute values to the product's variations. Here's a step-by-step guide on how to accomplish this programmatically:

1. Define the Product Variation Data:

Create an array containing the variation data, including attribute values, SKU, prices, and stock quantity. An example is provided below:

$variation_data = [
    'attributes' => [
        'size' => 'M',
        'color' => 'Blue'
    ],
    'sku' => 'my-product-variation',
    'regular_price' => 19.99,
    'stock_qty' => 10
];

2. Get the Variable Product Object:

Obtain the object of the variable product you want to add variations to:

$product = wc_get_product($product_id); // Replace $product_id with the actual product ID

3. Create the Variation Post:

Configure the post data for the new product variation:

$variation_post = [
    'post_title' => $product->get_title() . ' Variation', // Set the variation title
    'post_name' => 'product-' . $product_id . '-variation',
    'post_status' => 'publish',
    'post_parent' => $product_id,
    'post_type' => 'product_variation',
    'guid' => $product->get_permalink()
];

4. Insert the Variation Post:

Create the new product variation:

$variation_id = wp_insert_post($variation_post);

5. Set the Attribute Data:

Iterate through the variation attributes and set them in the product variation:

foreach ($variation_data['attributes'] as $attribute => $term_name) {
    $taxonomy = 'pa_' . $attribute;
    update_post_meta($variation_id, 'attribute_' . $taxonomy, $term_name);
}

6. Set Other Data:

Set the remaining variation data like SKU, prices, and stock quantity:

$variation = new WC_Product_Variation($variation_id); // Get the variation object

if (!empty($variation_data['sku'])) {
    $variation->set_sku($variation_data['sku']);
}

$variation->set_price($variation_data['regular_price']);

if (!empty($variation_data['stock_qty'])) {
    $variation->set_stock_quantity($variation_data['stock_qty']);
}

$variation->save(); // Save the variation

By following these steps, you can programmatically create product variations with new attribute values in WooCommerce.

The above is the detailed content of How to Programmatically Create WooCommerce Product Variations with New Attribute Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn