I would like to display all customer orders under my account queries to certain role. Tried using the following but only sorting out orders under the email.
How can I do that?
add_filter( 'woocommerce_my_account_my_orders_query',
'custom_my_orders_query' );
function custom_my_orders_query($args){
$user_id = get_current_user_id();
$args['post_status'] = array('wc-processing');
$args['meta_key'] = 'assignee';
$args['meta_value'] = $user_id;
return $args;
}
Related
I need to display WooCommerce products in a specific order by listing the SKU's/ID's. Either using a shortcode or a custom loop.
For example I want to show the following products in this exact order:
[products ids="1,3,2"]
I cannot use menu order because the order will be different depending on page:
Page 1:
[products ids="1,3,2"]
Page 2:
[products ids="3,2,1"]
I have tried the below suggestion, but it doesn't seem to change anything:
[products ids="1,3,2" orderby="post__in"]
The products still come through in the default order.
You can embed the woocommerce default product by extending like that -
add_shortcode( 'custom-products-ordering', 'custom_products_ordering_callback' );
function custom_products_ordering_callback( $attr ) {
global $wpdb;
$orderby = $attr['orderby'];
$columns = $attr['columns'];
$limit = $attr['limit'];
$paginate = $attr['paginate'];
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$query = $wpdb->get_results("SELECT post.`ID` FROM `wp_posts` AS post, `wp_postmeta` AS postmeta WHERE post.`ID` = postmeta.`post_id` AND post.`post_type` = 'shop_order' AND postmeta.`meta_key` = '_customer_user' AND postmeta.`meta_value` = '".$current_user_id."'");
foreach( $query as $key => $value ){
$filter = $value->ID;
}
$product_ids_str = implode( ",", $filter );
return do_shortcode("[products ids='".$product_ids_str."' columns='".$columns."' limit='".$limit."' paginate='".$paginate."' orderby = '".$orderby."']");
}
How you use is -
[custom-products-ordering column="4" limit="12" paginate="true" orderby="post__in"]
You can extend or sort anything you want by modifying the query.
Our scenario is:
The guest user enters the site and places one or more orders without the need to register. And after a while, he decides to register on the site.
Now how to link guest orders to customer account after registeration?
I use the following code, but this code only works for users who have already registered but did not log in at the time of purchase. Any advice?
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
You can use the woocommerce_created_customer action hook and the wc_update_new_customer_past_orders() function
So you get:
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
// Link past orders to this newly created customer
wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
Any ideas would be much appreciated.
I am trying to reorder my woocommerce products by most viewed using the post-views-counter plugin.
Followed these examples which did not seem to work Sort products by most viewed
function my_view_filter($query){
if ($query->is_main_query() && ( $query->is_home() || $query- >is_archive()
)
) {
$query->set('post_type', 'product');
$query->set('suppress_filters', false);
$query->set('orderby', 'post_views');
$query->set('order', 'asc');
$query->set('fields', '');
}
}
add_action( 'pre_get_posts', 'my_view_filter' );
First of all, the plugin you're trying to use is outdated. Secondly, it seems to create a table to hold the views and this would require changing the actual MySQL query to retrieve posts ordered by date which is a lot of work looking what you need to do.
You could simply just save views on visit to post meta and use that to order products on the catalog like this:
/**
* Setting post count on each visit
*
*/
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
function prefix_save_product_views( ) {
$product_id = get_the_ID();
$increment = 1;
$current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
}
/**
* Change the display order based on visit count only in Catalog
*
*/
add_filter('woocommerce_get_catalog_ordering_args', 'prefix_woocommerce_catalog_orderby');
function prefix_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'product_visit_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
You'd run into multiple issues using pre_get_posts
// Remove product category/tag meta from its original position
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
// Add product meta in new position
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 5 );
Hopefully some legend can point me in the right direction.
I am wanting to group users who use certain coupon codes in woocommerce when they purchase. I realise there are plugins such as woocommerce groups which allow you to restrict coupons to user groups but I need this the other way.
Eg: someone uses a coupon code "cricket" when they purchase and this will then add the user to the group "cricket-users".
Thanks V much!
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'cricket' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id()
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
}
}
}
i need to Delete wp_terms -> name field when a user is removed .
my requirement is admin needs to assign posts to specific users, that is admin need to add a post to user1 ,but user2 should not see that.for this i created a custom post and add taxonomy for that.And the terms are users Usernames .so i need to list the usernames of the users as terms in the taxonomy.when a new user is registered his username should updated in the wp_terms table also,so i will get the usernames as terms. this is working well ,now i need to delete the term from taxonomy when the user is removed.
i stucked here .please suggest some solution for this
what i have done is
add_action( 'delete_user', 'yg_user_delete', 10, 1 );
function yg_user_delete( $user_id ) {
$user_info = get_userdata($user_id); $user_name = $user_info->user_login;
print_r($user_info);
wp_delete_term( $user_name, 'user1', array() );
}
There are a few issues with your code.
The main issue is wp_delete_term requires a term_id not a term_name.
You can use wp_get_object_terms to get the term_id for the user.
I haven't tested this code but it should point you in the right direction.
function delete_user_terms( $user_id ) {
$taxonomy = 'user1'; // Change this.
$terms = wp_get_object_terms( $user_id, $taxonomy ); // Get the terms for this user.
if ( is_wp_error( $terms ) ) {
return; // Taxonomy does not exist.
}
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy ); // This requires Term ID. Not term name.
}
}
add_action( 'delete_user', 'delete_user_terms', 10, 1 );
i got the solution
add_action( 'delete_user', 'yg_user_delete', 10, 1 );
function yg_user_delete( $user_id ) {
$user_info = get_userdata($user_id);
$user_name = $user_info->user_login;
wp_delete_term( get_term_by('name', $user_name, 'user1')->term_id, 'user1', array() );
}