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.

Product Create

WooCommerce provides a native Product Data Schema to create or manipulate a product. Here is an example of how to programmatically create a product in WooCommerce.

<?php
$objProduct = new WC_Product();

//Set product name.
$objProduct->set_name('My Product 1');

//Set product status.
$objProduct->set_status('publish');

//Set if the product is featured. | bool
$objProduct->set_featured(TRUE);

//Set catalog visibility. | string $visibility Options: 'hidden', 'visible', 'search' and 'catalog'.
$objProduct->set_catalog_visibility('visible');

//Set product description.
$objProduct->set_description('My custom long description');

//Set product short description.
$objProduct->set_short_description('My short description');

//Set SKU
$objProduct->set_sku('U-123');


//Set the product's active price.
$objProduct->set_price(5.00);

//Set the product's regular price.
$objProduct->set_regular_price(5.00);

//Set the product's sale price.
$objProduct->set_sale_price();

//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_from();

//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_date_on_sale_to();


//Set if product manage stock. | bool
$objProduct->set_manage_stock(TRUE);

//Set number of items available for sale.
$objProduct->set_stock_quantity(10);

//Set stock status. | string $status 'instock', 'outofstock' and 'onbackorder'
$objProduct->set_stock_status('instock');

//Set backorders. | string $backorders Options: 'yes', 'no' or 'notify'.
$objProduct->set_backorders('no');

//Set if should be sold individually. | bool
$objProduct->set_sold_individually(FALSE);

//$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.

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


//$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.

//Saving the data to create new product, it will return product ID.
$new_product_id = $objProduct->save();

Product Update

To programmatically update an existing product we can use the same Product Data Schema just by passing a product ID during initialization. If the product ID is not known and only SKU is known then we can use wc_get_product_id_by_sku(); method to get the product ID.

<?php

//if Product ID is known
$objProduct = new WC_Product(123);

//Alternative
//if SKU is known insted of Product ID
$productID = wc_get_product_id_by_sku('my-sku');
$objProduct = new WC_Product($productID);

//now that you have the product object you can manipulate it's value

//Set product name.
$objProduct->set_name('My updated product title');

//…
//…
//…

$productID = $objProduct->save();

Product View

To view or to get the product details, we can use the get_ method of the product class. Here is an example to fetch the details of a product by ID.

<?php
$objProduct = new WC_Product(123);

//Get product name.
$productName = $objProduct->get_name();

//Get product status.
$objProduct->get_status();

//Get if the product is featured.
$objProduct->get_featured();

//Get catalog visibility.
$objProduct->get_catalog_visibility();

//Get product description.
$objProduct->get_description();

//Get product short description.
$objProduct->get_short_description();

//Get SKU
$objProduct->get_sku();

//Get the product's active price.
$objProduct->get_price();

//Get the product's regular price.
$objProduct->get_regular_price();

//Get the product's sale price.
$objProduct->get_sale_price();

//Get date on sale from.
$objProduct->get_date_on_sale_from();

//Get date on sale to.
$objProduct->get_date_on_sale_to();

//Get if product manage stock.
$objProduct->get_manage_stock();

//Get number of items available for sale.
$objProduct->get_stock_quantity();

//Get stock status.
$objProduct->get_stock_status();

//Get backorders.
$objProduct->get_backorders();

//Get if should be sold individually.
$objProduct->get_sold_individually();

//$objProduct->get_weight(); //Get the product's weight.
//$objProduct->get_length(); //Get the product length.
//$objProduct->get_width(); //Get the product width.
//$objProduct->get_height(); //Get the product height.

//Get if reviews is allowed.
$objProduct->get_reviews_allowed();

//$objProduct->get_purchase_note($purchase_note); //Get purchase note.

$objProduct->get_category_ids($term_ids); //Get the product categories. | array $term_ids List of terms IDs.
$objProduct->get_tag_ids($term_ids); //Get the product tags. | array $term_ids List of terms IDs.

$objProduct->get_image_id(); //Get main image ID.
$objProduct->get_gallery_image_ids(); //Get gallery attachment ids.

Product Delete

To delete a product we have to use delete() of the product class. This method accepts a boolean parameter. True for permanent delete and false for soft delete. Here is an example of the same.

<?php

$objProduct = new WC_Product(123);

// If it is set to true, then the product will be permanently deleted.
// if it is set as false, then the product will be sent to trash from where it can be recovered, if needed.
$forceDelete = true

$success = $objProduct->delete($forceDelete);

if(!empty($success)){
echo "Product deleted successfully.";
}
else{
echo "Unable to delete the product.";
}

By using the above methodology one can easily able to manipulate WooCommerce products. If I missed out on any minute details then let me know via the below comment. For further reading, you can also refer to the below articles.

Raunak Gupta

Raunak Gupta

I'm Raunak Gupta, a seasoned software developer with over 9 years of experience in a wide range of programming languages, frameworks, and tools. I started my journey as a WordPress & CakePHP developer in 2014, diving deep into the world of OOPs, Request handling, and SEO. Along the way, I crafted numerous dazzling WooCommerce stores, tamed payment gateways, optimized for full filament functionality, and achieved ultra-low latency for lightning-fast load times. My expertise extends to BI tools, website builders, DevOps, and team leadership. I like to help upcoming developers, so I share my experience through this blog and by assisting fellow developers on Stack Overflow, where I've earned a stellar reputation with over 10k+ points of recognition.

Articles: 29

4 Comments

  1. 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

Your email address will not be published. Required fields are marked *