Set Woocommerce State

Most clients want to limit the number of states they sell to…but for reasons beyond comprehension Woocommerce does not make this easy.

Here is some code to exclude a few state.

You  could also set the $states[‘US’] array to just the states you want if that would be easier.

Add Item to WooCommerce From Outside Shop

Sometimes I need to setup donations or memberships or some other item that needs to be added to a users cart but NOT be listed in the shop. First thing we should do is make sure to hide these items in the shop. For this to work you will need to set your Product Category. It can be anything you want of course. Then place this code in your functions.php file

Now that these items are hidden on the shop page (you may also find you need to hide them on other pages depending on your setup but just add those pages to the “if” check. Next we need to setup the code to add this to the cart.

Here is the basic code to add this to the cart. I have also setup a check to make sure no other items of the same category are in the cart, and if they are remove them and add this one. Then, if successful, direct the user to the cart page.

Get Product by SKU

Sometimes you need to find the page id of your product simply using the sku. I ran into this when I had to build a backend inventory manager which updated Receiving and Sales figures based off of some spreadsheets.

Stolen from:

NOTE: The site I was working with was actually a Marketpress site and not a Woocommerce site. I ended up modifying the code from skyverge a little bit to fit my needs. Same idea.