How to add membership levels to WordPress WooCommerce, based on an amount of purchases? - wordpress

I want to add 2 membership statuses to my WordPress WooCommerce based website.
The first one would be Basic package - user gets a custom message at the top saying he has to make 2 purchases before he can get access to the rest of the page and the second package would be Premium package - user gets access to the whole page, only if he has made 2 purchases on the site.
How can I achieve this?

I think you should be looking for some kind of WooCommerce membership plugin, e.g. premium extension WooCommerce Membership or free plugin Groups that is available on wordpress.org. This would provide you with access control and the first one would also give you some additional WooCommerce-related features.
Neither of the above plugins, however, offer the solution that you are looking for out of the box and I am not aware of any plugins that would. You would need to write custom code that tracks how many orders user has placed to this date. This could be run when orders are marked completed. Then simply instruct the membership plugin to upgrade user's membership level (both of them have APIs to do so I guess).
This piece of code can be used to count number of customer's orders:
$user_id = 1; // Change to take your real user ID dynamically
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => 'completed',
),
),
);
$posts = get_posts($args);
$number_of_orders_to_date = count($posts); // This is your answer

Related

How to add a new sortable date meta to a custom post type

Looking for a point in the right direction, i've been working on an events plugin for a site, quite a simple plugin, adds a custom post type called events, adds some custom taxonomies, used ACF to add the fields to the CPT.
But I need to add 'start date and time', and 'end date and time' fields to it in a way that I'll be able to sort by start date/time in the admin side, and be able to sort by them when I query the posts as well.
I thought I might be able to do this with the ACF date/time field, but it's just not playing ball. Any ideas?
If i'm understanding correctly, you want to sort your results by a meta value right? that meta value being the date/time field. In your loop, you may want to try out something along the lines of:
$your_custom_query = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_time_meta_key_here',
'compare' => 'EXISTS',
)
),
'orderby' => 'date_clause'
));
See the original wordpress documentation here for more info: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

Woocommerce REST API Update Product Categories

I'm using Wordpress 4.9.5 with Woocommerce 3.3.5. I am using the WooCommerce REST API PHP Client Library to update products on the website when they are changed in a separate product management system. That library is using v2 of the REST API.
Using the following code I am successfully updating the basic product data (title, description, sku, price etc) but I can't get the categories to update from Uncategorized. The categories are also not set when using similar code to create a product if it doesn't already exist on the site.
$client = new WC_API_Client( $domain, $consumerKey, $consumerSecret, $options );
$client->products->update( $id, array(
'sku' => $product->sku,
'title' => $product->title,
'type' => $product->type,
'status' => $product->status,
'regular_price' => $product->regular_price,
'description' => $product->description,
'categories' => array(
array(
'id' => 343
),
array(
'id' => 347
)
)
));
As I say, the other fields update as expected. I have confirmed that categories with IDs 343 and 347 definitely exist so I assume I must have a problem with the syntax. As the other fields update the authentication is definitely working.
I have read the official Woocommerce API documentation and based my code on this tutorial. Based on both of those, I'm not sure what I have done wrong.
Thanks for any help or advice.
I resolved this in the end. It was on oversight on my part.
The client library I was using was connecting to what the Woocommerce documentation calls the 'Legacy v2' version rather than the 'v2' version of the API. Categories, image alt tags, meta data etc are not supported in the legacy versions.
I switched from the using library to connecting directly to the 'v2' version using https://sitename/wp-json/wc/v2/endpoint and all is now well.

Wordpress filters

I used wordpress to make a website with some custom types. The problem is that i need to implement a filter for these types and I have some questions about the plugins that i'm using for this project:
For to make custom types and views I'm using Types and Views plugins for wordpress.
I made a new custom type with some fields (title, price, some selectors, ...) and I need to filter all the information ordering the results following 2 criterias. I don't know if is it possible to do so using the Views/Types plugin.
I also need to filter the fields of the filter. For example, if i have 2 fields one called Countries and other called Cities, if i select Spain in countries I should see only spanish cities in the second field.
Any idea if i can do that using Types and Views plugins? is there any other plugin?
Thanks in advance!
Any good plugin will save the custom fields as post-meta. You need to find out what the key is for the field you are trying to use as a filter. Once you have that you can use a simple meta query to get the posts you need:
$args = array(
'post_type' => 'post', // The post type
'posts_per_page' => -1, // Get all posts
'orderby' => 'rand', // Random order, also can use 'post_date' or 'post_title'
'order' => 'ASC', // Direction of sort
'meta_key' => 'my_meta_key', // Whatever your key is
'meta_query' => array(
array(
'key' => 'my_meta_key', // Whatever your key is
'value' => 'foo' // What it should be
'compare' => 'IN', // How to compare the value to the stored value
)
)
);
$query = new WP_Query($args); // The query
Here is more on the Wordpress codex.

Change wordpress to display search results in chronological order

This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.
Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page

WordPress get user by meta data

How can I retrieve all users registered in my WordPress blog having a particular meta data?
For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_id as 2 , then how can I do this?
Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.
Use the function
get_users($args)
(WP Documentation)
The function takes an array of parameters, in your case you need
get_users(array(
'meta_key' => 'parent_id',
'meta_value' => '42'
))
Simple way how to get one user by his metadata is:
$user = reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1
)
)
);
Here is how you can get users based on a custom role and multiple metadata keys,
$available_drivers = get_users(
array(
'role' => 'driver',
'meta_query' => array(
array(
'key' => 'approved',
'value' => true,
'compare' => '=='
),
array(
'key' => 'available',
'value' => true,
'compare' => '=='
)
)
)
);
Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.
If you have additional metadata to test, add another array element to the meta_query array.
Meanwhile checkout my open source at github.com/patrickingle
Here is the codex page from Wordpress detailing how to use the get_users($arg); function.
It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.
Additionally here is a link to a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.

Resources