MichelH

Widget to show inactive WCFM vendors

Apr 5th, 2023 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.87 KB | Source Code | 0 0
  1. //This code, when inserted into a plugin/functions.php will create a widget on the WP dashboard that will show you vendors with NO products added to the shop.
  2.  
  3.  
  4. function custom_wcfm_get_vendor_store_name_by_vendor( $vendor_id ) {
  5.         global $WCFM, $wpdb, $WCMp;
  6.    
  7.     $vendor_store = '';
  8.    
  9.     if( !$vendor_id ) return $vendor_store;
  10.     $vendor_id = absint( $vendor_id );
  11.    
  12.     $marketplece = wcfm_is_marketplace();
  13.     if( $marketplece == 'wcvendors' ) {
  14.         $shop_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
  15.             if( $shop_name ) $vendor_store = $shop_name;
  16.         } elseif( $marketplece == 'wcmarketplace' ) {
  17.             $vendor = get_wcmp_vendor( $vendor_id );
  18.             if( $vendor ) {
  19.                 $shop_name = get_user_meta( $vendor_id , '_vendor_page_title', true);
  20.                 $store_name = get_user_meta( $vendor_id, 'store_name', true );
  21.                 if( $shop_name ) { $vendor_store = $shop_name; }
  22.             }
  23.         } elseif( $marketplece == 'wcpvendors' ) {
  24.             $vendor_data = get_term( $vendor_id, WC_PRODUCT_VENDORS_TAXONOMY );
  25.             $shop_name = $vendor_data->name;
  26.             if( $shop_name ) { $vendor_store = $shop_name; }
  27.         } elseif( $marketplece == 'dokan' ) {
  28.             $vendor_data = get_user_meta( $vendor_id, 'dokan_profile_settings', true );
  29.             $shop_name     = isset( $vendor_data['store_name'] ) ? esc_attr( $vendor_data['store_name'] ) : '';
  30.             $vendor_user   = get_user_by( 'id', $vendor_id );
  31.             if(  empty( $shop_name ) && $vendor_user ) {
  32.                 $shop_name     = $vendor_user->display_name;
  33.             }
  34.             if( $shop_name ) { $vendor_store = $shop_name; }
  35.         } elseif( $marketplece == 'wcfmmarketplace' ) {
  36.             $shop_name     = get_user_meta( $vendor_id, 'store_name', true );
  37.             $vendor_user   = get_user_by( 'id', $vendor_id );
  38.             if(  empty( $shop_name ) && $vendor_user ) {
  39.                 $shop_name     = $vendor_user->display_name;
  40.             }
  41.             if( $shop_name ) { $vendor_store = $shop_name; }
  42.         }
  43.        
  44.         return $vendor_store;
  45.     }
  46.  
  47.  function custom_wcfm_get_vendor_list( $all = false, $offset = '', $number = '', $search = '', $allow_vendors_list = '', $is_disabled_vendors = true, $vendor_search_data = array() ) {
  48.     global $WCFM;
  49.    
  50.     $is_marketplace = wcfm_is_marketplace();
  51.     $vendor_arr = array();
  52.         if( $is_marketplace ) {
  53.             if( !wcfm_is_vendor() || apply_filters( 'wcfm_is_allow_get_all_vendor_list_by_force', true ) ) {
  54.                 if( $all ) {
  55.                     $vendor_arr = array( 0 => __('All', 'wc-frontend-manager' ) );
  56.                 } else {
  57.                     $vendor_arr = array( '' => __('Choose Vendor ...', 'wc-frontend-manager' ) );
  58.                 }
  59.                 $wcfm_allow_vendors_list = apply_filters( 'wcfm_allow_vendors_list', $allow_vendors_list, $is_marketplace );
  60.                 if( $is_marketplace == 'wcpvendors' ) {
  61.                     $args = array(
  62.                         'hide_empty'   => false,
  63.                         'hierarchical' => false,
  64.                     );
  65.                     if( $number ) {
  66.                         $args['offset'] = $offset;
  67.                         $args['number'] = $number;
  68.                     }
  69.                     if( $search ) {
  70.                         $args['search'] = $search;
  71.                     }
  72.                     if( $wcfm_allow_vendors_list ) {
  73.                         $args['include']  = $wcfm_allow_vendors_list;
  74.                     }
  75.                     $vendors = get_terms( WC_PRODUCT_VENDORS_TAXONOMY, $args );
  76.                    
  77.                     if( !empty( $vendors ) ) {
  78.                         foreach ( $vendors as $vendor ) {
  79.                             $vendor_arr[$vendor->term_id] = esc_html( $vendor->name );
  80.                         }
  81.                     }
  82.                 } else {
  83.                     $vendor_user_roles = apply_filters( 'wcfm_allwoed_vendor_user_roles', array( 'dc_vendor', 'vendor', 'seller', 'wcfm_vendor' ) );
  84.                     if( $is_disabled_vendors ) {
  85.                         $vendor_user_roles = apply_filters( 'wcfm_allwoed_vendor_user_roles', array( 'dc_vendor', 'vendor', 'seller', 'wcfm_vendor', 'disable_vendor' ) );
  86.                     }
  87.                     $args = array(
  88.                         'role__in'     => $vendor_user_roles,
  89.                         'orderby'      => 'login',
  90.                         'order'        => 'ASC',
  91.                         'include'      => $wcfm_allow_vendors_list,
  92.                         'count_total'  => false,
  93.                         'fields'       => array( 'ID', 'display_name', 'user_login' )
  94.                      );
  95.                     if( $number ) {
  96.                         $args['offset'] = $offset;
  97.                         $args['number'] = $number;
  98.                     }
  99.                     if( $search ) {
  100.                         //$args['search'] = $search;
  101.                         $args['meta_query'] = array(
  102.                                                                              'relation' => 'OR',
  103.                                                                                 array(
  104.                                                                                         'key'     => 'first_name',
  105.                                                                                         'value'   => $search,
  106.                                                                                         'compare' => 'LIKE'
  107.                                                                                 ),
  108.                                                                                 array(
  109.                                                                                         'key'     => 'last_name',
  110.                                                                                         'value'   => $search,
  111.                                                                                         'compare' => 'LIKE'
  112.                                                                                 ),
  113.                                                                                 array(
  114.                                                                                         'key'     => 'nickname',
  115.                                                                                         'value'   => $search,
  116.                                                                                         'compare' => 'LIKE'
  117.                                                                                 ),
  118.                                                                                 array(
  119.                                                                                         'key'     => 'pv_shop_name',
  120.                                                                                         'value'   => $search,
  121.                                                                                         'compare' => 'LIKE'
  122.                                                                                 ),
  123.                                                                                 array(
  124.                                                                                         'key'     => '_vendor_page_title',
  125.                                                                                         'value'   => $search,
  126.                                                                                         'compare' => 'LIKE'
  127.                                                                                 ),
  128.                                                                                 array(
  129.                                                                                         'key'     => 'dokan_store_name',
  130.                                                                                         'value'   => $search,
  131.                                                                                         'compare' => 'LIKE'
  132.                                                                                 ),
  133.                                                                                 array(
  134.                                                                                         'key'     => 'wcfmmp_store_name',
  135.                                                                                         'value'   => $search,
  136.                                                                                         'compare' => 'LIKE'
  137.                                                                                 ),
  138.                                                                                 array(
  139.                                                                                         'key'     => 'store_name',
  140.                                                                                         'value'   => $search,
  141.                                                                                         'compare' => 'LIKE'
  142.                                                                                 ),
  143.                                                                         );
  144.                     }
  145.                    
  146.                     if( !empty( $vendor_search_data ) && is_array( $vendor_search_data ) ) {
  147.                         foreach( $vendor_search_data as $search_key => $search_value ) {
  148.                             if( !$search_value ) continue;
  149.                             if( in_array( $search_key, apply_filters( 'wcfmmp_vendor_list_exclude_search_keys', array( 'v', 'search_term', 'wcfmmp_store_search', 'wcfmmp_store_category', 'wcfmmp_radius_addr', 'wcfmmp_radius_lat', 'wcfmmp_radius_lng', 'wcfmmp_radius_range', 'pagination_base', 'wcfm_paged', 'paged', 'per_row', 'per_page', 'excludes', 'orderby', 'has_product', 'theme', 'nonce', 'lang' ) ) ) ) continue;
  150.                             if( $search ) $args['meta_query']['relation'] = 'AND';
  151.                             $args['meta_query'][] = array(
  152.                                                                                          'relation' => 'OR',
  153.                                                                                          array(
  154.                                                                                                 'key'     => str_replace( 'wcfmmp_store_', '', $search_key ),
  155.                                                                                                 'value'   => $search_value,
  156.                                                                                                 'compare' => 'LIKE'
  157.                                                                                         ),
  158.                                                                                         array(
  159.                                                                                                 'key'     => str_replace( 'wcfmmp_store_', '_wcfm_', $search_key ),
  160.                                                                                                 'value'   => $search_value,
  161.                                                                                                 'compare' => 'LIKE'
  162.                                                                                         )
  163.                                                                                     );
  164.                         }
  165.                     }
  166.                    
  167.                     $all_users = get_users( $args );
  168.                     if( !empty( $all_users ) ) {
  169.                         foreach( $all_users as $all_user ) {
  170.                             $user_info = get_userdata($all_user->ID);
  171.                             $user_email = $user_info->user_email;
  172.  
  173.                             $vendor_arr[$all_user->ID] = [custom_wcfm_get_vendor_store_name_by_vendor( $all_user->ID ),$user_email];
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.     // var_dump($vendor_arr);
  180.         return $vendor_arr;
  181.     }
  182. // Add Custom Dashboard Widget
  183. function add_wcfm_vendors_no_products_dashboard_widgets() {
  184.  
  185.     wp_add_dashboard_widget(
  186.         'wcfm_vendors_no_products',
  187.         'WCFM Vendors with no products',
  188.         'wcfm_vendors_no_products_dashboard_widgets'
  189.     );
  190. }
  191. add_action( 'wp_dashboard_setup', 'add_wcfm_vendors_no_products_dashboard_widgets' );
  192.  
  193.  
  194. function wcfm_vendors_no_products_dashboard_widgets() {
  195.     global $WCFM;
  196.     $wcfm_vendors_array = custom_wcfm_get_vendor_list( true, $offset, $length, '', $search_vendor, false, $vendor_search_data );
  197.     //var_dump($wcfm_vendors_array);
  198.     if(!empty($wcfm_vendors_array)) {
  199.         echo '<table id="wcfm_vendors"><thead><tr><td>Name</td><td>Email</td></tr></thead><tbody>';
  200.         $index = 0;
  201.         $wcfm_vendors_json_arr = array();
  202.         foreach($wcfm_vendors_array as $wcfm_vendors_id => $wcfm_vendors_name ) {
  203.             if($index != 0 ){
  204.             $total_products = wcfm_get_user_posts_count( $wcfm_vendors_id, 'product', apply_filters( 'wcfm_limit_check_status', 'any' ) );
  205.             if($total_products == 0 ) {
  206.        
  207.  
  208.                 echo '<tr><td>'.$wcfm_vendors_name[0].'</td><td>'.$wcfm_vendors_name[1].'</td></tr>';
  209.             }
  210.         }
  211.         $index++;
  212.            
  213.         }
  214.         echo '</tbody></table>';
  215.     }
  216. ?>
  217. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.24/b-1.7.0/b-html5-1.7.0/r-2.2.7/datatables.min.css"/>
  218. <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.24/b-1.7.0/b-html5-1.7.0/r-2.2.7/datatables.min.js"></script>
  219.  
  220.  
  221. <script>
  222. jQuery(document).ready(function() {
  223.     jQuery('#wcfm_vendors').DataTable({
  224.         dom: 'Bfrtip',
  225.         buttons: [
  226.             'copy', 'csv'
  227.         ]
  228.     });
  229. } );
  230. </script>
  231. <?php
  232. }
Advertisement
Add Comment
Please, Sign In to add comment