WooCommerce change category url and product base

Got to Settings -> Permalinks ->
In ‘Product permalink base’ select the ‘Shop base with category’ option.

After saving if the .htaccess file is not writable then WordPress prompts you to create the .htaccess file and add the code which the mention after saving in the .htaccess file.

The reason that it is giving 404 error is that the Permalink structure needs the code to be added to .htaccess file or the settings which you saved will only apply when not using “default” permalinks for ‘Common Settings’.