Creating Simple Product using WooCommerce CRUD

WooCommerce 3.0 had released a new way of creating, updating or deleting a product or an order using CRUD method. In this tutorial, we'll be learning how to use it to create or update a product.
<?php
$objProduct = new WC_Product();

$objProduct->set_name('My Product 1'); //Set product name.
$objProduct->set_status('publish'); //Set product status.
$objProduct->set_featured(TRUE); //Set if the product is featured.                          | bool
$objProduct->set_catalog_visibility('visible'); //Set catalog visibility.                   | string $visibility Options: 'hidden', 'visible', 'search' and 'catalog'.
$objProduct->set_description('My custom long description'); //Set product description.
$objProduct->set_short_description('My short description'); //Set product short description.
$objProduct->set_sku('U-123'); //Set SKU

$objProduct->set_price(5.00); //Set the product's active price.
$objProduct->set_regular_price(5.00); //Set the product's regular price.
$objProduct->set_sale_price(); //Set the product's sale price.
$objProduct->set_date_on_sale_from(); //Set date on sale from.                              | string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
$objProduct->set_date_on_sale_to();//Set date on sale to.                                   | string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.

$objProduct->set_manage_stock(TRUE); //Set if product manage stock.                         | bool
$objProduct->set_stock_quantity(10); //Set number of items available for sale.
$objProduct->set_stock_status('instock'); //Set stock status.                               | string $status 'instock', 'outofstock' and 'onbackorder'
$objProduct->set_backorders('no'); //Set backorders.                                        | string $backorders Options: 'yes', 'no' or 'notify'.
$objProduct->set_sold_individually(FALSE); //Set if should be sold individually.            | bool

//$objProduct->set_weight(); //Set the product's weight.
//$objProduct->set_length(); //Set the product length.
//$objProduct->set_width(); //Set the product width.
//$objProduct->set_height(); //Set the product height.

//$objProduct->set_upsell_ids($upsell_ids); //Set upsell IDs.                               | array $upsell_ids IDs from the up-sell products.
//$objProduct->set_cross_sell_ids($cross_sell_ids); //Set crosssell IDs.                    | array $cross_sell_ids IDs from the cross-sell products.

$objProduct->set_reviews_allowed(TRUE); //Set if reviews is allowed.                        | bool

//$objProduct->set_purchase_note($purchase_note); //Set purchase note.                      | string $purchase_note Purchase note.


$attribute = new WC_Product_Attribute();
$attribute->set_id(wc_attribute_taxonomy_id_by_name('pa_color')); //if passing the attribute name to get the ID
$attribute->set_name('pa_color'); //attribute name
$attribute->set_options(['red']); // attribute value
$attribute->set_position(1); //attribute display order
$attribute->set_visible(1); //attribute visiblity
$attribute->set_variation(0);//to use this attribute as varint or not

$raw_attributes[] = $attribute; //<--- storing the attribute in an array

$attribute = new WC_Product_Attribute();
$attribute->set_id(25);
$attribute->set_name('pa_size');
$attribute->set_options(['XL']);
$attribute->set_position(2);
$attribute->set_visible(1);
$attribute->set_variation(0);

$raw_attributes[] = $attribute; //<--- storing the attribute in an array

$objProduct->set_attributes($raw_attributes); //Set product attributes.                   | array $raw_attributes Array of WC_Product_Attribute objects.

$objProduct->set_category_ids($term_ids); //Set the product categories.                   | array $term_ids List of terms IDs.
$objProduct->set_tag_ids($term_ids); //Set the product tags.                              | array $term_ids List of terms IDs.

$objProduct->set_image_id(); //Set main image ID.                                         | int|string $image_id Product image id.
$objProduct->set_gallery_image_ids(); //Set gallery attachment ids.                       | array $image_ids List of image ids.

$new_product_id = $objProduct->save(); //Saving the data to create new product, it will return product ID.
Default image
Raunak Gupta
I'm an expert Laravel, WooCommerce, WordPress theme and plugin developer, over the time I have developed several sites and applications. I like to help the upcoming developer, So I share my experience through this blog.
Articles: 18

4 Comments

  1. Just a comment here!
    the set_options() method gets an array and not a string

    $attribute->set_options([‘XL’]);

    https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product_Attribute.html#157-164

  2. Very nice blog post. I absolutely appreciate this site. Jordan Vaughn Chace

  3. pwede din po bang paki send sakin yung reviewer for cse pro paki send nalng din po sa email ko thanks. Blakeley Mathew Iila.

Leave a Reply