Woocommerce
-
Finally here… required PHP version
I talked about it many times, I thought about it even more. However I was stupid enough not to talk about it to WordPress team. Well, somebody did. On March 2013 as far as I remember. And finally it is here… We can mark miminal required PHP version in plugins! And maybe in themes. It wasn’t very clear, but they mentioned themes As a next step, the WordPress core team is going look into showing users a notice that they cannot install a certain plugin or theme because their install does not meet the required criteria. I won’t test it, because I don’t have any themes in WordPress repository. However,…
-
Get woocommerce random products
Sometimes people asks me how to get N random products from Woocommerce plugin. Well, I heard it isn’t obvious or easy. And still I don’t know why. It is more than easy! We just need to use get_posts function. Simplest function to get N random products (default value is three as you may see in the source code) would look like that: [crayon-67a816d5b15b6013185768/]
-
Add woocommerce category programmatically
As I promised in this article Assign woocommerce product categories programmatically, I will explain how to create woocommerce category programmatically. It is very easy, just use this code: [crayon-67a816d5b1d13665862310/] You should get something like this: [crayon-67a816d5b1d17700891219/] However, sometimes you may get WP_Error. Sometimes, category may already exists. You must check the result, if it isn’t WP_Error object. So, if you need to add category or get existing category ID, use this: [crayon-67a816d5b1d19102668630/] This code uses PHP7 construction (?? – null coalesce operator), you may substitute it with isset when using earlier PHP version (or much better – upgrade your PHP or your hosting operator!) Remember to wrote this code in…
-
Assign woocommerce product categories programmatically
How to assign categories for your Woocommerce products programmatically? Well, it isn’t that hard. First of all, you must know the ID of your product. If you insert your product programmatically, it is easy, you got it. If not, you can see ID for many ways, for example in your edit screen (something like http://example.com/wp-admin/post.php?post=123&action=edit – 123 is your product ID). Let’s assume you’ve got your product ID in the $product_id variable. Now, you must decide if you want to assign product to existing category or create category by yourself? Well, if you already got category, you need your term ID. How to get it? Go to product categories list…
-
Insert new woocommerce product programmatically
How to insert brand new Woocommerce product programmatically? Sometimes you need your own product importer. You may read database, XML, JSON, CSV, XLS, XLSX, ODS, absolutely anything. Now you got all your products in memory or cache or your temporary file and what’s next? You need to add this product to Woocommerce… Well, the product is – like almost anything in WordPress – post type with specific taxonomy and meta data set. So, first things first, insert your new post: [crayon-67a816d5b24d7762487143/] Of course this is not everything, that would be too easy. Now we need to add some meta information to the product post and that is why, we take…
-
Woocommerce product category fields
Yeah, we step into Woocommerce chambers. This is a huge, good plugin, widely recognizable, used on many many e-commerce sites. They got nice product category taxonomy and it works fine. But what if we need a few more fields? It’s not that hard as you may think so, because Woocommerce is well written plugin, we can hook into it without any trouble. We need four action hooks and three functions. I will use methods, because I prefer object oriented programming, but it’s pretty easy to convert it into functional programming. You may ask, why only three functions, when we want to use four hooks? Two hooks will be handled by…