Set product SKU same as product ID in WooCommerce

add_filter( 'woocommerce_product_get_sku', 'wpcb_set_product_sku', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpcb_set_product_sku' ) ) {
	/**
	 * wpcb_set_product_sku.
	 * 
	 * @see https://wpcodebook.com/snippets/set-product-sku-by-product-id-in-woocommerce
	 */
	function wpcb_set_product_sku( $sku, $product ) {
		return $product->get_id();
		// more complex example, which includes adding up to six leading zeros and setting custom prefix in SKU, would be:
		// return sprintf( 'MY_PREFIX-%06s', $product->get_id() );
	}
}

No comments yet.

Leave a Reply