/** * Calc line tax */ public function calc_line_taxes() { global $wpdb; check_ajax_referer('calc-totals', 'security'); $this->json_headers(); $tax = new WC_Tax(); $taxes = $tax_rows = $item_taxes = $shipping_taxes = array(); $order_id = absint($_POST['order_id']); $order = new WC_Order($order_id); $country = strtoupper(esc_attr($_POST['country'])); $state = strtoupper(esc_attr($_POST['state'])); $postcode = strtoupper(esc_attr($_POST['postcode'])); $city = sanitize_title(esc_attr($_POST['city'])); $items = isset($_POST['items']) ? $_POST['items'] : array(); $shipping = $_POST['shipping']; $item_tax = 0; // Calculate sales tax first if (sizeof($items) > 0) { foreach ($items as $item_id => $item) { $item_id = absint($item_id); $line_subtotal = isset($item['line_subtotal']) ? wc_format_decimal($item['line_subtotal']) : 0; $line_total = wc_format_decimal($item['line_total']); $tax_class = sanitize_text_field($item['tax_class']); $product_id = $order->get_item_meta($item_id, '_product_id', true); if (!$item_id || '0' == $tax_class) { continue; } // Get product details if (get_post_type($product_id) == 'product') { $_product = get_product($product_id); $item_tax_status = $_product->get_tax_status(); } else { $item_tax_status = 'taxable'; } // Only calc if taxable if ('taxable' == $item_tax_status) { $tax_rates = $tax->find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); $line_subtotal_taxes = $tax->calc_tax($line_subtotal, $tax_rates, false); $line_taxes = $tax->calc_tax($line_total, $tax_rates, false); $line_subtotal_tax = array_sum($line_subtotal_taxes); $line_tax = array_sum($line_taxes); if ($line_subtotal_tax < 0) { $line_subtotal_tax = 0; } if ($line_tax < 0) { $line_tax = 0; } $item_taxes[$item_id] = array('line_subtotal_tax' => wc_format_localized_price($line_subtotal_tax), 'line_tax' => wc_format_localized_price($line_tax)); $item_tax += $line_tax; // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } } // Now calculate shipping tax $matched_tax_rates = array(); $tax_rates = $tax->find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if ($tax_rates) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' == $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $shipping_taxes = $tax->calc_shipping_tax($shipping, $matched_tax_rates); $shipping_tax = $tax->round(array_sum($shipping_taxes)); // Remove old tax rows $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax' )", $order_id)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax'", $order_id)); // Get tax rates $rates = $wpdb->get_results("SELECT tax_rate_id, tax_rate_country, tax_rate_state, tax_rate_name, tax_rate_priority FROM {$wpdb->prefix}woocommerce_tax_rates ORDER BY tax_rate_name"); $tax_codes = array(); foreach ($rates as $rate) { $code = array(); $code[] = $rate->tax_rate_country; $code[] = $rate->tax_rate_state; $code[] = $rate->tax_rate_name ? sanitize_title($rate->tax_rate_name) : 'TAX'; $code[] = absint($rate->tax_rate_priority); $tax_codes[$rate->tax_rate_id] = strtoupper(implode('-', array_filter($code))); } // Now merge to keep tax rows ob_start(); foreach (array_keys($taxes + $shipping_taxes) as $key) { $item = array(); $item['rate_id'] = $key; $item['name'] = $tax_codes[$key]; $item['label'] = $tax->get_rate_label($key); $item['compound'] = $tax->is_compound($key) ? 1 : 0; $item['tax_amount'] = wc_format_decimal(isset($taxes[$key]) ? $taxes[$key] : 0); $item['shipping_tax_amount'] = wc_format_decimal(isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0); if (!$item['label']) { $item['label'] = WC()->countries->tax_or_vat(); } // Add line item $item_id = wc_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'tax')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, 'rate_id', $item['rate_id']); wc_add_order_item_meta($item_id, 'label', $item['label']); wc_add_order_item_meta($item_id, 'compound', $item['compound']); wc_add_order_item_meta($item_id, 'tax_amount', $item['tax_amount']); wc_add_order_item_meta($item_id, 'shipping_tax_amount', $item['shipping_tax_amount']); } include 'admin/post-types/meta-boxes/views/html-order-tax.php'; } $tax_row_html = ob_get_clean(); // Return echo json_encode(array('item_tax' => $item_tax, 'item_taxes' => $item_taxes, 'shipping_tax' => $shipping_tax, 'tax_row_html' => $tax_row_html)); // Quit out die; }
<th class="line_cost"><?php _e('Totals', 'woocommerce'); ?> </th> </tr> </thead> <tbody id="order_items_list"> <?php // List order items $order_items = $order->get_items(apply_filters('woocommerce_admin_order_item_types', array('line_item', 'fee'))); foreach ($order_items as $item_id => $item) { switch ($item['type']) { case 'line_item': $_product = $order->get_product_from_item($item); $item_meta = $order->get_item_meta($item_id); include 'order-item-html.php'; break; case 'fee': include 'order-fee-html.php'; break; } do_action('woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item); } ?> </tbody> <tfoot> <?php if ($totals = $order->get_order_item_totals()) { foreach ($totals as $total) {
/** * get_vendor_items_from_order function get items of a order belongs to a vendor * @access public * @param order_id , vendor term id * @return array with order item detail */ public function get_vendor_items_from_order($order_id, $term_id) { $item_dtl = array(); $order = new WC_Order($order_id); if ($order) { $items = $order->get_items('line_item'); if ($items) { foreach ($items as $item_id => $item) { $product_id = $order->get_item_meta($item_id, '_product_id', true); if ($product_id) { if ($term_id > 0) { $product_vendors = get_wcmp_product_vendors($product_id); if (!empty($product_vendors) && $product_vendors->term_id == $term_id) { $item_dtl[$item_id] = $item; } } } } } } return $item_dtl; }
/** * Gets an individual order item by ID without requiring the order ID associated with it. * * @param WC_Order|int $order The WC_Order object or ID of the order for which the meta should be sought. * @param int $item_id The product/post ID of a subscription. Option - if no product id is provided, the first item's meta will be returned * @return array $item An array containing the order_item_id, order_item_name, order_item_type, order_id and any item_meta. Array structure matches that returned by WC_Order::get_items() * @since 1.2.5 */ public static function get_item_by_id($order_item_id) { global $wpdb; $item = $wpdb->get_row($wpdb->prepare("\n\t\t\tSELECT order_item_id, order_item_name, order_item_type, order_id\n\t\t\tFROM {$wpdb->prefix}woocommerce_order_items\n\t\t\tWHERE order_item_id = %d\n\t\t", $order_item_id), ARRAY_A); $order = new WC_Order(absint($item['order_id'])); $item['name'] = $item['order_item_name']; $item['type'] = $item['order_item_type']; $item['item_meta'] = $order->get_item_meta($item['order_item_id']); // Put meta into item array if (is_array($item['item_meta'])) { foreach ($item['item_meta'] as $meta_name => $meta_value) { $key = substr($meta_name, 0, 1) == '_' ? substr($meta_name, 1) : $meta_name; $item[$key] = maybe_unserialize($meta_value[0]); } } return $item; }
private function output_wc_start() { global $product, $woocommerce; $this->disable_export_btns = $product->is_downloadable() ? true : false; //added to cart, recall added product if (isset($_POST['fpd_product'])) { $views = $_POST['fpd_product']; $this->form_views = stripslashes($views); } else { if (isset($_GET['cart_item_key'])) { //load from cart item $cart = $woocommerce->cart->get_cart(); $cart_item = $cart[$_GET['cart_item_key']]; if ($cart_item) { if (isset($cart_item['fpd_data'])) { $views = $cart_item['fpd_data']['fpd_product']; $this->form_views = stripslashes($views); } } else { //cart item could not be found echo '<p><strong>'; _e('Sorry, but the cart item could not be found!', 'radykal'); echo '</strong></p>'; return; } } else { if (isset($_GET['order']) && isset($_GET['item_id'])) { //load ordered product in designer $order = new WC_Order($_GET['order']); $item_meta = $order->get_item_meta($_GET['item_id'], 'fpd_data'); $this->form_views = $item_meta[0]["fpd_product"]; if ($product->is_downloadable() && $order->is_download_permitted()) { $this->disable_export_btns = false; ?> <br /> <a href="#" id="fpd-extern-download-pdf"><?php echo fpd_get_option('fpd_label_downLoadPDF'); ?> </a> <?php } } else { if (isset($_GET['share_id'])) { $transient_key = 'fpd_share_' . $_GET['share_id']; $transient_val = get_transient($transient_key); if ($transient_val !== false) { $this->form_views = stripslashes($transient_val['product']); } } } } } }
/** * 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); } }
/** * Get variation detail from order and item id * * @package WooCommerce - PDF Vouchers * @since 2.2.2 **/ public function woo_vou_get_variation_data($woo_order = array(), $item_key = '') { //Get product order meta using meta_key $order = new WC_Order($woo_order->id); $product_item_meta = $order->get_item_meta($item_key); $items = $order->get_items(); $item_array = $this->woo_vou_get_item_data_using_item_key($items, $item_key); $item = isset($item_array['item_data']) ? $item_array['item_data'] : array(); $item_id = isset($item_array['item_id']) ? $item_array['item_id'] : array(); $_product = $order->get_product_from_item($item); //Get variation data without recipient fields $variation_data = $this->woo_vou_display_product_item_name($item, $_product, true); return apply_filters('woo_vou_get_variation_data', $variation_data, $order, $item_key); }
/** * Get vendor commission per item for an order * * @param int $product_id * @param int $variation_id * @param array $item * @param int $order_id * * @return $commission_amount */ public function get_item_commission($product_id, $variation_id, $item, $order_id, $item_id = '') { global $WCMp; $order = new WC_Order($order_id); $amount = 0; $commission = array(); if (isset($WCMp->vendor_caps->payment_cap['commission_include_coupon'])) { $line_total = $order->get_item_total($item, false, false) * $item['qty']; } else { $line_total = $order->get_item_subtotal($item, false, false) * $item['qty']; } if ($product_id && $line_total) { $vendor_id = $order->get_item_meta($item_id, '_vendor_id', true); if ($vendor_id) { $vendor = get_wcmp_vendor($vendor_id); } else { $vendor = get_wcmp_product_vendors($product_id); } if ($vendor) { $commission = $this->get_commission_amount($product_id, $vendor->term_id, $variation_id, $item_id, $order); if (!empty($commission)) { if ($WCMp->vendor_caps->payment_cap['commission_type'] == 'fixed_with_percentage') { $amount = (double) $line_total * ((double) $commission['commission_val'] / 100) + (double) $commission['commission_fixed']; } else { if ($WCMp->vendor_caps->payment_cap['commission_type'] == 'fixed_with_percentage_qty') { $amount = (double) $line_total * ((double) $commission['commission_val'] / 100) + (double) $commission['commission_fixed'] * $item['qty']; } else { if ($WCMp->vendor_caps->payment_cap['commission_type'] == 'percent') { $amount = (double) $line_total * ((double) $commission['commission_val'] / 100); } else { if ($WCMp->vendor_caps->payment_cap['commission_type'] == 'fixed') { $amount = (double) $commission['commission_val'] * $item['qty']; } } } } if (isset($WCMp->vendor_caps->payment_cap['revenue_sharing_mode'])) { if ($WCMp->vendor_caps->payment_cap['revenue_sharing_mode'] == 'admin') { $amount = (double) $line_total - (double) $amount; if ($amount < 0) { $amount = 0; } } } return apply_filters('vendor_commission_amount', $amount); } } } return apply_filters('vendor_commission_amount', $amount); }
/** * get vendor commission by date * * @access public * @param mixed $vars * @return array */ public function vendor_sales_stat_overview($vendor, $start_date = false, $end_date = false) { global $WCMp; $total_sales = 0; $total_vendor_earnings = 0; $total_order_count = 0; $total_purchased_products = 0; $total_coupon_used = 0; $total_coupon_discuont_value = 0; $total_earnings = 0; $total_customers = array(); $vendor = get_wcmp_vendor(get_current_user_id()); for ($date = strtotime($start_date); $date <= strtotime('+1 day', strtotime($end_date)); $date = strtotime('+1 day', $date)) { $year = date('Y', $date); $month = date('n', $date); $day = date('j', $date); $line_total = $sales = $comm_amount = $vendor_earnings = $earnings = 0; $args = array('post_type' => 'shop_order', 'posts_per_page' => -1, 'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed'), 'meta_query' => array(array('key' => '_commissions_processed', 'value' => 'yes', 'compare' => '=')), 'date_query' => array(array('year' => $year, 'month' => $month, 'day' => $day))); $qry = new WP_Query($args); $orders = apply_filters('wcmp_filter_orders_report_overview', $qry->get_posts(), $vendor->id); if (!empty($orders)) { foreach ($orders as $order_obj) { $order = new WC_Order($order_obj->ID); $items = $order->get_items('line_item'); $commission_array = array(); foreach ($items as $item_id => $item) { $comm_pro_id = $product_id = $order->get_item_meta($item_id, '_product_id', true); $line_total = $order->get_item_meta($item_id, '_line_total', true); $variation_id = $order->get_item_meta($item_id, '_variation_id', true); if ($variation_id) { $comm_pro_id = $variation_id; } if ($product_id && $line_total) { $product_vendors = get_wcmp_product_vendors($product_id); if ($product_vendors) { $sales += $line_total; $total_sales += $line_total; $args = array('post_type' => 'dc_commission', 'post_status' => array('publish', 'private'), 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_commission_vendor', 'value' => absint($product_vendors->term_id), 'compare' => '='), array('key' => '_commission_order_id', 'value' => absint($order_obj->ID), 'compare' => '='), array('key' => '_commission_product', 'value' => absint($comm_pro_id), 'compare' => 'LIKE'))); $commissions = get_posts($args); $comm_amount = 0; if (!empty($commissions)) { foreach ($commissions as $commission) { if (in_array($commission->ID, $commission_array)) { continue; } $comm_amount += (double) get_post_meta($commission->ID, '_commission_amount', true); $commission_array[] = $commission->ID; } } $vendor_earnings += $comm_amount; $total_vendor_earnings += $comm_amount; $earnings += $line_total - $comm_amount; $total_earnings += $line_total - $comm_amount; $total_purchased_products++; } } } //coupons count $coupon_used = array(); $coupons = $order->get_items('coupon'); foreach ($coupons as $coupon_item_id => $item) { $coupon = new WC_Coupon(trim($item['name'])); $coupon_post = get_post($coupon->id); $author_id = $coupon_post->post_author; if ($vendor->id == $author_id) { $total_coupon_used++; $total_coupon_discuont_value += (double) wc_add_order_item_meta($coupon_item_id, 'discount_amount', true); } } ++$total_order_count; //user count if ($order->customer_user != 0 && $order->customer_user != 1) { array_push($total_customers, $order->customer_user); } } } } return array('total_order_count' => $total_order_count, 'total_vendor_sales' => $total_sales, 'total_vendor_earning' => $total_vendor_earnings, 'total_coupon_discuont_value' => $total_coupon_discuont_value, 'total_coupon_used' => $total_coupon_used, 'total_customers' => array_unique($total_customers), 'total_purchased_products' => $total_purchased_products); }
/** * Get all data needed for this report and store in the class */ private function query_report_data() { $this->report_data = new stdClass(); $start_date = $this->start_date; $end_date = $this->end_date; $chart_data_order_count = array(); $total_order_count = 0; $total_earnings = 0; $total_sales = 0; $total_avg_sales = 0; $total_vendor_earnings = 0; for ($date = $start_date; $date <= strtotime('+1 day', $end_date); $date = strtotime('+1 day', $date)) { $year = date('Y', $date); $month = date('n', $date); $day = date('j', $date); $line_total = $sales = $comm_amount = $vendor_earnings = $earnings = $order_count = 0; $args = array('post_type' => 'shop_order', 'posts_per_page' => -1, 'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed'), 'meta_query' => array(array('key' => '_commissions_processed', 'value' => 'yes', 'compare' => '=')), 'date_query' => array(array('year' => $year, 'month' => $month, 'day' => $day))); $qry = new WP_Query($args); $orders = apply_filters('wcmp_filter_orders_report_overview', $qry->get_posts()); if (!empty($orders)) { foreach ($orders as $order_obj) { $order = new WC_Order($order_obj->ID); $items = $order->get_items('line_item'); $commission_array = array(); foreach ($items as $item_id => $item) { $comm_pro_id = $product_id = $order->get_item_meta($item_id, '_product_id', true); $line_total = $order->get_item_meta($item_id, '_line_total', true); $variation_id = $order->get_item_meta($item_id, '_variation_id', true); if ($variation_id) { $comm_pro_id = $variation_id; } if ($product_id && $line_total) { $vendor_id = $order->get_item_meta($item_id, '_vendor_id', true); if ($vendor_id) { $product_vendors = get_wcmp_vendor($vendor_id); } else { $product_vendors = get_wcmp_product_vendors($product_id); } if ($product_vendors) { $sales += $line_total; $total_sales += $line_total; $args = array('post_type' => 'dc_commission', 'post_status' => array('publish', 'private'), 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_commission_vendor', 'value' => absint($product_vendors->term_id), 'compare' => '='), array('key' => '_commission_order_id', 'value' => absint($order_obj->ID), 'compare' => '='), array('key' => '_commission_product', 'value' => absint($comm_pro_id), 'compare' => 'LIKE'))); $commissions = get_posts($args); $comm_amount = 0; if (!empty($commissions)) { foreach ($commissions as $commission) { if (in_array($commission->ID, $commission_array)) { continue; } $comm_amount += (double) get_post_meta($commission->ID, '_commission_amount', true); $commission_array[] = $commission->ID; } } $vendor_earnings += $comm_amount; $total_vendor_earnings += $comm_amount; $earnings += $line_total - $comm_amount; $total_earnings += $line_total - $comm_amount; } } } ++$order_count; ++$total_order_count; } } if ($order_count > 0) { $avg_sales = $sales / $order_count; } else { $avg_sales = 0; } $chart_data_order_count[] = wcmpArrayToObject(array('post_date' => date("Y-m-d H:i:s", $date), 'count' => $order_count)); $chart_data_sales[] = wcmpArrayToObject(array('post_date' => date("Y-m-d H:i:s", $date), 'sales' => $sales)); $chart_data_vendor_earnings[] = wcmpArrayToObject(array('post_date' => date("Y-m-d H:i:s", $date), 'vendor_earnings' => $vendor_earnings)); $chart_data_earnings[] = wcmpArrayToObject(array('post_date' => date("Y-m-d H:i:s", $date), 'earnings' => $earnings)); $chart_data_avg_sales[] = wcmpArrayToObject(array('post_date' => date("Y-m-d H:i:s", $date), 'avg_sales' => $avg_sales)); } $this->report_data->order_count = $chart_data_order_count; $this->report_data->sales = $chart_data_sales; $this->report_data->vendor_earnings = $chart_data_vendor_earnings; $this->report_data->earnings = $chart_data_earnings; $this->report_data->avg_sales = $chart_data_avg_sales; if ($total_order_count > 0) { $total_avg_sales = $total_sales / $total_order_count; } else { $total_avg_sales = 0; } // Total order_count $this->report_data->total_orders = $total_order_count; // Total the refunds and sales amounts. Sales subract refunds. $this->report_data->total_sales = wc_format_decimal($total_sales); // Calculate average based on net $this->report_data->average_sales = wc_format_decimal($total_avg_sales); $this->report_data->total_earned = wc_format_decimal($total_earnings); $this->report_data->vendor_total_earned = wc_format_decimal($total_vendor_earnings); }
/** * wcmp_get_vendors_due_from_order function to get vendor due from an order. * @access public * @param order , vendor term id */ function wcmp_get_vendors_due_from_order($order_id) { $order = new WC_Order($order_id); $items = $order->get_items('line_item'); $vendors_array = array(); if ($items) { foreach ($items as $item_id => $item) { $product_id = $order->get_item_meta($item_id, '_product_id', true); if ($product_id) { $vendor = get_wcmp_product_vendors($product_id); if (!empty($vendor) && isset($vendor->term_id)) { $vendors_array[$vendor->term_id] = $vendor->wcmp_get_vendor_part_from_order($order, $vendor->term_id); } } } } return $vendors_array; }
/** * Action - woocommerce_checkout_order_processed * Adds offer postmeta 'offer_order_id' * @since 0.1.0 */ public function ae_ofwc_woocommerce_checkout_order_processed($order_id) { global $woocommerce; // Get Order $order = new WC_Order($order_id); // Get order items $order_items = $order->get_items(); // Check for offer id foreach ($order_items as $key => $value) { $item_offer_id = $order->get_item_meta($key, 'Offer ID', true); /** * Update offer * Add postmeta value 'offer_order_id' for this order id * Set offer post status to 'completed-offer' */ if ($item_offer_id) { // Update offer post args $offer_data = array(); $offer_data['ID'] = $item_offer_id; $offer_data['post_status'] = 'completed-offer'; // Update offer $offer_id = wp_update_post($offer_data); // Check for offer post id if ($offer_id != 0) { // Add 'offer_order_id' postmeta to offer post add_post_meta($item_offer_id, 'offer_order_id', $order_id, true); // Insert WP comment on related 'offer' $comment_text = "<span>" . __('Updated - Status:', $this->plugin_slug) . "</span> " . __('Completed', $this->plugin_slug); $comment_text .= '<p>' . __('Related Order', $this->plugin_slug) . ': ' . '<a href="post.php?post=' . $order_id . '&action=edit">#' . $order_id . '</a></p>'; $comment_data = array('comment_post_ID' => '', 'comment_author' => 'admin', 'comment_author_email' => '', 'comment_author_url' => '', 'comment_content' => $comment_text, 'comment_type' => '', 'comment_parent' => 0, 'user_id' => 1, 'comment_author_IP' => '127.0.0.1', 'comment_agent' => '', 'comment_date' => date("Y-m-d H:i:s", current_time('timestamp', 0)), 'comment_approved' => 'post-trashed'); $new_comment_id = wp_insert_comment($comment_data); // insert comment meta if ($new_comment_id) { add_comment_meta($new_comment_id, 'angelleye_woocommerce_offer_id', $item_offer_id, true); } } } } }
/** * Gets an individual order item by ID without requiring the order ID associated with it. * * @param $order WC_Order | int The WC_Order object or ID of the order for which the meta should be sought. * @param $item_id int The product/post ID of a subscription. Option - if no product id is provided, the first item's meta will be returned * @return $item array An array containing the order_item_id, order_item_name, order_item_type, order_id and any item_meta. Array structure matches that returned by WC_Order::get_items() * @since 1.2.5 */ public static function get_item_by_id($order_item_id) { global $wpdb; $item = $wpdb->get_row($wpdb->prepare("\n\t\t\tSELECT order_item_id, order_item_name, order_item_type, order_id\n\t\t\tFROM {$wpdb->prefix}woocommerce_order_items\n\t\t\tWHERE order_item_id = %d\n\t\t", $order_item_id)); $order = new WC_Order($order_id); $item['name'] = $item->order_item_name; $item['type'] = $item->order_item_type; $item['item_meta'] = $order->get_item_meta($item->order_item_id); // Put meta into item array foreach ($item['item_meta'] as $meta_name => $meta_value) { $key = substr($meta_name, 0, 1) == '_' ? substr($meta_name, 1) : $meta_name; $item[$key] = $meta_value[0]; } return $item; }
/** * Auction order * * Checks for auction product in order and assign order id to auction product * * @access public * @param int, array * @return void */ function auction_order($order_id, $posteddata) { $order = new WC_Order($order_id); if ($order) { if ($order_items = $order->get_items()) { foreach ($order_items as $item_id => $item) { $item_meta = $order->get_item_meta($item_id); $product_data = get_product($item_meta["_product_id"][0]); if ($product_data->product_type == 'auction') { update_post_meta($order_id, '_auction', '1'); update_post_meta($item_meta["_product_id"][0], '_order_id', $order_id, true); update_post_meta($item_meta["_product_id"][0], '_stop_mails', '1'); if (!$product_data->is_finished()) { update_post_meta($item_meta["_product_id"][0], '_auction_closed', '3'); update_post_meta($item_meta["_product_id"][0], '_buy_now', '1'); update_post_meta($item_meta["_product_id"][0], '_auction_dates_to', date('Y-m-h h:s')); do_action('woocommerce_simple_auction_close_buynow', $product_data->id); } } } } } }