/**
  * widget function.
  *
  * @see WP_Widget
  * @access public
  * @param array $args
  * @param array $instance
  * @return void
  */
 function widget($args, $instance)
 {
     global $WCMp, $woocommerce;
     extract($args, EXTR_SKIP);
     $vendor_id = false;
     $vendor = false;
     $show_widget = true;
     if ($show_widget) {
         $vendors = get_wcmp_vendors();
         if ($vendors) {
             // Set up widget title
             if ($instance['title']) {
                 $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
             } else {
                 $title = false;
             }
             // Before widget (defined by themes)
             echo $before_widget;
             // Display the widget title if one was input (before and after defined by themes).
             if ($title) {
                 echo $before_title . $title . $after_title;
             }
             // Widget content
             // Action for plugins/themes to hook onto
             do_action($this->widget_cssclass . '_top');
             $WCMp->template->get_template('widget/vendor-list.php', array('vendors' => $vendors));
             // Action for plugins/themes to hook onto
             do_action($this->widget_cssclass . '_bottom');
             // After widget (defined by themes).
             echo $after_widget;
         }
     }
 }
Example #2
0
 /**
  * On activation, include the installer and run it.
  *
  * @access public
  * @return void
  */
 function wcmp_plugins_loaded()
 {
     global $WCMp, $wpdb;
     //delete_option('dc_product_vendor_plugin_db_version');
     $previous_plugin_version = get_option('dc_product_vendor_plugin_db_version');
     if (!$previous_plugin_version || $previous_plugin_version < $WCMp->version) {
         $prev_general = get_option('dc_general_settings_name');
         $prev_product = get_option('dc_product_settings_name');
         $prev_capability = get_option('dc_capabilities_settings_name');
         $prev_pages = get_option('dc_pages_settings_name');
         $prev_payment = get_option('dc_payment_settings_name');
         $new_general = $new_product = $new_capability = $new_pages = $new_payment = $new_frontend = array();
         $new_payment = $prev_payment;
         if (!empty($prev_general)) {
             if (isset($prev_general['enable_registration'])) {
                 $new_general['enable_registration'] = 'Enable';
             }
             if (isset($prev_general['approve_vendor_manually'])) {
                 $new_general['approve_vendor_manually'] = 'Enable';
             }
             if (isset($prev_general['notify_configure_vendor_store'])) {
                 $new_general['notify_configure_vendor_store'] = $prev_general['notify_configure_vendor_store'];
             }
             if (isset($prev_general['default_commission'])) {
                 $new_payment['default_commission'] = $prev_general['default_commission'];
             }
             if (isset($prev_general['commission_type'])) {
                 $new_payment['commission_type'] = $prev_general['commission_type'];
             }
             if (isset($prev_general['commission_include_coupon'])) {
                 $new_payment['commission_include_coupon'] = $prev_general['commission_include_coupon'];
             }
             if (isset($prev_general['sold_by_catalog'])) {
                 $new_frontend['sold_by_catalog'] = $prev_general['sold_by_catalog'];
             }
             if (isset($prev_general['catalog_colorpicker'])) {
                 $new_frontend['catalog_colorpicker'] = $prev_general['catalog_colorpicker'];
             }
             if (isset($prev_general['catalog_hover_colorpicker'])) {
                 $new_frontend['catalog_hover_colorpicker'] = $prev_general['catalog_hover_colorpicker'];
             }
             if (isset($prev_general['sold_by_cart_and_checkout'])) {
                 $new_frontend['sold_by_cart_and_checkout'] = $prev_general['sold_by_cart_and_checkout'];
             }
             if (isset($prev_general['sold_by_text'])) {
                 $new_frontend['sold_by_text'] = $prev_general['sold_by_text'];
             }
             if (isset($prev_general['block_vendor_desc'])) {
                 $new_frontend['block_vendor_desc'] = $prev_general['block_vendor_desc'];
             }
         }
         if (!empty($prev_capability)) {
             $new_capability = $prev_capability;
             if (isset($new_capability['give_tax'])) {
                 $new_payment['give_tax'] = $new_capability['give_tax'];
                 unset($new_capability['give_tax']);
             }
             if (isset($new_capability['give_shipping'])) {
                 $new_payment['give_shipping'] = $new_capability['give_shipping'];
                 unset($new_capability['give_shipping']);
             }
         }
         if (!empty($prev_product)) {
             update_option('wcmp_product_settings_name', $prev_product);
         }
         if (!empty($prev_pages)) {
             update_option('wcmp_pages_settings_name', $prev_pages);
         }
         if (!empty($new_general)) {
             update_option('wcmp_general_settings_name', $new_general);
         }
         if (!empty($new_capability)) {
             update_option('wcmp_capabilities_settings_name', $new_capability);
         }
         if (!empty($new_payment)) {
             update_option('wcmp_payment_settings_name', $new_payment);
         }
         if (!empty($new_frontend)) {
             update_option('wcmp_frontend_settings_name', $new_frontend);
         }
         delete_option('dc_general_settings_name');
         delete_option('dc_product_settings_name');
         delete_option('dc_capabilities_settings_name');
         delete_option('dc_payment_settings_name');
         delete_option('dc_pages_settings_name');
         $vendors = get_wcmp_vendors();
         if (!empty($vendors)) {
             foreach ($vendors as $vendor) {
                 $vendorusers = new WP_User($vendor->id);
                 $vendorusers->remove_cap('manage_woocommerce');
                 $vendor_products = $vendor->get_products();
                 if (!empty($vendor_products)) {
                     foreach ($vendor_products as $vendor_product) {
                         wp_update_post(array('ID' => $vendor_product->ID, 'post_author' => $vendor->id));
                         $product_obj = wc_get_product($vendor_product->ID);
                         if ($product_obj->is_type('variable')) {
                             $childrens = $product_obj->get_children();
                             foreach ($childrens as $child_id) {
                                 wp_update_post(array('ID' => $child_id, 'post_author' => $vendor->id));
                             }
                         }
                     }
                 }
             }
         }
         $vendor_role = get_role('dc_vendor');
         $vendor_role->remove_cap('manage_woocommerce');
         $wcmp_pages = get_option('wcmp_pages_settings_name');
         $page_slug = 'wcmp_withdrawal_request';
         $page_found = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_found) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Withdrawal Request Status', $WCMp->text_domain), 'post_content' => '[transaction_thankyou]', 'comment_status' => 'closed');
             $transaction_withdrawal_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_transaction_widthdrawal_page_id', $transaction_withdrawal_page_id);
             $wcmp_pages['vendor_transaction_thankyou'] = $transaction_withdrawal_page_id;
         }
         $page_slug = 'wcmp_transaction_details';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Transaction Details', $WCMp->text_domain), 'post_content' => '[transaction_details]', 'comment_status' => 'closed');
             $transaction_details_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_transaction_details_page_id', $transaction_details_page_id);
             $wcmp_pages['vendor_transaction_detail'] = $transaction_details_page_id;
         }
         $page_slug = 'wcmp_vendor_policies';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Policies', $WCMp->text_domain), 'post_content' => '[vendor_policies]', 'comment_status' => 'closed');
             $policy_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_policies_page_id', $policy_page_id);
             $wcmp_pages['vendor_policies'] = $policy_page_id;
         }
         $page_slug = 'wcmp_vendor_billing';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Billing', $WCMp->text_domain), 'post_content' => '[vendor_billing]', 'comment_status' => 'closed');
             $vendor_billing_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_billing_page_id', $vendor_billing_page_id);
             $wcmp_pages['vendor_billing'] = $vendor_billing_page_id;
         }
         $page_slug = 'wcmp_vendor_shipping';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Shipping', $WCMp->text_domain), 'post_content' => '[vendor_shipping_settings]', 'comment_status' => 'closed');
             $vendor_shipping_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_shipping_page_id', $vendor_shipping_page_id);
             $wcmp_pages['vendor_shipping'] = $vendor_shipping_page_id;
         }
         $page_slug = 'wcmp_vendor_report';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Report', $WCMp->text_domain), 'post_content' => '[vendor_report]', 'comment_status' => 'closed');
             $vendor_report_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_report_page_id', $vendor_report_page_id);
             $wcmp_pages['vendor_report'] = $vendor_report_page_id;
         }
         $page_slug = 'wcmp_vendor_widthdrawals';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Widthdrawals', $WCMp->text_domain), 'post_content' => '[vendor_widthdrawals]', 'comment_status' => 'closed');
             $vendor_widthdrawals_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_widthdrawals_page_id', $vendor_widthdrawals_page_id);
             $wcmp_pages['vendor_widthdrawals'] = $vendor_widthdrawals_page_id;
         }
         $page_slug = 'wcmp_vendor_university';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '{$page_slug}' LIMIT 1;");
         if (!$page_foundd) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor University', $WCMp->text_domain), 'post_content' => '[vendor_university]', 'comment_status' => 'closed');
             $vendor_university_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_university_page_id', $vendor_university_page_id);
             $wcmp_pages['vendor_university'] = $vendor_university_page_id;
         }
         $page_slug = 'wcmp_vendor_announcements';
         $page_foundd = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = 'wcmp_vendor_messages' LIMIT 1;");
         $page_foundd2 = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = 'wcmp_vendor_announcements' LIMIT 1;");
         if (!$page_foundd && !$page_foundd2) {
             $page_data = array('post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'post_name' => $page_slug, 'post_title' => __('Vendor Announcements', $WCMp->text_domain), 'post_content' => '[vendor_announcements]', 'comment_status' => 'closed');
             $vendor_announcements_page_id = wp_insert_post($page_data);
             update_option('wcmp_product_vendor_announcements_page_id', $vendor_announcements_page_id);
             $wcmp_pages['vendor_announcements'] = $vendor_announcements_page_id;
         }
         if ($page_foundd && !$page_foundd2) {
             wp_update_post(array('ID' => $wcmp_pages['vendor_messages'], 'post_content' => '[vendor_announcements]', 'post_name' => 'vendor_announcements', 'post_title' => 'Vendor Announcements'));
             $wcmp_pages['vendor_announcements'] = $wcmp_pages['vendor_messages'];
             unset($wcmp_pages['vendor_messages']);
         }
         wp_update_post(array('ID' => $wcmp_pages['vendor_dashboard'], 'post_content' => '[vendor_dashboard]'));
         wp_update_post(array('ID' => $wcmp_pages['view_order'], 'post_content' => '[vendor_orders]'));
         update_option('wcmp_pages_settings_name', $wcmp_pages);
         if (!empty($wpdb->charset)) {
             $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
         }
         if (!empty($wpdb->collate)) {
             $charset_collate .= " COLLATE {$wpdb->collate}";
         }
         $migs = array();
         // Create course_purchase table
         $migs[] = "\n\t\t\t\tCREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . "wcmp_vendor_orders` (\n\t\t\t\t`ID` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\t`order_id` bigint(20) NOT NULL,\n\t\t\t\t`commission_id` bigint(20) NOT NULL,\n\t\t\t\t`vendor_id` bigint(20) NOT NULL,\n\t\t\t\t`shipping_status` varchar(255) NOT NULL,\n\t\t\t\t`order_item_id` bigint(20) NOT NULL,\n\t\t\t\t`product_id` bigint(20) NOT NULL,\n\t\t\t\t`commission_amount` varchar(255) NOT NULL,\n\t\t\t\t`shipping` varchar(255) NOT NULL,\n\t\t\t\t`tax` varchar(255) NOT NULL,\n\t\t\t\t`is_trashed` varchar(10) NOT NULL,\t\t\t\t\n\t\t\t\t`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\t\t\t\t\n\t\t\t\tPRIMARY KEY (`ID`),\n\t\t\t\tCONSTRAINT vendor_orders UNIQUE (order_id, vendor_id, commission_id, product_id)\n\t\t\t){$charset_collate};";
         $needed_migration = count($migs);
         for ($i = 0; $i < $needed_migration; $i++) {
             $mig = $migs[$i];
             $wpdb->query($mig);
         }
         $WCMp_Calculate_Commission_obj = new WCMp_Calculate_Commission();
         $vendors = get_wcmp_vendors();
         if (!empty($vendors)) {
             $vendor_orders_array = array();
             foreach ($vendors as $vendor) {
                 $vendor_orders = $vendor->get_orders();
                 if (!empty($vendor_orders)) {
                     foreach ($vendor_orders as $commission_id => $order_id) {
                         $vendor_shipping_array = get_post_meta($order_id, 'dc_pv_shipped', true);
                         $order = new WC_Order($order_id);
                         $commission_array = array();
                         $mark_ship = false;
                         $items = $order->get_items('line_item');
                         foreach ($items as $order_item_id => $item) {
                             $comm_pro_id = $product_id = $order->get_item_meta($order_item_id, '_product_id', true);
                             $variation_id = $order->get_item_meta($order_item_id, '_variation_id', true);
                             if ($variation_id) {
                                 $comm_pro_id = $variation_id;
                             }
                             if ($product_id) {
                                 $product_vendors = get_wcmp_product_vendors($product_id);
                                 if ($product_vendors) {
                                     if (isset($product_vendors->id)) {
                                         if (isset($vendor_shipping_array) && !empty($vendor_shipping_array)) {
                                             if (in_array($product_vendors->id, $vendor_shipping_array)) {
                                                 $mark_ship = true;
                                             } else {
                                                 $mark_ship = 0;
                                             }
                                         } else {
                                             $mark_ship = 0;
                                         }
                                         $item_commission = $WCMp_Calculate_Commission_obj->get_item_commission($comm_pro_id, $comm_pro_id, $item, $order_id, $order_item_id);
                                         $vendor_shipping_costs = $vendor->get_wcmp_vendor_shipping_total($order_id, $item);
                                         $item_shipping = $vendor_shipping_costs['shipping_amount'] + $vendor_shipping_costs['shipping_tax'];
                                         $item_tax = get_metadata('order_item', $order_item_id, '_line_tax', true);
                                         $commission_vendor_term_id = get_post_meta($commission_id, '_commission_vendor', true);
                                         $vendor_term_id = get_user_meta($product_vendors->id, '_vendor_term_id', true);
                                         if ($commission_vendor_term_id == $vendor_term_id) {
                                             $vendor_orders_array[] = array($order_id, $commission_id, $product_vendors->id, $mark_ship, $order_item_id, $comm_pro_id, $order->order_date, $item_commission, $item_shipping, $item_tax);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if (!empty($vendor_orders_array)) {
                 usort($vendor_orders_array, function ($a, $b) {
                     return $a[0] - $b[0];
                 });
                 if (!get_option('wcmp_vendor_orders_update')) {
                     foreach ($vendor_orders_array as $vendor_orders) {
                         $insert_query = $wpdb->query($wpdb->prepare("INSERT INTO `{$wpdb->prefix}wcmp_vendor_orders` ( order_id, commission_id, vendor_id, shipping_status, order_item_id, product_id, created, commission_amount, shipping, tax )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t VALUES\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t ( %d, %d, %d, %s, %d, %d, %s, %s, %s, %s )", $vendor_orders[0], $vendor_orders[1], $vendor_orders[2], $vendor_orders[3], $vendor_orders[4], $vendor_orders[5], $vendor_orders[6], $vendor_orders[7], $vendor_orders[8], $vendor_orders[9]));
                     }
                 }
                 update_option('wcmp_vendor_orders_update', 1);
             }
         }
         update_option('dc_product_vendor_plugin_db_version', $WCMp->version);
     }
 }
    /**
     * Output the report
     */
    public function output_report()
    {
        global $wpdb, $woocommerce, $WCMp;
        $vendor = $vendor_id = $order_items = false;
        $ranges = array('year' => __('Year', $WCMp->text_domain), 'last_month' => __('Last Month', $WCMp->text_domain), 'month' => __('This Month', $WCMp->text_domain), '7day' => __('Last 7 Days', $WCMp->text_domain));
        $current_range = !empty($_GET['range']) ? sanitize_text_field($_GET['range']) : '7day';
        if (!in_array($current_range, array('custom', 'year', 'last_month', 'month', '7day'))) {
            $current_range = '7day';
        }
        $this->calculate_current_range($current_range);
        if (isset($_POST['vendor'])) {
            $vendor_id = $_POST['vendor'];
            $vendor = get_wcmp_vendor_by_term($vendor_id);
            if ($vendor) {
                $products = $vendor->get_products();
            }
            if (!empty($products)) {
                foreach ($products as $product) {
                    $chosen_product_ids[] = $product->ID;
                }
            }
        }
        if ($vendor_id && $vendor) {
            $option = '<option value="' . $vendor_id . '" selected="selected">' . $vendor->user_data->display_name . '</option>';
        } else {
            $option = '<option></option>';
        }
        $all_vendors = get_wcmp_vendors();
        $start_date_str = $this->start_date;
        $end_date_str = $this->end_date;
        $end_date_str = strtotime('+1 day', $end_date_str);
        $start_date = date("Y-m-d H:i:s", $start_date_str);
        $end_date = date("Y-m-d H:i:s", $end_date_str);
        $total_sales = $admin_earning = $vendor_report = $report_bk = array();
        $max_total_sales = $i = 0;
        if (isset($all_vendors) && !empty($all_vendors)) {
            foreach ($all_vendors as $all_vendor) {
                $chosen_product_ids = array();
                $vendor_id = $all_vendor->id;
                $vendor = get_wcmp_vendor($vendor_id);
                if ($vendor) {
                    $products = $vendor->get_products();
                }
                if (!empty($products)) {
                    foreach ($products as $product) {
                        $chosen_product_ids[] = $product->ID;
                    }
                }
                if ($chosen_product_ids && is_array($chosen_product_ids)) {
                    // Get titles and ID's related to product
                    $chosen_product_titles = array();
                    $children_ids = array();
                    foreach ($chosen_product_ids as $product_id) {
                        $children = (array) get_posts('post_parent=' . $product_id . '&fields=ids&post_status=any&numberposts=-1');
                        $children_ids = $children_ids + $children;
                        $chosen_product_titles[] = get_the_title($product_id);
                    }
                    // Get order items
                    $order_items = apply_filters('woocommerce_reports_product_sales_order_items', $wpdb->get_results("\n\t\t\t\t\t\tSELECT posts.ID as order_id, order_item_meta_2.meta_value as product_id, order_item_meta_1.meta_value as variation_id, posts.post_date, SUM( order_item_meta.meta_value ) as item_quantity, SUM( order_item_meta_3.meta_value ) as line_total\n\t\t\t\t\t\tFROM {$wpdb->prefix}woocommerce_order_items as order_items\n\t\t\t\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_1 ON order_items.order_item_id = order_item_meta_1.order_item_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_3 ON order_items.order_item_id = order_item_meta_3.order_item_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID\n\t\t\t\n\t\t\t\t\t\tWHERE posts.post_type \t= 'shop_order'\n\t\t\t\t\t\tAND \torder_item_meta_2.meta_value IN ('" . implode("','", array_merge($chosen_product_ids, $children_ids)) . "')\n\t\t\t\t\t\tAND posts.post_status IN ('wc-pending','wc-processing','wc-on-hold','wc-completed','wc-cancelled','wc-refunded','wc-failed')\n\t\t\t\t\t\tAND \torder_items.order_item_type = 'line_item'\n\t\t\t\t\t\tAND \torder_item_meta.meta_key = '_qty'\n\t\t\t\t\t\tAND \torder_item_meta_2.meta_key = '_product_id'\n\t\t\t\t\t\tAND \torder_item_meta_1.meta_key = '_variation_id'\n\t\t\t\t\t\tAND \torder_item_meta_3.meta_key = '_line_total'\n\t\t\t\t\t\tAND   posts.post_date BETWEEN '" . $start_date . "' AND '" . $end_date . "'\n\t\t\t\t\t\tGROUP BY order_items.order_id\n\t\t\t\t\t\tORDER BY posts.post_date ASC\n\t\t\t\t\t"), array_merge($chosen_product_ids, $children_ids));
                    if ($order_items) {
                        foreach ($order_items as $order_item) {
                            if ($order_item->line_total == 0 && $order_item->item_quantity == 0) {
                                continue;
                            }
                            if ($order_item->variation_id != '0') {
                                $product_id = $order_item->variation_id;
                                $variation_id = $order_item->variation_id;
                            } else {
                                $product_id = $order_item->product_id;
                                $variation_id = 0;
                            }
                            $commissions = false;
                            $vendor_earnings = 0;
                            $args = array('post_type' => 'dc_commission', 'post_status' => array('publish', 'private'), 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_commission_vendor', 'value' => absint($vendor->term_id), 'compare' => '='), array('key' => '_commission_order_id', 'value' => absint($order_item->order_id), 'compare' => '='), array('key' => '_commission_product', 'value' => absint($product_id), 'compare' => 'LIKE')));
                            $commissions = get_posts($args);
                            if (!empty($commissions)) {
                                foreach ($commissions as $commission) {
                                    $vendor_earnings = $vendor_earnings + get_post_meta($commission->ID, '_commission_amount', true);
                                }
                            }
                            if ($vendor_earnings <= 0) {
                                continue;
                            }
                            $total_sales[$vendor_id] = isset($total_sales[$vendor_id]) ? $total_sales[$vendor_id] + $order_item->line_total : $order_item->line_total;
                            $admin_earning[$vendor_id] = isset($admin_earning[$vendor_id]) ? $admin_earning[$vendor_id] + $order_item->line_total - $vendor_earnings : $order_item->line_total - $vendor_earnings;
                            if ($total_sales[$vendor_id] > $max_total_sales) {
                                $max_total_sales = $total_sales[$vendor_id];
                            }
                        }
                    }
                }
                if (isset($total_sales[$vendor_id]) && isset($admin_earning[$vendor_id])) {
                    $vendor_report[$i]['vendor_id'] = $vendor_id;
                    $vendor_report[$i]['total_sales'] = $total_sales[$vendor_id];
                    $vendor_report[$i++]['admin_earning'] = $admin_earning[$vendor_id];
                    $report_bk[$vendor_id]['total_sales'] = $total_sales[$vendor_id];
                    $report_bk[$vendor_id]['admin_earning'] = $admin_earning[$vendor_id];
                }
            }
            $i = 0;
            $max_value = 10;
            $report_sort_arr = array();
            if (isset($vendor_report) && isset($report_bk)) {
                $total_sales_sort = wp_list_pluck($vendor_report, 'total_sales', 'vendor_id');
                $admin_earning_sort = wp_list_pluck($vendor_report, 'admin_earning', 'vendor_id');
                foreach ($total_sales_sort as $key => $value) {
                    $total_sales_sort_arr[$key]['total_sales'] = $report_bk[$key]['total_sales'];
                    $total_sales_sort_arr[$key]['admin_earning'] = $report_bk[$key]['admin_earning'];
                }
                arsort($total_sales_sort);
                foreach ($total_sales_sort as $key => $value) {
                    if ($i++ < $max_value) {
                        $report_sort_arr[$key]['total_sales'] = $report_bk[$key]['total_sales'];
                        $report_sort_arr[$key]['admin_earning'] = $report_bk[$key]['admin_earning'];
                    }
                }
            }
            wp_localize_script('wcmp_report_js', 'wcmp_report_vendor', array('vendor_report' => $vendor_report, 'report_bk' => $report_bk, 'total_sales_sort' => $total_sales_sort, 'admin_earning_sort' => $admin_earning_sort, 'max_total_sales' => $max_total_sales, 'start_date' => $start_date, 'end_date' => $end_date));
            $chart_arr = $html_chart = '';
            if (count($report_sort_arr) > 0) {
                foreach ($report_sort_arr as $vendor_id => $sales_report) {
                    $total_sales_width = $sales_report['total_sales'] > 0 ? $sales_report['total_sales'] / round($max_total_sales) * 100 : 0;
                    $admin_earning_width = $sales_report['admin_earning'] > 0 ? $sales_report['admin_earning'] / round($max_total_sales) * 100 : 0;
                    $user = get_userdata($vendor_id);
                    $user_name = $user->data->display_name;
                    $chart_arr .= '<tr><th><a href="user-edit.php?user_id=' . $vendor_id . '">' . $user_name . '</a></th>
					<td width="1%"><span>' . woocommerce_price($sales_report['total_sales']) . '</span><span class="alt">' . woocommerce_price($sales_report['admin_earning']) . '</span></td>
					<td class="bars">
						<span style="width:' . esc_attr($total_sales_width) . '%">&nbsp;</span>
						<span class="alt" style="width:' . esc_attr($admin_earning_width) . '%">&nbsp;</span>
					</td></tr>';
                }
                $html_chart = '
					<h4>' . __("Sales and Earnings", $WCMp->text_domain) . '</h4>
					<div class="bar_indecator">
						<div class="bar1">&nbsp;</div>
						<span class="">' . __('Gross Sales', $WCMp->text_domain) . '</span>
						<div class="bar2">&nbsp;</div>
						<span class="">' . __('My Earnings', $WCMp->text_domain) . '</span>
					</div>
					<table class="bar_chart">
						<thead>
							<tr>
								<th>' . __("Vendors", $WCMp->text_domain) . '</th>
								<th colspan="2">' . __("Sales Report", $WCMp->text_domain) . '</th>
							</tr>
						</thead>
						<tbody>
							' . $chart_arr . '
						</tbody>
					</table>
				';
            } else {
                $html_chart = '<tr><td colspan="3">' . __('Any vendor did not generate any sales in the given period.', $WCMp->text_domain) . '</td></tr>';
            }
        } else {
            $html_chart = '<tr><td colspan="3">' . __('Your store has no vendors.', $WCMp->text_domain) . '</td></tr>';
        }
        include $WCMp->plugin_path . '/classes/reports/views/html-wcmp-report-by-vendor.php';
    }
Example #4
0
 /**
  * Search vendors via AJAX
  *
  * @return void
  */
 function woocommerce_json_search_vendors()
 {
     global $WCMp;
     //check_ajax_referer( 'search-vendors', 'security' );
     header('Content-Type: application/json; charset=utf-8');
     $term = urldecode(stripslashes(strip_tags($_GET['term'])));
     if (empty($term)) {
         die;
     }
     $found_vendors = array();
     $args = array('search' => '*' . $term . '*', 'search_columns' => array('user_login', 'display_name', 'user_email'));
     $vendors = get_wcmp_vendors($args);
     if ($vendors) {
         foreach ($vendors as $vendor) {
             $found_vendors[$vendor->term_id] = $vendor->user_data->display_name;
         }
     }
     echo json_encode($found_vendors);
     die;
 }
    /**
     * Register and add settings
     */
    public function settings_page_init()
    {
        global $WCMp;
        //pending vendor
        $get_pending_vendors = get_users('role=dc_pending_vendor');
        if (!empty($get_pending_vendors)) {
            ?>
  	<h3><?php 
            _e('Pending Vendor Approval', $WCMp->text_domain);
            ?>
</h3>
  	<table class="form-table" id="to_do_list">
  		<tbody>
  			<tr>
					<th style="width:50%" ><?php 
            _e('Pending User', $WCMp->text_domain);
            ?>
 </th>
					<th><?php 
            _e('Edit', $WCMp->text_domain);
            ?>
</th>
					<th><?php 
            _e('Activate', $WCMp->text_domain);
            ?>
</th>
					<th><?php 
            _e('Reject', $WCMp->text_domain);
            ?>
</th>
					<th><?php 
            _e('Dismiss', $WCMp->text_domain);
            ?>
</th>
				</tr>
  			<?php 
            foreach ($get_pending_vendors as $pending_vendor) {
                $dismiss = get_user_meta($pending_vendor->ID, '_dismiss_to_do_list', true);
                if ($dismiss) {
                    continue;
                }
                ?>
					<tr>
						<td style="width:50%" class="username column-username"><img alt="" src="<?php 
                echo $WCMp->plugin_url . 'assets/images/wp-avatar-frau.jpg';
                ?>
" class="avatar avatar-32 photo" height="32" width="32"><?php 
                echo $pending_vendor->user_login;
                ?>
</td>
						<td class="edit"><a target="_blank" href="user-edit.php?user_id=<?php 
                echo $pending_vendor->ID;
                ?>
&amp;wp_http_referer=%2Fwordpress%2Fdc_vendor%2Fwp-admin%2Fusers.php%3Frole%3Ddc_pending_vendor"><input type="button" class="vendor_edit_button" value="Edit" /> </a> </td>
						<td class="activate"><input class="activate_vendor" type="button" class="activate_vendor" data-id="<?php 
                echo $pending_vendor->ID;
                ?>
" value="Activate" ></td> 
						<td class="reject"><input class="reject_vendor" type="button" class="reject_vendor" data-id="<?php 
                echo $pending_vendor->ID;
                ?>
" value="Reject"></td>
						<td class="dismiss"><input class="vendor_dismiss_button" type="button" data-type="user" data-id="<?php 
                echo $pending_vendor->ID;
                ?>
" id="dismiss_request" name="dismiss_request" value="Dismiss"></td>
					</tr>
				<?php 
            }
            ?>
  		</tbody>
  	</table>
  	<?php 
        }
        $vendors = get_wcmp_vendors();
        if ($vendors) {
            $vendor_ids = array();
            foreach ($vendors as $vendor) {
                $vendor_ids[] = $vendor->id;
            }
        }
        //coupon
        $args = array('posts_per_page' => -1, 'author__in' => $vendor_ids, 'post_type' => 'shop_coupon', 'post_status' => 'pending');
        $get_pending_coupons = new WP_Query($args);
        $get_pending_coupons = $get_pending_coupons->get_posts();
        if (!empty($get_pending_coupons)) {
            ?>
  	<h3><?php 
            _e('Pending Coupons Approval', $WCMp->text_domain);
            ?>
</h3>
  	<table class="form-table" id="to_do_list">
  		<tbody>
  			<tr>
					<th><?php 
            _e('Vendor Name', $WCMp->text_domain);
            ?>
 </th>
					<th><?php 
            _e('Coupon Name', $WCMp->text_domain);
            ?>
</th>
					<th><?php 
            _e('Edit', $WCMp->text_domain);
            ?>
</th>
					<th><?php 
            _e('Dismiss', $WCMp->text_domain);
            ?>
</th>
				</tr>
  			<?php 
            foreach ($get_pending_coupons as $get_pending_coupon) {
                $dismiss = get_post_meta($get_pending_coupon->ID, '_dismiss_to_do_list', true);
                if ($dismiss) {
                    continue;
                }
                ?>
					<tr>
						<?php 
                $currentvendor = get_userdata($get_pending_coupon->post_author);
                ?>
						<td class="coupon column-coupon"><a href="user-edit.php?user_id=<?php 
                echo $get_pending_coupon->post_author;
                ?>
&amp;wp_http_referer=%2Fwordpress%2Fdc_vendor%2Fwp-admin%2Fusers.php%3Frole%3Ddc_vendor" target="_blank"><?php 
                echo $currentvendor->display_name;
                ?>
</a></td>
						<td class="coupon column-coupon"><?php 
                echo $get_pending_coupon->post_title;
                ?>
</td>
						<td class="edit"><a target="_blank" href="post.php?post=<?php 
                echo $get_pending_coupon->ID;
                ?>
&action=edit"><input type="button" class="vendor_edit_button" value="Edit" /> </a> </td>
						<td class="dismiss"><input class="vendor_dismiss_button" type="button" data-type="shop_coupon" data-id="<?php 
                echo $get_pending_coupon->ID;
                ?>
" id="dismiss_request" name="dismiss_request" value="Dismiss"></td>
					</tr>
				<?php 
            }
            ?>
  		</tbody>
  	</table>
  	<?php 
        }
        //product
        $args = array('posts_per_page' => -1, 'author__in' => $vendor_ids, 'post_type' => 'product', 'post_status' => 'pending');
        $get_pending_products = new WP_Query($args);
        $get_pending_products = $get_pending_products->get_posts();
        if (!empty($get_pending_products)) {
            ?>
  	<h3><?php 
            _e('Pending Products Approval', $WCMp->text_domain);
            ?>
</h3>
  	<table class="form-table" id="to_do_list">
  		<tbody>
  			<tr>
						<th><?php 
            _e('Vendor Name', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Product Name', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Edit', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Dismiss', $WCMp->text_domain);
            ?>
</th>
				</tr>
  			<?php 
            foreach ($get_pending_products as $get_pending_product) {
                $dismiss = get_post_meta($get_pending_product->ID, '_dismiss_to_do_list', true);
                if ($dismiss) {
                    continue;
                }
                ?>
					<tr>
						<?php 
                $currentvendor = get_userdata($get_pending_product->post_author);
                ?>
						<td class="vendor column-coupon"><a href="user-edit.php?user_id=<?php 
                echo $get_pending_product->post_author;
                ?>
&amp;wp_http_referer=%2Fwordpress%2Fdc_vendor%2Fwp-admin%2Fusers.php%3Frole%3Ddc_vendor" target="_blank"><?php 
                echo $currentvendor->display_name;
                ?>
</a></td>
						<td class="coupon column-coupon"><?php 
                echo $get_pending_product->post_title;
                ?>
</td>
						<td class="edit"><a target="_blank" href="post.php?post=<?php 
                echo $get_pending_product->ID;
                ?>
&action=edit"><input type="button" class="vendor_edit_button" value="Edit" /> </a> </td>
						<td class="dismiss"><input class="vendor_dismiss_button" data-type="product" data-id="<?php 
                echo $get_pending_product->ID;
                ?>
"  type="button" id="dismiss_request" name="dismiss_request" value="Dismiss"></td>
					</tr>
				<?php 
            }
            ?>
  		</tbody>
  	</table>
  	<?php 
        }
        //commission
        $args = array('post_type' => 'wcmp_transaction', 'post_status' => 'wcmp_processing', 'meta_key' => 'transaction_mode', 'meta_value' => 'direct_bank', 'posts_per_page' => -1);
        $transactions = get_posts($args);
        if (!empty($transactions)) {
            ?>
  	<h3><?php 
            _e('Pending Bank Transfer', $WCMp->text_domain);
            ?>
</h3>
  	<table class="form-table" id="to_do_list">
  		<tbody>
  			<tr>
						<th><?php 
            _e('Vendor', $WCMp->text_domain);
            ?>
 </th>
						<th><?php 
            _e('Commission', $WCMp->text_domain);
            ?>
 </th>
						<th><?php 
            _e('Amount', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Account Detail', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Notify the Vendor', $WCMp->text_domain);
            ?>
</th>
						<th><?php 
            _e('Dismiss', $WCMp->text_domain);
            ?>
</th>
				</tr>
  			<?php 
            foreach ($transactions as $transaction) {
                $dismiss = get_post_meta($transaction->ID, '_dismiss_to_do_list', true);
                if ($dismiss) {
                    continue;
                }
                ?>
					<tr>
						<?php 
                $vendor_term_id = $transaction->post_author;
                $currentvendor = get_wcmp_vendor_by_term($vendor_term_id);
                $account_name = get_user_meta($currentvendor->id, '_vendor_account_holder_name', true);
                $account_no = get_user_meta($currentvendor->id, '_vendor_bank_account_number', true);
                $bank_name = get_user_meta($currentvendor->id, '_vendor_bank_name', true);
                $iban = get_user_meta($currentvendor->id, '_vendor_iban', true);
                $amount = get_post_meta($transaction->ID, 'amount', true);
                ?>
    
						<td class="vendor column-coupon"><a href="user-edit.php?user_id=<?php 
                echo $currentvendor->id;
                ?>
&amp;wp_http_referer=%2Fwordpress%2Fdc_vendor%2Fwp-admin%2Fusers.php%3Frole%3Ddc_vendor" target="_blank"><?php 
                echo $currentvendor->user_data->display_name;
                ?>
</a></td>
						<td class="commission column-coupon"><a href="post.php?post=<?php 
                echo $transaction->ID;
                ?>
&action=edit" target="_blank" ><?php 
                echo $transaction->post_title;
                ?>
</a></td>
						<td class="commission_val column-coupon"><?php 
                echo get_woocommerce_currency_symbol() . $amount;
                ?>
</td>
						<td class="account_detail"><?php 
                echo __('Account Name- ', $WCMp->text_domain) . ' ' . $account_name . '<br>' . __('Account No - ', $WCMp->text_domain) . $account_no . '<br>' . __('Bank Name - ', $WCMp->text_domain) . $bank_name . '<br>' . __('IBAN - ', $WCMp->text_domain) . $iban;
                ?>
</td>
						<td class="done"><input class="vendor_transaction_done_button" data-transid="<?php 
                echo $transaction->ID;
                ?>
" data-vendorid="<?php 
                echo $vendor_term_id;
                ?>
" type="button" id="done_request" name="done_request" value="Done"></td>
						<td class="dismiss"><input class="vendor_dismiss_button" data-type="dc_commission" data-id="<?php 
                echo $transaction->ID;
                ?>
" type="button" id="dismiss_request" name="dismiss_request" value="Dismiss"></td>
					</tr>
				<?php 
            }
            ?>
  		</tbody>
  	</table>
  	<?php 
        }
    }
    /**
     * 	list of vendors 
     * 
     * 	@param $atts shortcode attributs 
     */
    public function wcmp_show_vendorslist($atts)
    {
        global $WCMp;
        $vendors_available = false;
        $get_all_vendors = array();
        $select_html = '';
        extract(shortcode_atts(array('orderby' => 'registered', 'order' => 'ASC'), $atts));
        $vendors = '';
        $vendor_sort_type = '';
        if (isset($_GET['vendor_sort_type'])) {
            if ($_GET['vendor_sort_type'] == 'category') {
                $vendors_ids = array();
                $vendor_sort_type = $_GET['vendor_sort_type'];
                $selected_category = $_GET['vendor_sort_category'];
                $args = array('post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $selected_category)));
                $wp_obj = new WP_Query($args);
                $sorted_products = $wp_obj->posts;
                if (isset($sorted_products) && !empty($sorted_products)) {
                    foreach ($sorted_products as $sorted_product) {
                        $vendor_obj = get_wcmp_product_vendors($sorted_product->ID);
                        if (isset($vendor_obj) && !empty($vendor_obj)) {
                            if (!in_array($vendor_obj->id, $vendors_ids)) {
                                $vendors_ids[] = $vendor_obj->id;
                                $get_all_vendors[] = new WCMp_Vendor($vendor_obj->id);
                                $vendors_available = true;
                            }
                        }
                    }
                }
            } else {
                $vendor_sort_type = $_GET['vendor_sort_type'];
                $orderby = $vendor_sort_type;
                $order = 'ASC';
            }
        }
        if (!$vendors_available) {
            $get_all_vendors = get_wcmp_vendors(array('orderby' => $orderby, 'order' => $order));
        }
        $vendors .= '<div class="vendor_list">';
        $vendors .= '<form name="vendor_sort" method="get" ><div class="vendor_sort">';
        $vendors .= '<select class="select short" id="vendor_sort_type" name="vendor_sort_type">';
        if ($vendor_sort_type) {
            if ($vendor_sort_type == 'registered') {
                $option = '<option selected value="registered">' . __("By date", $WCMp->text_domain) . '</option><option value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
            } else {
                if ($vendor_sort_type == 'name') {
                    $option = '<option value="registered">' . __("By date", $WCMp->text_domain) . '</option><option selected value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
                } else {
                    if ($vendor_sort_type == 'category') {
                        $option = '<option value="registered">' . __("By date", $WCMp->text_domain) . '</option><option value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option selected value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
                    } else {
                        $option = '<option value="registered">' . __("By date", $WCMp->text_domain) . '</option><option value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
                    }
                }
            }
        } else {
            if ($orderby == 'registered') {
                $option = '<option selected value="registered">' . __("By date", $WCMp->text_domain) . '</option><option value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
            } else {
                if ($orderby == 'name') {
                    $option = '<option  value="registered">' . __("By date", $WCMp->text_domain) . '</option><option selected value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
                } else {
                    if ($vendor_sort_type == 'category') {
                        $option = '<option value="registered">' . __("By date", $WCMp->text_domain) . '</option><option value="name">' . __("By Alphabetically", $WCMp->text_domain) . '</option><option value="category">' . __("By Category", $WCMp->text_domain) . '</option>';
                    }
                }
            }
        }
        if (isset($_GET['vendor_sort_type'])) {
            if ($_GET['vendor_sort_type'] == 'category') {
                $category_terms = get_terms('product_cat');
                $select_html = '&nbsp&nbsp&nbsp<select class="select" id="vendor_sort_category" name="vendor_sort_category">';
                foreach ($category_terms as $terms) {
                    if (isset($_GET['vendor_sort_category'])) {
                        if ($_GET['vendor_sort_category'] == $terms->term_id) {
                            $select_html .= '<option selected value="' . $terms->term_id . '">' . $terms->name . '</option>';
                        } else {
                            $select_html .= '<option value="' . $terms->term_id . '">' . $terms->name . '</option>';
                        }
                    }
                }
                $select_html .= '</select>';
            }
        }
        $vendors .= $option . '</select>' . $select_html;
        $vendors .= '&nbsp;&nbsp;&nbsp;<input type="submit" value="' . __("Sort", $WCMp->text_domain) . '" />';
        $vendors .= '</div>';
        $vendors .= '</form>';
        $get_blocked = wcmp_get_all_blocked_vendors();
        $get_block_array = array();
        if (!empty($get_blocked)) {
            foreach ($get_blocked as $get_block) {
                $get_block_array[] = (int) $get_block->id;
            }
        }
        if (isset($get_all_vendors) && !empty($get_all_vendors)) {
            foreach ($get_all_vendors as $get_vendor) {
                if (in_array($get_vendor->id, $get_block_array)) {
                    continue;
                }
                if (!$get_vendor->image) {
                    $get_vendor->image = $WCMp->plugin_url . 'assets/images/WP-stdavatar.png';
                }
                $vendors .= '<div class="sorted_vendors" style="display:inline-block; margin-right:10%;">
											 <center>
													<a href="' . $get_vendor->permalink . '"><img width="125" class="vendor_img" src="' . $get_vendor->image . '" id="vendor_image_display"></a><br />
													<a href="' . $get_vendor->permalink . '" class="button">' . $get_vendor->user_data->display_name . '</a>
													<br /><br />
											 </center>
										 </div>';
            }
            $vendors .= '</div>';
        }
        return $vendors;
    }
Example #7
0
 /**
  * wcmp_get_all_blocked_vendors Function
  *
  * @access public
  * @return plugin array
  */
 function wcmp_get_all_blocked_vendors()
 {
     $vendors = get_wcmp_vendors();
     $blocked_vendor = array();
     if (!empty($vendors)) {
         foreach ($vendors as $vendor_key => $vendor) {
             $is_block = get_user_meta($vendor->id, '_vendor_turn_off', true);
             if ($is_block) {
                 $blocked_vendor[] = $vendor;
             }
         }
     }
     return $blocked_vendor;
 }