Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ontiuk/e28fa07d3999d9b8f3d4 to your computer and use it in GitHub Desktop.

Select an option

Save ontiuk/e28fa07d3999d9b8f3d4 to your computer and use it in GitHub Desktop.
Woocommerce and VCVendors integration: Bookings Post List
/**
* Sets filtering of bookings per vendor.
*/
// Bookings Post Listing. Restrict to current user as author
function riv_vendor_bookings_where( $where, $q ) {
global $current_user, $wpdb;
// test the page
$ok = ( is_admin() && $q->is_main_query() ) ? TRUE : FALSE;
if ( !$ok ) { return $where; }
// test screen
$screen = get_current_screen();
if ( $screen->id !== 'edit-wc_booking' ) { return $where; }
// get the user
if ( !$current_user instanceof WP_User ) {
$current_user = wp_get_current_user();
if ( !$current_user instanceof WP_User ) { return $where; }
}
// vendor only
if ( class_exists( 'WCV_Vendors' ) && WCV_Vendors::is_vendor( $current_user->ID ) ) {
// get vendor shop orders
$q = 'SELECT ' . $wpdb->posts . '.ID FROM ' . $wpdb->posts . ' LEFT JOIN ' . $wpdb->prefix . 'pv_commission pv ON ' . $wpdb->posts . '.ID = pv.order_id WHERE ' . $wpdb->posts . '.post_type = "shop_order" AND pv.vendor_id=%d ORDER BY '. $wpdb->posts .'.ID DESC';
$qs = $wpdb->prepare( $q, $current_user->ID );
$orders = $wpdb->get_col( $qs );
$orders_in = join( ',', $orders );
// filter bookings
$where .= ( empty( $where ) ) ? ' '.$wpdb->posts.'.post_parent IN ('.$orders_in.') ' :
' AND '.$wpdb->posts.'.post_parent IN ('.$orders_in.') ';
}
return $where;
}
add_filter( 'posts_where', 'riv_vendor_bookings_where', 10, 2 );
// set post list - post status views
function riv_vendor_bookings_views( $views ) {
global $current_user, $wpdb;
// get the user
if ( !$current_user instanceof WP_User ) {
$current_user = wp_get_current_user();
if ( !$current_user instanceof WP_User ) { return $views; }
}
$post_type = get_query_var( 'post_type', 'wc_booking' );
$post_status = get_query_var( 'post_status', '' );
$views = array(
'all' => __('All'),
'paid' => __('Paid & Confirmed'),
'confirmed' => __('Confirmed'),
'unpaid' => __('Un-Paid'),
'cancelled' => __('Cancelled')
);
// get vendor shop orders
$q = 'SELECT ' . $wpdb->posts . '.ID FROM ' . $wpdb->posts . ' LEFT JOIN ' . $wpdb->prefix . 'pv_commission pv ON ' . $wpdb->posts . '.ID = pv.order_id WHERE ' . $wpdb->posts . '.post_type = "shop_order" AND pv.vendor_id=%d ORDER BY '. $wpdb->posts .'.ID DESC';
$qs = $wpdb->prepare( $q, $current_user->ID );
$orders = $wpdb->get_col( $qs );
// orders?
if ( empty( $orders ) ) {
foreach ( $views as $k=>$v ) {
$class = ( $post_status == $k) ? ' class="current"' : '';
$admin_url = ( $k === 'all' ) ? admin_url('edit.php?post_type='.$post_type) :
admin_url('edit.php?post_type='.$post_type.'&post_status='.$k);
$views[$k] = sprintf(
'<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
$admin_url,
$class,
0,
$v
);
}
} else {
// new query
$query = array(
'post_type' => $post_type,
'post_parent__in' => $orders
);
foreach ( $views as $k=>$v ) {
$class = ( $post_status == $k) ? ' class="current"' : '';
$admin_url = ( $k === 'all' ) ? admin_url('edit.php?post_type='.$post_type) :
admin_url('edit.php?post_type='.$post_type.'&post_status='.$k);
if ( $k == 'all') {
$query['all_posts'] = 1;
} else {
$query['post_status'] = $k;
}
$result = new WP_Query( $query );
$views[$k] = sprintf(
'<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
$admin_url,
$class,
$result->found_posts,
$v
);
}
}
return $views;
}
function riv_vendor_bookings_statuses( $query ) {
global $current_user;
// test the page
$ok = ( is_admin() && $query->is_main_query() ) ? TRUE : FALSE;
if ( !$ok ) { return $query; }
// get the user
if ( !$current_user instanceof WP_User ) {
$current_user = wp_get_current_user();
if ( !$current_user instanceof WP_User ) { return $query; }
}
// vendor only
if ( class_exists( 'WCV_Vendors' ) && WCV_Vendors::is_vendor( $current_user->ID ) ) {
add_filter('views_edit-wc_booking', 'riv_vendor_bookings_views' );
}
}
add_action( 'pre_get_posts', 'riv_vendor_bookings_statuses' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment