Skip to content

Commit

Permalink
fix: the order list as a vendor on the back-end is not showing proper…
Browse files Browse the repository at this point in the history
…ly is fixed #430 (#431)

* fix: the order list as a vendor on the back-end is not showing properly is fixed #430

* refactor: change function name

* refactor: shop_order table in admin backend

* refactor: shop_order in admin backend
  • Loading branch information
saimonh3 authored and sabbir1991 committed Nov 26, 2018
1 parent a4c98af commit 33a281d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
12 changes: 11 additions & 1 deletion includes/admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ function dokan_shop_order_custom_columns( $col ) {
$the_order = new WC_Order( $post->ID );
}

if ( ! current_user_can( 'manage_woocommerce' ) ) {
return $col;
}

switch ($col) {
case 'order_number':
if ($post->post_parent !== 0) {
Expand Down Expand Up @@ -130,7 +134,13 @@ function dokan_shop_order_custom_columns( $col ) {
function dokan_admin_shop_order_row_classes( $classes, $post_id ) {
global $post;

if ( is_search() ) {
if ( is_search() || ! current_user_can( 'manage_woocommerce' ) ) {
return $classes;
}

$vendor_id = isset( $_GET['vendor_id'] ) ? $_GET['vendor_id'] : '';

if ( $vendor_id ) {
return $classes;
}

Expand Down
92 changes: 86 additions & 6 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1570,29 +1570,109 @@ function dokan_disable_admin_bar( $show_admin_bar ) {
add_filter( 'show_admin_bar', 'dokan_disable_admin_bar' );

/**
* Filter the orders, products and booking products of current user
* Filter products of current user
*
* @param object $query
* @since 2.7.3
* @return object $query
*/
function dokan_filter_orders_for_current_vendor( $query ) {
function dokan_filter_product_for_current_vendor( $query ) {
if ( current_user_can( 'manage_woocommerce' ) ) {
return;
return $query;
}

if ( ! isset( $query->query_vars['post_type'] ) ) {
return;
return $query;
}

if ( is_admin() && $query->is_main_query() && ( $query->query_vars['post_type'] == 'shop_order' || $query->query_vars['post_type'] == 'product' || $query->query_vars['post_type'] == 'wc_booking' ) ) {
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product' ) {
$query->set( 'author', get_current_user_id() );
}

return $query;
}

add_action( 'pre_get_posts', 'dokan_filter_orders_for_current_vendor' );
add_filter( 'pre_get_posts', 'dokan_filter_product_for_current_vendor' );

/**
* Filter orders of current user
*
* @param object $args
* @param object $query
* @since 2.9.4
* @return object $args
*/
function dokan_filter_orders_for_current_vendor( $args, $query ) {
global $wpdb;

if ( current_user_can( 'manage_woocommerce' ) ) {
if ( ! empty( $_GET['vendor_id'] ) ) {
$vendor_id = $_GET['vendor_id'];
$args['join'] .= " LEFT JOIN {$wpdb->prefix}dokan_orders as do ON $wpdb->posts.ID=do.order_id";
$args['where'] .= " AND do.seller_id=$vendor_id";
}

return $args;
}

if ( ! isset( $query->query_vars['post_type'] ) ) {
return $args;
}

$vendor_id = get_current_user_id();

if ( is_admin() && $query->is_main_query() && ( $query->query_vars['post_type'] == 'shop_order' || $query->query_vars['post_type'] == 'wc_booking' ) ) {
$args['join'] .= " LEFT JOIN {$wpdb->prefix}dokan_orders as do ON $wpdb->posts.ID=do.order_id";
$args['where'] .= " AND do.seller_id=$vendor_id";
}

return $args;
}

add_filter( 'posts_clauses', 'dokan_filter_orders_for_current_vendor', 12, 2 );

/**
* Dokan map meta cpas for vendors
*
* @param array $caps
* @param string $cap
* @param int $user_id
* @param array $args
*
* @return array
*/
function dokan_map_meta_caps( $caps, $cap, $user_id, $args ) {
global $post;

if ( ! is_admin() ) {
return $caps;
}

$post_id = ! empty( $args[0] ) ? $args[0] : 0;

if ( $cap === 'edit_post' || $cap === 'edit_others_shop_orders' ) {
$post_id = ! empty( $args[0] ) ? $args[0] : 0;

if ( empty( $post_id ) ) {
if ( empty( $post->ID ) ) {
return $caps;
}

$post_id = $post->ID;
}

$vendor_id = get_post_meta( $post_id, '_dokan_vendor_id', true );
$current_user_id = get_current_user_id();

if ( absint( $vendor_id ) === absint( $current_user_id ) ) {
return array( 'edit_shop_orders' );
}
}

return $caps;
}

add_filter( 'map_meta_cap', 'dokan_map_meta_caps', 12, 4 );

/**
* Remove sellerdiv metabox when a seller can access the backend
Expand Down

0 comments on commit 33a281d

Please sign in to comment.