Last updated on October 18, 2023

WooCommerce product SKU management

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Effectively manage product SKUs in WooCommerce.

The code snippet provided below demonstrates how to manage WooCommerce product SKUs using custom PHP functions that adhere to the WordPress coding standards.

To retrieve the SKU of a WooCommerce product, you can use the get_sku() function. This function returns the SKU value for a given product ID. Here’s an example:

$product_id = 123; // Replace with the actual product ID
$sku = get_sku( $product_id );

To update the SKU of a WooCommerce product, you can use the update_post_meta() function. This function allows you to update the meta value of a specific post (product) by providing the post ID, meta key, and new meta value. In the case of WooCommerce, the SKU is stored as a meta value with the key _sku. Here’s an example:

$product_id = 123; // Replace with the actual product ID
$new_sku = 'ABC123'; // Replace with the new SKU value
update_post_meta( $product_id, '_sku', $new_sku );

To delete the SKU of a WooCommerce product, you can use the delete_post_meta() function. This function removes a specific meta value from a post (product) by providing the post ID and meta key. In this case, the meta key is _sku. Here’s an example:

$product_id = 123; // Replace with the actual product ID
delete_post_meta( $product_id, '_sku' );

These code snippets can be useful when you need to programmatically retrieve, update, or delete the SKU of a WooCommerce product. For example, you may want to synchronize product data with an external system or perform bulk updates to SKUs based on certain criteria.

Examples

Example 1: Adding a custom SKU prefix to WooCommerce products

This use case demonstrates how to add a custom prefix to the SKU of WooCommerce products. The code example below uses the woocommerce_product_sku filter to modify the SKU before it is displayed.

function wpsnippets_add_sku_prefix( $sku ) {
    $prefix = 'ABC-';
    return $prefix . $sku;
}
add_filter( 'woocommerce_product_sku', 'wpsnippets_add_sku_prefix' );

In this code example, the wpsnippets_add_sku_prefix function adds a custom prefix (ABC-) to the SKU of WooCommerce products. The function is hooked to the woocommerce_product_sku filter, which allows us to modify the SKU before it is displayed.

Example 2: Removing SKU from WooCommerce product pages

This use case demonstrates how to remove the SKU from WooCommerce product pages. The code example below uses the woocommerce_get_sku filter to return an empty string for the SKU.

function wpsnippets_remove_sku( $sku ) {
    return '';
}
add_filter( 'woocommerce_get_sku', 'wpsnippets_remove_sku' );

In this code example, the wpsnippets_remove_sku function returns an empty string, effectively removing the SKU from WooCommerce product pages. The function is hooked to the woocommerce_get_sku filter, which allows us to modify the SKU value.

Example 3: Updating SKU for WooCommerce products programmatically

This use case demonstrates how to update the SKU for WooCommerce products programmatically. The code example below uses the update_post_meta function to update the SKU value for a specific product.

$product_id = 123;
$new_sku = 'NEW-SKU-123';

update_post_meta( $product_id, '_sku', $new_sku );

In this code example, the update_post_meta function is used to update the SKU value for a specific product. The $product_id variable represents the ID of the product, and the $new_sku variable holds the new SKU value. The _sku meta key is used to store the SKU value for WooCommerce products.

Last updated on October 18, 2023. Originally posted on November 29, 2023.

Leave a Reply