Home >Backend Development >PHP Tutorial >How to Create WooCommerce Product Variations with New Attributes Programmatically?

How to Create WooCommerce Product Variations with New Attributes Programmatically?

Linda Hamilton
Linda HamiltonOriginal
2024-11-18 11:30:02346browse

How to Create WooCommerce Product Variations with New Attributes Programmatically?

Create WooCommerce Product Variations with New Attributes Programmatically

When working with variable products in WooCommerce 3 , you may encounter the need to create variations programmatically. This can be achieved while also creating new attribute values and setting them within the parent variable product.

Creating Product Variations

To create a variation for a variable product, we can use the following custom function:

/**
 * Create a product variation for a defined variable product ID.
 *
 * @since 3.0.0
 * @param int   $product_id | Post ID of the product parent variable product.
 * @param array $variation_data | The data to insert in the product.
 */
function create_product_variation( $product_id, $variation_data ){
    // Get the Variable product object (parent)
    $product = wc_get_product($product_id);

    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    // Creating the product variation
    $variation_id = wp_insert_post( $variation_post );

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

}

Handling Attribute Values and Taxonomy Creation

Within the function, we enhance functionality by handling attribute value checking and creation:

// Iterating through the variations attributes
foreach ($variation_data['attributes'] as $attribute => $term_name )
{
    $taxonomy = 'pa_'.$attribute; // The attribute taxonomy

        // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
        if( ! taxonomy_exists( $taxonomy ) ){
            register_taxonomy(
                $taxonomy,
               'product_variation',
                array(
                    'hierarchical' => false,
                    'label' => ucfirst( $attribute ),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug
                ),
            );
        }

        // Check if the Term name exist and if not we create it.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term
}

Usage

To utilize this function, provide it with the variable product ID and the following data array:

// The variation data
$variation_data =  array(
    'attributes' => array(
        'size'  => 'M',
        'color' => 'Green',
    ),
    'sku'           => '',
    'regular_price' => '22.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

Conclusion

Through this function, you can now programmatically create product variations with new attribute values, setting them up within the parent variable product seamlessly.

The above is the detailed content of How to Create WooCommerce Product Variations with New Attributes Programmatically?. 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