From 33a281d9532ba49a12b96769b13287fe1a2abd6a Mon Sep 17 00:00:00 2001 From: Saimon Sajjad Date: Mon, 26 Nov 2018 17:48:34 +0600 Subject: [PATCH] fix: the order list as a vendor on the back-end is not showing properly 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 --- includes/admin-functions.php | 12 ++++- includes/functions.php | 92 +++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/includes/admin-functions.php b/includes/admin-functions.php index cc5ba57484..659b94a8b3 100755 --- a/includes/admin-functions.php +++ b/includes/admin-functions.php @@ -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) { @@ -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; } diff --git a/includes/functions.php b/includes/functions.php index 2de760275c..28451ef1a7 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -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