How to Create Products Programmatically in WordPress
In this tutorial, we will walk you through the process of programmatically creating different types of products in WooCommerce using code. This method leverages WooCommerce’s CRUD (Create, Read, Update, Delete) objects, which were introduced in WooCommerce 3.0, ensuring compatibility with the latest versions of WooCommerce.
CRUD Objects in WooCommerce
The CRUD objects we will use are:
WC_Product_Simple
– for simple productsWC_Product_External
– for external productsWC_Product_Grouped
– for grouped productsWC_Product_Variable
– for variable products
These objects and their methods allow us to configure our product data easily.
Simple Products
Let’s start by creating a simple product. A simple product typically includes a product name, slug (for URL), price, and an image.
$product = new WC_Product_Simple();
$product->set_name('Simple Product'); // Product title
$product->set_slug('how-to-add-simple-product-programmatically'); // URL slug
$product->set_regular_price(100.00); // Regular price
$product->set_short_description('<p>This is a sample product!!</p>'); // Short description
$product->set_image_id(123); // Replace Image ID with yours
// Suppose the 'Coding' category has ID = 111
$product->set_category_ids(array(111)); // Category ID
$product->save(); // Save the product
The code above is enough to create a simple product. There are additional methods you might find useful:
set_featured()
– Mark the product as featured.set_gallery_image_ids()
– Set multiple image IDs as an array.set_menu_order()
– Set manual product order.set_status()
– Set post status (e.g., ‘draft’).set_total_sales()
– Set product total sales.set_catalog_visibility()
– Set catalog visibility (e.g., ‘hidden’, ‘visible’).
Products on Sale
To create a product on sale, add the following methods before the $product->save();
line:
$product->set_regular_price(100.00);
$product->set_sale_price(75.00); // Sale price
$product->set_date_on_sale_from('2024-05-01'); // Sale start date
$product->set_date_on_sale_to('2024-05-31'); // Sale end date
Inventory Settings
The configuration of inventory settings can also be done by using these methods:
$product->set_sku('sample-hat'); // Unique SKU
$product->set_stock_status('instock'); // Stock status ('instock', 'outofstock', 'onbackorder')
$product->set_manage_stock(true); // Manage stock
$product->set_stock_quantity(10); // Stock quantity
$product->set_backorders('no'); // Backorders ('yes', 'no', 'notify')
$product->set_low_stock_amount(3); // Low stock amount
$product->set_sold_individually(true); // Sold individually
Dimensions and Shipping
You can set the product dimensions and shipping class by using the below code.
$product->set_weight(0.5); // Weight
$product->set_length(50); // Length
$product->set_width(50); // Width
$product->set_height(30); // Height
$product->set_shipping_class_id('sample-shipping'); // Shipping class ID
Linked Products
Set upsell and cross-sell products:
$product->set_upsell_ids(array(111, 122)); // Upsell product IDs
// $product->set_cross_sell_ids(array(121, 137, 119, 110)); // Cross-sell product IDs
Attributes
Add product attributes using the WC_Product_Attribute
class:
$attributes = array();
// Custom attribute
$attribute = new WC_Product_Attribute();
$attribute->set_name('Sample');
$attribute->set_options(array('Yes', 'No'));
$attribute->set_position(0);
$attribute->set_visible(true);
$attribute->set_variation(true);
$attributes[] = $attribute;
// Predefined taxonomy-based attribute
$attribute = new WC_Product_Attribute();
$attribute->set_id(wc_attribute_taxonomy_id_by_name('pa_color'));
$attribute->set_name('pa_color');
$attribute->set_options(array(29, 31));
$attribute->set_position(1);
$attribute->set_visible(true);
$attribute->set_variation(false);
$attributes[] = $attribute;
$product->set_attributes($attributes); // Set attributes
Virtual and Downloadable Products
To create a virtual and downloadable product:
$product->set_virtual(true); // Virtual product
$product->set_downloadable(true); // Downloadable product
$downloads = array();
$download = new WC_Product_Download();
$file_url = wp_get_attachment_url($attachment_id); // Attachment ID
$download->set_name('sample-product-download');
$download->set_id(md5($file_url));
$download->set_file($file_url);
$downloads[] = $download;
$product->set_downloads($downloads); // Set downloads
$product->set_download_limit(1); // Download limit
$product->set_download_expiry(7); // Download expiry (days)
External and Grouped Products
For external products:
$product = new WC_Product_External();
$product->set_name('External Product');
$product->set_product_url('https://externalproducturl.com'); // External product URL
$product->set_button_text('Buy Now'); // Button text
$product->save();
For grouped products:
$product = new WC_Product_Grouped();
$product->set_name('Grouped Product');
$product->set_children(array(110, 120, 130)); // Child product IDs
$product->save();
Variable Products
Creating a variable product involves setting up variations:
$product = new WC_Product_Variable();
$product->set_name('Variable Product');
$product->set_image_id(190); // Image ID
$attribute = new WC_Product_Attribute();
$attribute->set_name('Sample');
$attribute->set_options(array('Yes', 'No'));
$attribute->set_position(0);
$attribute->set_visible(true);
$attribute->set_variation(true);
$product->set_attributes(array($attribute)); // Set attributes
$product->save();
$variation = new WC_Product_Variation();
$variation->set_parent_id($product->get_id());
$variation->set_attributes(array('sample' => 'Yes'));
$variation->set_regular_price(1000000); // Price for sample
$variation->save();
$variation = new WC_Product_Variation();
$variation->set_parent_id($product->get_id());
$variation->set_attributes(array('sample' => 'No'));
$variation->set_regular_price(500); // Price for non-sample
$variation->save();
Set a default variation with:
$product->set_default_attributes(array('sample' => 'No'));
Conclusion
Creating different types of products programmatically in WooCommerce can streamline your store management, especially for large inventories or integrations with external systems. By using the CRUD objects and methods provided by WooCommerce, you can create simple, external, grouped, and variable products efficiently and customize them to fit your needs.
Feel free to modify the functions and product data arrays to suit your specific requirements. Happy coding!