Create Attributes from an Array in WooCommerce

Problem: How to Programmatically Create Attributes from an Array in WooCommerce ?

I need to create product attributes in WooCommerce programmatically using an array. These attributes need to be set up dynamically during a bulk import or automated process without manually adding them in the admin area.

 

Introduction:

When working with WooCommerce, there may be instances where you need to create attributes programmatically rather than manually through the dashboard. This is especially useful for bulk operations or when importing products from external sources. In this guide, we’ll show you how to create attributes in WooCommerce using PHP and an array.

 

Solution:

You can create product attributes in WooCommerce using the wp_insert_term() function, which allows you to add terms and taxonomies programmatically. Here’s how you can do this step-by-step:

Step 1: Define Your Attributes in an Array

First, define an array of attributes with the attribute name and associated terms (options).

$attributes_array = [
'color' => ['Red', 'Blue', 'Green'],
'size' => ['Small', 'Medium', 'Large'],
'material' => ['Cotton', 'Wool', 'Polyester']
];

Step 2: Create a Function to Insert Attributes Programmatically

This function loops through your array and adds the attributes and their terms (options) programmatically.

function create_woocommerce_attributes_from_array($attributes_array) {
global $wpdb;

foreach ($attributes_array as $attribute_name => $terms) {
// Create the attribute taxonomy slug
$attribute_slug = 'pa_' . sanitize_title($attribute_name);

// Check if the attribute taxonomy already exists
$taxonomy_exists = $wpdb->get_var($wpdb->prepare("
SELECT attribute_id
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name = %s
", sanitize_title($attribute_name)));

// If the taxonomy does not exist, create it
if (!$taxonomy_exists) {
wc_create_attribute([
'name' => ucfirst($attribute_name),
'slug' => $attribute_slug,
'type' => 'select',
'orderby' => 'menu_order',
'has_archives' => false,
]);

// Register the new taxonomy to make it available for terms
register_taxonomy(
$attribute_slug,
apply_filters('woocommerce_taxonomy_objects_' . $attribute_slug, ['product']),
apply_filters('woocommerce_taxonomy_args_' . $attribute_slug, [
'hierarchical' => true,
'show_ui' => false,
'query_var' => true,
'rewrite' => false,
])
);
}

// Add terms to the attribute if they do not already exist
foreach ($terms as $term) {
if (!term_exists($term, $attribute_slug)) {
wp_insert_term($term, $attribute_slug);
}
}
}

// Clear caches and flush rules after adding attributes and terms
wc_delete_product_transients();
delete_transient('wc_attribute_taxonomies');
}

// Sample attributes array
$attributes_array = [
'color' => ['Red', 'Blue', 'Green'],
'size' => ['Small', 'Medium', 'Large'],
'material' => ['Cotton', 'Wool', 'Polyester']
];

// Call the function to create the attributes and terms
create_woocommerce_attributes_from_array($attributes_array);

 

Step 3: Implement the Code

You can add this code to your theme’s functions.php file or within a custom plugin.

Step 4: Verify the Attributes

Once you run this code, the attributes and terms should appear in the WooCommerce product attributes section, ready to be used in your products.

Conclusion:

Programmatically creating WooCommerce attributes from an array allows you to automate and streamline your operations, saving time when managing large catalogs or complex imports. By using the provided function, you can dynamically add attributes and their terms without manual input, making your WooCommerce setup more efficient.

 

Related Blog

Sign up for our newsletter to stay up to
date with tech news!