/** * Add data for our newly-added custom columns. * @access public * @since 1.0.0 * @param string $column_name * @param int $id * @return void */ public function add_column_data($column_name, $id) { global $wpdb, $post; switch ($column_name) { case 'id': echo $id; break; case 'course-prerequisite': $course_prerequisite_id = get_post_meta($id, '_course_prerequisite', true); if (0 < absint($course_prerequisite_id)) { echo '<a href="' . esc_url(get_edit_post_link(absint($course_prerequisite_id))) . '" title="' . esc_attr(sprintf(__('Edit %s', 'woothemes-sensei'), get_the_title(absint($course_prerequisite_id)))) . '">' . get_the_title(absint($course_prerequisite_id)) . '</a>'; } break; case 'course-woocommerce-product': if (WooThemes_Sensei_Utils::sensei_is_woocommerce_activated()) { $course_woocommerce_product_id = get_post_meta($id, '_course_woocommerce_product', true); if (0 < absint($course_woocommerce_product_id)) { if ('product_variation' == get_post_type($course_woocommerce_product_id)) { $product_object = get_product($course_woocommerce_product_id); if (sensei_check_woocommerce_version('2.1')) { $formatted_variation = wc_get_formatted_variation($product_object->variation_data, true); } else { $formatted_variation = woocommerce_get_formatted_variation($product_object->variation_data, true); } $product_name = $product_object->parent->post->post_title . '<br/> ' . ucwords($formatted_variation); } else { $product_name = get_the_title(absint($course_woocommerce_product_id)); } // End If Statement echo '<a href="' . esc_url(get_edit_post_link(absint($course_woocommerce_product_id))) . '" title="' . esc_attr(sprintf(__('Edit %s', 'woothemes-sensei'), $product_name)) . '">' . $product_name . '</a>'; } // End If Statement } // End If Statement break; case 'course-category': $output = get_the_term_list($id, 'course-category', '', ', ', ''); if ('' == $output) { $output = __('None', 'woothemes-sensei'); } // End If Statement echo $output; break; default: break; } }
echo $woocommerce->cart->get_cart_total(); echo sprintf(_n('<mark>%d</mark>', '<mark>%d</mark>', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?> </span> </a> <?php echo '<ul class="cart_list">'; if (sizeof($woocommerce->cart->cart_contents) > 0) { foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $_product = $cart_item['data']; if ($_product->exists() && $cart_item['quantity'] > 0) { echo '<li class="cart_list_product"><a href="' . get_permalink($cart_item['product_id']) . '">'; echo $_product->get_image(); echo '</a>'; if ($_product instanceof woocommerce_product_variation && is_array($cart_item['variation'])) { echo woocommerce_get_formatted_variation($cart_item['variation']); } echo '<span class="details quantity"><a href="' . get_permalink($cart_item['product_id']) . '">' . apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product) . '</a>' . $cart_item['quantity'] . ' × ' . woocommerce_price($_product->get_price()) . '</span></li>'; } } } else { echo '<li class="empty">' . __('No products in the cart.', 'woothemes') . '</li>'; } if (sizeof($woocommerce->cart->cart_contents) > 0) { echo '<li class="total"><strong>'; if (get_option('js_prices_include_tax') == 'yes') { _e('Total', 'woothemes'); } else { _e('Subtotal', 'woothemes'); } echo ':</strong>' . $woocommerce->cart->get_cart_total();
function gtm4wp_woocommerce_datalayer_filter_items($dataLayer) { global $woocommerce, $gtm4wp_options, $wp_query, $gtm4wp_datalayer_name, $gtm4wp_product_counter; if (is_product_category() || is_product_tag() || is_front_page() || is_shop()) { if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING] || true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { if (count($woocommerce->query->filtered_product_ids) > 0) { // The following 5 lines are being borrowed from WC source $paged = max(1, $wp_query->get('paged')); $per_page = $wp_query->get('posts_per_page'); $total = $wp_query->found_posts; $first = $per_page * $paged - $per_page + 1; $last = min($total, $wp_query->get('posts_per_page') * $paged); $gtm4wp_product_counter = $first; $sumprice = 0; $product_ids = array(); $product_impressions = array(); $_temp_product_id_list = $woocommerce->query->filtered_product_ids; $i = $first - 1; foreach ($_temp_product_id_list as $itemid => $oneproductid) { $product = get_product($oneproductid); if (false === $product) { continue; } $product_price = $product->get_price(); $_product_cats = get_the_terms($product->id, 'product_cat'); if (is_array($_product_cats) && count($_product_cats) > 0) { $product_cat = array_pop($_product_cats); $product_cat = $product_cat->name; } else { $product_cat = ""; } $sumprice += $product_price; $product_ids[] = "'" . $oneproductid . "'"; $product_impressions[] = "{'name': '" . str_replace("'", "\\'", $product->get_title()) . "', 'id': '" . $oneproductid . "', 'price': '" . $product_price . "', 'category': '" . str_replace("'", "\\'", $product_cat) . "', 'position': " . ($i + 1) . " }"; $i++; if ($i > $last) { break; } } if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING]) { $dataLayer["ecomm_prodid"] = '-~-[' . implode(", ", $product_ids) . ']-~-'; $dataLayer["ecomm_pagetype"] = is_front_page() ? "home" : "category"; $dataLayer["ecomm_totalvalue"] = $sumprice; } if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { $dataLayer["ecommerce"] = "-~-{'impressions': [" . implode(", ", $product_impressions) . "]}-~-"; } } } } else { if (is_product()) { if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING] || true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { $prodid = get_the_ID(); $product = get_product($prodid); $product_price = $product->get_price(); $_product_cats = get_the_terms($product->id, 'product_cat'); if (is_array($_product_cats) && count($_product_cats) > 0) { $product_cat = array_pop($_product_cats); $product_cat = $product_cat->name; } else { $product_cat = ""; } if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING]) { $dataLayer["ecomm_prodid"] = $prodid; $dataLayer["ecomm_pagetype"] = "product"; $dataLayer["ecomm_totalvalue"] = $product_price; } if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { $dataLayer["ecommerce"] = "-~-{'detail': {'products': [{'name': '" . str_replace("'", "", get_the_title()) . "', 'id': '" . $prodid . "', 'price': '" . $product_price . "', 'category': '" . str_replace("'", "", $product_cat) . "'}]}}-~-"; } } } else { if (is_cart()) { if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { gtm4wp_woocommerce_addjs("\r\n\t\t\$('.product-remove').click(function() {\r\n\t\t\tvar productdata = \$( this )\r\n\t\t\t\t.parent()\r\n\t\t\t\t.find( '.gtm4wp_productdata' );\r\n\t\t\t\r\n\t\t\t" . $gtm4wp_datalayer_name . ".push({\r\n\t\t\t\t'event': 'gtm4wp.removeFromCart',\r\n\t\t\t\t'ecommerce': {\r\n\t\t\t\t\t'remove': {\r\n\t\t\t\t\t\t'products': [{\r\n\t\t\t\t\t\t\t'name': productdata.data( 'product_name' ),\r\n\t\t\t\t\t\t\t'id': productdata.data( 'product_id' ),\r\n\t\t\t\t\t\t\t'price': productdata.data( 'product_price' ),\r\n\t\t\t\t\t\t\t'category': productdata.data( 'product_cat' ),\r\n\t\t\t\t\t\t\t'quantity': \$( this ).parent().parent().find( '.product-quantity input.qty' ).val()\r\n\t\t\t\t\t\t}]\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t});\r\n\t\t});\r\n\t\t\t"); } if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING]) { $products = $woocommerce->cart->get_cart(); $product_ids = array(); foreach ($products as $oneproduct) { $product_ids[] = "'" . str_replace("'", "\\'", $oneproduct['product_id']) . "'"; } $dataLayer["ecomm_prodid"] = '-~-[' . implode(", ", $product_ids) . ']-~-'; $dataLayer["ecomm_pagetype"] = "cart"; $dataLayer["ecomm_totalvalue"] = $woocommerce->cart->cart_contents_total; } } else { if (is_order_received_page()) { $order_id = apply_filters('woocommerce_thankyou_order_id', empty($_GET['order']) ? $GLOBALS["wp"]->query_vars["order-received"] ? $GLOBALS["wp"]->query_vars["order-received"] : 0 : absint($_GET['order'])); $order_key = apply_filters('woocommerce_thankyou_order_key', empty($_GET['key']) ? '' : woocommerce_clean($_GET['key'])); if ($order_id > 0) { $order = new WC_Order($order_id); if ($order->order_key != $order_key) { unset($order); } } if (1 == get_post_meta($order_id, '_ga_tracked', true)) { unset($order); } if (isset($order)) { if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC]) { $dataLayer["transactionId"] = $order->get_order_number(); $dataLayer["transactionDate"] = date("c"); $dataLayer["transactionType"] = "sale"; $dataLayer["transactionAffiliation"] = get_bloginfo('name'); $dataLayer["transactionTotal"] = $order->get_total(); $dataLayer["transactionShipping"] = $order->get_total_shipping(); $dataLayer["transactionTax"] = $order->get_total_tax(); $dataLayer["transactionPaymentType"] = $order->payment_method_title; $dataLayer["transactionCurrency"] = get_woocommerce_currency(); $dataLayer["transactionShippingMethod"] = $order->get_shipping_method(); $dataLayer["transactionPromoCode"] = implode(", ", $order->get_used_coupons()); } if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { $dataLayer["ecommerce"] = "-~-{'purchase': {'actionField': {'id': '" . $order->get_order_number() . "', 'affiliation': '" . get_bloginfo('name') . "', 'revenue': '" . $order->get_total() . "', 'tax': '" . $order->get_total_tax() . "', 'shipping': '" . $order->get_total_shipping() . "'}, "; } $_products = array(); $_sumprice = 0; $_product_ids = array(); if ($order->get_items()) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); $variation_data = null; if (get_class($_product) == "WC_Product_Variation") { $variation_data = $_product->get_variation_attributes(); } if (isset($variation_data)) { $_category = woocommerce_get_formatted_variation($_product->variation_data, true); } else { $out = array(); $categories = get_the_terms($_product->id, 'product_cat'); if ($categories) { foreach ($categories as $category) { $out[] = $category->name; } } $_category = implode(" / ", $out); } $_prodprice = $order->get_item_total($item); $_products[] = array("id" => $_product->id, "name" => $item['name'], "sku" => $_product->get_sku() ? __('SKU:', GTM4WP_TEXTDOMAIN) . ' ' . $_product->get_sku() : $_product->id, "category" => $_category, "price" => $_prodprice, "currency" => get_woocommerce_currency(), "quantity" => $item['qty']); $_sumprice += $_prodprice; $_product_ids[] = "'" . $_product->id . "'"; } } if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC]) { $dataLayer["transactionProducts"] = $_products; $dataLayer["event"] = "gtm4wp.orderCompleted"; } if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { $dataLayer["ecommerce"] .= "'products': " . str_replace('"', "'", json_encode($_products)) . " }}-~-"; $dataLayer["event"] = "gtm4wp.orderCompleted"; } if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING]) { $dataLayer["ecomm_prodid"] = '-~-[' . implode(", ", $_product_ids) . ']-~-'; $dataLayer["ecomm_pagetype"] = "purchase"; $dataLayer["ecomm_totalvalue"] = $_sumprice; } // update_post_meta( $order_id, '_ga_tracked', 1 ); } } else { if (is_checkout()) { if (true === $gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC]) { foreach ($woocommerce->cart->get_cart() as $cart_item_id => $cart_item_data) { $product = $cart_item_data["data"]; $_product_cats = get_the_terms($product->id, 'product_cat'); if (is_array($_product_cats) && count($_product_cats) > 0) { $product_cat = array_pop($_product_cats); $product_cat = $product_cat->name; } else { $product_cat = ""; } $gtm4wp_checkout_products[] = "{'name': '" . str_replace("'", "\\'", $product->post->post_title) . "', 'id': '" . $product->id . "', 'price': '" . $product->get_price() . "', 'category': '" . $product_cat . "', 'quantity': '" . $cart_item_data["quantity"] . "'}"; } $dataLayer["ecommerce"] = "-~-{'checkout': {'actionField': {'step': 1}, 'products':[" . implode(", ", $gtm4wp_checkout_products) . "]}}-~-"; } } else { if ($gtm4wp_options[GTM4WP_OPTION_INTEGRATE_WCREMARKETING]) { $dataLayer["ecomm_pagetype"] = "siteview"; } } } } } } return $dataLayer; }
function ebor_dropdown() { global $woocommerce; if (is_cart()) { return; } echo '<div id="dropdowncart"> <a href="' . $woocommerce->cart->get_cart_url() . '" class="dropdowncarttrigger"><i class="icon-basket-1"></i></a>'; if (sizeof($woocommerce->cart->cart_contents) > 0) { echo ' <div class="dropdowncartcontents"> <ul class="cart_list">'; $i = 0; foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $i++; $i % 2 == 0 ? $rowclass = ' class="dark_wrapper"' : ($rowclass = ''); $_product = $cart_item['data']; if ($_product->exists() && $cart_item['quantity'] > 0) { echo '<li' . $rowclass . '><div class="dropdowncartimage">'; echo '<a href="' . get_permalink($cart_item['product_id']) . '">'; if (has_post_thumbnail($cart_item['product_id'])) { echo get_the_post_thumbnail($cart_item['product_id'], 'shop_thumbnail'); } else { echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/placeholder.png" alt="Placeholder" width="' . $woocommerce->get_image_size('shop_thumbnail_image_width') . '" height="' . $woocommerce->get_image_size('shop_thumbnail_image_height') . '" />'; } echo '</a>'; echo '</div>'; echo '<div class="dropdowncartproduct">'; echo '<a href="' . get_permalink($cart_item['product_id']) . '">'; echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product); if ($_product instanceof woocommerce_product_variation && is_array($cart_item['variation'])) { echo woocommerce_get_formatted_variation($cart_item['variation']); } echo '<span class="quantity">' . $cart_item['quantity'] . ' × ' . woocommerce_price($_product->get_price()) . '</span> </a></div><div class="clear"></div></li>'; } } echo '</ul>'; } if (sizeof($woocommerce->cart->cart_contents) > 0) { echo '<p class="total"><strong>'; if (get_option('js_prices_include_tax') == 'yes') { _e('Total', 'marble'); } else { _e('Subtotal', 'marble'); } echo ':</strong><span class="contents">' . $woocommerce->cart->get_cart_total() . '</span></p>'; do_action('woocommerce_widget_shopping_cart_before_buttons'); echo '<p class="buttons"> <a href="' . $woocommerce->cart->get_cart_url() . '" class="btn">' . __('View Cart →', 'marble') . '</a> <a href="' . $woocommerce->cart->get_checkout_url() . '" class="btn checkout">' . __('Checkout →', 'marble') . '</a> </p></div>'; } echo '</div>'; }
/** * Google Analytics eCommerce tracking * * @access public * @param mixed $order_id * @return void */ function ecommerce_tracking_code($order_id) { global $woocommerce; if ($this->disable_tracking($this->ga_eeT) || current_user_can("manage_options") || get_post_meta($order_id, "_tracked", true) == 1) { return; } $tracking_id = $this->ga_id; if (!$tracking_id) { return; } // Doing eCommerce tracking so unhook standard tracking from the footer remove_action("wp_footer", array($this, "ee_settings")); // Get the order and output tracking code $order = new WC_Order($order_id); //Get Applied Coupon Codes $coupons_list = ''; if ($order->get_used_coupons()) { $coupons_count = count($order->get_used_coupons()); $i = 1; foreach ($order->get_used_coupons() as $coupon) { $coupons_list .= $coupon; if ($i < $coupons_count) { $coupons_list .= ', '; } $i++; } } //get domain name if value is set if (!empty($this->ga_Dname)) { $set_domain_name = esc_js($this->ga_Dname); } else { $set_domain_name = "auto"; } //add display features if ($this->ga_DF) { $ga_display_feature_code = 'ga("require", "displayfeatures");'; } else { $ga_display_feature_code = ""; } //add Pageview on order page if user checked Add Standard UA code if ($this->ga_ST) { $ga_pageview = 'ga("send", "pageview");'; } else { $ga_pageview = ""; } $code = '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,"script","//www.google-analytics.com/analytics.js","ga"); ga("create", "' . esc_js($tracking_id) . '", "' . $set_domain_name . '"); ' . $ga_display_feature_code . ' ga("require", "ec", "ec.js"); ' . $ga_pageview . ' '; // Order items if ($order->get_items()) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); if (isset($_product->variation_data)) { $categories = esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)); } else { $out = array(); $categories = get_the_terms($_product->id, "product_cat"); if ($categories) { foreach ($categories as $category) { $out[] = $category->name; } } $categories = esc_js(join(",", $out)); } //orderpage Prod json $orderpage_prod_Array[get_permalink($_product->id)] = array("tvc_id" => esc_html($_product->id), "tvc_i" => esc_js($_product->get_sku() ? $_product->get_sku() : $_product->id), "tvc_n" => esc_js($item["name"]), "tvc_p" => esc_js($order->get_item_total($item)), "tvc_c" => $categories, "tvc_q" => esc_js($item["qty"])); } //make json for prod meta data on order page $this->wc_version_compare("tvc_oc=" . json_encode($orderpage_prod_Array) . ";"); } //get shipping cost based on version >2.1 get_total_shipping() < get_shipping if (version_compare($woocommerce->version, "2.1", ">=")) { $tvc_sc = $order->get_total_shipping(); } else { $tvc_sc = $order->get_shipping(); } //orderpage transcation data json $orderpage_trans_Array = array("id" => esc_js($order->get_order_number()), "affiliation" => esc_js(get_bloginfo('name')), "revenue" => esc_js($order->get_total()), "tax" => esc_js($order->get_total_tax()), "shipping" => esc_js($tvc_sc), "coupon" => $coupons_list); //make json for trans data on order page $this->wc_version_compare("tvc_td=" . json_encode($orderpage_trans_Array) . ";"); $code .= ' //set local currencies ga("set", "&cu", tvc_lc); for(var t_item in tvc_oc){ ga("ec:addProduct", { "id": tvc_oc[t_item].tvc_i, "name": tvc_oc[t_item].tvc_n, "category": tvc_oc[t_item].tvc_c, "price": tvc_oc[t_item].tvc_p, "quantity": tvc_oc[t_item].tvc_q, }); } ga("ec:setAction","purchase", { "id": tvc_td.id, "affiliation": tvc_td.affiliation, "revenue": tvc_td.revenue, "tax": tvc_td.tax, "shipping": tvc_td.shipping, "coupon": tvc_td.coupon }); ga("send", "event", "Enhanced-Ecommerce","load", "order_confirmation", {"nonInteraction": 1}); '; //check woocommerce version $this->wc_version_compare($code); update_post_meta($order_id, "_tracked", 1); }
function captiva_header_add_to_cart_fragment($fragments) { global $woocommerce; ob_start(); ?> <ul class="tiny-cart"> <li> <a class="cart_dropdown_link cart-parent" href="<?php echo $woocommerce->cart->get_cart_url(); ?> " title="<?php _e('View your shopping cart', 'captiva'); ?> "> <div class="bag-icon"></div> <span class='cart_subtotal'><?php echo $woocommerce->cart->get_cart_total(); ?> </span> <?php echo sprintf(_n('<span class="cart_itemstotal">(%d item)</span>', '<span class="cart_itemstotal">(%d items)</span>', $woocommerce->cart->cart_contents_count, 'captiva'), $woocommerce->cart->cart_contents_count); ?> </a> <?php echo '<ul class="cart_list">'; //echo '<li class="cart-title"><h3>' . __( 'Your Cart Contents', 'captiva' ) . '</h3></li>'; if (sizeof($woocommerce->cart->cart_contents) > 0) { foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $_product = $cart_item['data']; if ($_product->exists() && $cart_item['quantity'] > 0) { echo '<li class="cart_list_product"><a href="' . get_permalink($cart_item['product_id']) . '">'; echo $_product->get_image(); echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product) . '</a>'; if ($_product instanceof woocommerce_product_variation && is_array($cart_item['variation'])) { echo woocommerce_get_formatted_variation($cart_item['variation']); } echo '<span class="quantities">' . $cart_item['quantity'] . ' × ' . woocommerce_price($_product->get_price()) . '</span></li>'; } } } else { echo '<li class="empty">' . __('No products in the cart.', 'captiva') . '</li>'; } if (sizeof($woocommerce->cart->cart_contents) > 0) { echo '<li class="total"><strong>'; if (get_option('js_prices_include_tax') == 'yes') { _e('Total', 'captiva'); } else { _e('Subtotal', 'captiva'); } echo ':</strong>' . $woocommerce->cart->get_cart_total(); '</li>'; echo '<li class="buttons"><a href="' . $woocommerce->cart->get_cart_url() . '" class="button">' . __('View Cart', 'captiva') . '</a> <a href="' . $woocommerce->cart->get_checkout_url() . '" class="button checkout">' . __('Checkout', 'captiva') . '</a></li>'; } echo '</ul>'; ?> </li> </ul> <?php $fragments['ul.tiny-cart'] = ob_get_clean(); return $fragments; }
/** * get_content_plain function. * * @access public * @return string */ function get_content_plain() { $order = new WC_Order($this->object['order_id']); $item_name = WC_Subscriptions_Order::get_item_name($this->object['order_id'], $this->object['product_id']); $order_item = WC_Subscriptions_Order::get_item_by_product_id($order, $this->object['product_id']); $product = $order->get_product_from_item($order_item); if (isset($product->variation_data)) { $item_name .= ' (' . woocommerce_get_formatted_variation($product->variation_data, true) . ')'; } WC_Subscriptions_Order::$recurring_only_price_strings = true; $recurring_total = WC_Subscriptions_Order::get_formatted_order_total(0, $order); WC_Subscriptions_Order::$recurring_only_price_strings = false; if ($end_of_prepaid_term = wp_next_scheduled('scheduled_subscription_end_of_prepaid_term', array('user_id' => (int) $order->user_id, 'subscription_key' => $this->subscription_key))) { $end_of_prepaid_term = date_i18n(woocommerce_date_format(), $end_of_prepaid_term + get_option('gmt_offset') * 3600); } ob_start(); woocommerce_get_template($this->template_plain, array('subscription_key' => $this->subscription_key, 'subscription' => $this->object, 'order' => $order, 'email_heading' => $this->get_heading(), 'item_name' => $item_name, 'recurring_total' => $recurring_total, 'last_payment' => date_i18n(woocommerce_date_format(), WC_Subscriptions_Manager::get_last_payment_date($this->subscription_key, '', 'timestamp')), 'end_of_prepaid_term' => $end_of_prepaid_term), '', $this->template_base); return ob_get_clean(); }
public function woocommerce_cart_items_meta_box($post) { global $woocommerce; $order_items = (array) maybe_unserialize(get_post_meta($post->ID, 'av8_cartitems', true)); ?> <div class="woocommerce_cart_reports_items_wrapper"> <?php if (sizeof($order_items) > 0) { ?> <table cellpadding="0" width="100%" cellspacing="0" class="woocommerce_cart_reports_items"> <thead > <tr> <th class="thumb" width="20%" style="text-align:left;"><?php _e('Item', 'woocommerce'); ?> </th> <th class="sku" width="20%" style="text-align:left"><?php _e('SKU', 'woocommerce'); ?> </th> <th class="name" width="20%" style="text-align:left"><?php _e('Name', 'woocommerce'); ?> </th> <th class="price" width="20%" style="text-align:left"><?php _e('Price', 'woocommerce'); ?> </th> <?php //do_action('woocommerce_admin_order_item_headers'); ?> <th class="quantity" width="20%" style="text-align:left"><?php _e('Qty', 'woocommerce'); ?> </th> </tr> </thead> <tbody id="cart_items_list"> <?php } $loop = 0; if (sizeof($order_items) > 0) { foreach ($order_items as $item) { if (isset($item['variation_id']) && $item['variation_id'] > 0) { if (function_exists('get_product')) { $_product = get_product($item['variation_id']); } else { $_product = new WC_Product($item['variation_id']); } } else { if (function_exists('get_product')) { $_product = get_product($item['product_id']); } else { $_product = new WC_Product($item['product_id']); } } // Totals - Backwards Compatibility if ($loop % 2 == 0) { $table_color = " td1 "; } else { $table_color = " td2 "; } ?> <?php if (isset($_product) && $_product != false) { ?> <tr class="item <?php echo $table_color; ?> " rel="<?php echo $loop; ?> "> <td class="thumb" width="20%"> <a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> " class="tips" data-tip="<?php echo '<strong>' . __('Product ID:', 'woocommerce') . '</strong> ' . $_product->id; echo '<br/><strong>' . __('Variation ID:', 'woocommerce') . '</strong> '; if (isset($item->variation_id)) { echo $item->variation_id; } else { echo '-'; } echo '<br/><strong>' . __('Product SKU:', 'woocommerce') . '</strong> '; if (isset($_product->sku)) { echo $_product->sku; } else { echo '-'; } ?> "><?php echo $_product->get_image(); ?> </a> </td> <td class="sku" width="20%"> <?php if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> <input type="hidden" class="item_id" name="item_id[<?php echo $loop; ?> ]" value="<?php if (isset($item->id) && $item->id != '') { echo esc_attr($item->id); } ?> " /> <input type="hidden" name="item_name[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item->id); ?> " /> <?php if (isset($item->variation_id)) { ?> <input type="hidden" name="item_variation[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item->variation_id); ?> " /> <?php } ?> </td> <td class="name" width="20%"> <div class="row-actions"> <span class="view"><a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php _e('View product', 'woocommerce'); ?> </a> </div> <?php echo $_product->get_title(); ?> <?php if (isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> </td> <?php //do_action('woocommerce_admin_order_item_values', $_product, $item); ?> <td class="price" width="20%"><p> <?php echo $_product->get_price_html(); ?> </p></td> <td class="quantity" width="20%"><p> <?php echo $item['quantity']; ?> </p></td> </tr> <?php } ?> <?php $loop++; } } else { //Explain to the user why no products could show up in a recently abandoned / opened cart ?> <span style="color:gray;"'>No Products In The Cart</span> <?php av8_tooltip(__('When a customer adds items to a cart, then abandones the cart for a considerable amount of time, the browser often deletes the cart data. The cart still belongs to the customer, but their brower removed the products. :( But hey! This indicates that they came back. And might be ready to purchase.', 'woocommerce_cart_reports')); ?> <?php } ?> </tbody> </table> </div> <script type="text/javascript"> </script> <?php }
?> <?php echo esc_html($item['name']); ?> <?php } ?> <input type="hidden" class="order_item_id" name="order_item_id[]" value="<?php echo esc_attr($item_id); ?> " /> <?php if ($_product && isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> <table class="meta" cellspacing="0"> <tfoot> <tr> <td colspan="4"><button class="add_order_item_meta button"><?php _e('Add meta', 'woocommerce'); ?> </button></td> </tr> </tfoot> <tbody class="meta_items"> <?php if ($metadata = $order->has_meta($item_id)) { foreach ($metadata as $meta) {
/** * Order items meta box * * Displays the order items meta box - for showing individual items in the order */ function woocommerce_order_items_meta_box($post) { global $woocommerce; $order_items = (array) maybe_unserialize(get_post_meta($post->ID, '_order_items', true)); ?> <div class="woocommerce_order_items_wrapper"> <table cellpadding="0" cellspacing="0" class="woocommerce_order_items"> <thead> <tr> <th class="product-id" width="1%"><?php _e('ID', 'woothemes'); ?> </th> <th class="sku"><?php _e('SKU', 'woothemes'); ?> </th> <th class="name"><?php _e('Name', 'woothemes'); ?> </th> <th class="meta" width="1%"><?php _e('Item Meta', 'woothemes'); ?> </th> <?php do_action('woocommerce_admin_order_item_headers'); ?> <th class="quantity"><?php _e('Quantity', 'woothemes'); ?> </th> <th class="cost"><?php _e('Base Cost', 'woothemes'); ?> <a class="tips" tip="<?php _e('Cost before discounts', 'woothemes'); ?> <?php echo $woocommerce->countries->ex_tax_or_vat(); ?> . <?php _e('Up to 4 decimals are allowed for precision.', 'woothemes'); ?> " href="#">[?]</a></th> <th class="cost"><?php _e('Cost', 'woothemes'); ?> <a class="tips" tip="<?php _e('Final cost after discount', 'woothemes'); ?> <?php echo $woocommerce->countries->ex_tax_or_vat(); ?> . <?php _e('Up to 4 decimals are allowed for precision.', 'woothemes'); ?> " href="#">[?]</a></th> <th class="tax"><?php _e('Tax Rate', 'woothemes'); ?> </th> <th class="center" width="1%"><?php _e('Remove', 'woothemes'); ?> </th> </tr> </thead> <tbody id="order_items_list"> <?php $loop = 0; if (sizeof($order_items) > 0 && isset($order_items[0]['id'])) { foreach ($order_items as $item) { if (isset($item['variation_id']) && $item['variation_id'] > 0) { $_product =& new woocommerce_product_variation($item['variation_id']); } else { $_product =& new woocommerce_product($item['id']); } ?> <tr class="item" rel="<?php echo $loop; ?> "> <td class="product-id"> <img class="tips" tip="<?php echo '<strong>' . __('Product ID:', 'woothemes') . '</strong> ' . $item['id']; echo '<br/><strong>' . __('Variation ID:', 'woothemes') . '</strong> '; if ($item['variation_id']) { echo $item['variation_id']; } else { echo '-'; } echo '<br/><strong>' . __('Product SKU:', 'woothemes') . '</strong> '; if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> " src="<?php echo $woocommerce->plugin_url(); ?> /assets/images/tip.png" /> </td> <td class="sku"><?php if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> </td> <td class="name"> <a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php echo $item['name']; ?> </a> <?php if (isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> </td> <td> <table class="meta" cellspacing="0"> <tfoot> <tr> <td colspan="4"><button class="add_meta button"><?php _e('Add meta', 'woothemes'); ?> </button></td> </tr> </tfoot> <tbody class="meta_items"> <?php if (isset($item['item_meta']) && is_array($item['item_meta']) && sizeof($item['item_meta']) > 0) { foreach ($item['item_meta'] as $key => $meta) { // Backwards compatibility if (is_array($meta) && isset($meta['meta_name'])) { $meta_name = $meta['meta_name']; $meta_value = $meta['meta_value']; } else { $meta_name = $key; $meta_value = $meta; } echo '<tr><td><input type="text" name="meta_name[' . $loop . '][]" value="' . $meta_name . '" /></td><td><input type="text" name="meta_value[' . $loop . '][]" value="' . esc_attr($meta_value) . '" /></td><td><button class="remove_meta button">×</button></td></tr>'; } } ?> </tbody> </table> </td> <?php do_action('woocommerce_admin_order_item_values', $_product, $item); ?> <td class="quantity"> <input type="text" name="item_quantity[<?php echo $loop; ?> ]" placeholder="<?php _e('0', 'woothemes'); ?> " value="<?php echo esc_attr($item['qty']); ?> " /> </td> <td class="cost"> <input type="text" name="base_item_cost[<?php echo $loop; ?> ]" placeholder="<?php _e('0.00', 'woothemes'); ?> " value="<?php if (isset($item['base_cost'])) { echo esc_attr($item['base_cost']); } else { echo esc_attr($item['cost']); } ?> " /> </td> <td class="cost"> <input type="text" name="item_cost[<?php echo $loop; ?> ]" placeholder="<?php _e('0.00', 'woothemes'); ?> " value="<?php echo esc_attr($item['cost']); ?> " /> </td> <td class="tax"> <input type="text" name="item_tax_rate[<?php echo $loop; ?> ]" placeholder="<?php _e('0.0000', 'woothemes'); ?> " value="<?php echo esc_attr($item['taxrate']); ?> " /> </td> <td class="center"> <button type="button" class="remove_row button">×</button> <input type="hidden" name="item_id[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['id']); ?> " /> <input type="hidden" name="item_name[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['name']); ?> " /> <input type="hidden" name="item_variation[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['variation_id']); ?> " /> </td> </tr> <?php $loop++; } } ?> </tbody> </table> </div> <p class="buttons"> <select name="add_item_id" class="add_item_id chosen_select_nostd" data-placeholder="<?php _e('Choose an item…', 'woothemes'); ?> "> <?php echo '<option value=""></option>'; $args = array('post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'post_parent' => 0, 'order' => 'ASC', 'orderby' => 'title'); $products = get_posts($args); if ($products) { foreach ($products as $product) { $sku = get_post_meta($product->ID, 'sku', true); if ($sku) { $sku = ' SKU: ' . $sku; } echo '<option value="' . $product->ID . '">' . $product->post_title . $sku . ' (#' . $product->ID . '' . $sku . ')</option>'; $args_get_children = array('post_type' => array('product_variation', 'product'), 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title', 'post_parent' => $product->ID); if ($children_products =& get_children($args_get_children)) { foreach ($children_products as $child) { echo '<option value="' . $child->ID . '"> — ' . $child->post_title . '</option>'; } } } } ?> </select> <button type="button" class="button button-primary add_shop_order_item"><?php _e('Add item', 'woothemes'); ?> </button> </p> <p class="buttons buttons-alt"> <button type="button" class="button button calc_totals"><?php _e('Calculate totals', 'woothemes'); ?> </button> </p> <div class="clear"></div> <?php }
function woocommerce_add_order_item() { global $woocommerce; check_ajax_referer('add-order-item', 'security'); global $wpdb; $index = trim(stripslashes($_POST['index'])); $item_to_add = trim(stripslashes($_POST['item_to_add'])); $post = ''; // Find the item if (is_numeric($item_to_add)) { $post = get_post($item_to_add); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { $post_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT post_id\n\t\t\tFROM {$wpdb->posts}\n\t\t\tLEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\tWHERE {$wpdb->postmeta}.meta_key = 'sku'\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\tAND {$wpdb->posts}.post_type = 'shop_product'\n\t\t\tAND {$wpdb->postmeta}.meta_value = %s\n\t\t\tLIMIT 1\n\t\t"), $item_to_add); $post = get_post($post_id); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { die; } if ($post->post_type == "product") { $_product =& new woocommerce_product($post->ID); } else { $_product =& new woocommerce_product_variation($post->ID); } ?> <tr class="item" rel="<?php echo $index; ?> "> <td class="product-id"> <img class="tips" tip="<?php echo '<strong>' . __('Product ID:', 'woothemes') . '</strong> ' . $_product->id; echo '<br/><strong>' . __('Variation ID:', 'woothemes') . '</strong> '; if ($_product->variation_id) { echo $_product->variation_id; } else { echo '-'; } echo '<br/><strong>' . __('Product SKU:', 'woothemes') . '</strong> '; if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> " src="<?php echo $woocommerce->plugin_url(); ?> /assets/images/tip.png" /> </td> <td class="sku"><?php if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> </td> <td class="name"> <a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php echo $_product->get_title(); ?> </a> <?php if (isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> </td> <td> <table class="meta" cellspacing="0"> <tfoot> <tr> <td colspan="3"><button class="add_meta button"><?php _e('Add meta', 'woothemes'); ?> </button></td> </tr> </tfoot> <tbody class="meta_items"></tbody> </table> </td> <?php do_action('woocommerce_admin_order_item_values', $_product); ?> <td class="quantity"><input type="text" name="item_quantity[<?php echo $index; ?> ]" placeholder="0" value="1" /></td> <td class="cost"><input type="text" name="base_item_cost[<?php echo $index; ?> ]" placeholder="0.00" value="<?php echo esc_attr($_product->get_price_excluding_tax(false)); ?> " /></td> <td class="cost"><input type="text" name="item_cost[<?php echo $index; ?> ]" placeholder="0.00" value="<?php echo esc_attr($_product->get_price_excluding_tax(false)); ?> " /></td> <td class="tax"><input type="text" name="item_tax_rate[<?php echo $index; ?> ]" placeholder="0.0000" value="<?php echo esc_attr($_product->get_tax_base_rate()); ?> " /></td> <td class="center"> <input type="hidden" name="item_id[<?php echo $index; ?> ]" value="<?php echo esc_attr($_product->id); ?> " /> <input type="hidden" name="item_name[<?php echo $index; ?> ]" value="<?php echo esc_attr($_product->get_title()); ?> " /> <input type="hidden" name="item_variation[<?php echo $index; ?> ]" value="<?php if (isset($_product->variation_id)) { echo $_product->variation_id; } ?> " /> <button type="button" class="remove_row button">×</button> </td> </tr> <?php // Quit out die; }
$_product = get_product($product['id']); ?> <tr> <td class="product"><strong><a href="<?php echo esc_url(get_permalink($_product->id)); ?> "><?php echo $product['title']; ?> </a></strong> <?php if (!empty($_product->variation_id)) { echo woocommerce_get_formatted_variation($_product->variation_data); } ?> </td> <td class="qty"><?php echo $product['qty']; ?> </td> <td class="commission"><?php echo woocommerce_price($product['cost']); ?> </td> <td class="rate"><?php echo sprintf('%.2f%%', $product['commission_rate']); ?> </td>
function wc_subs_exporter_create_csv($export) { global $wc_subs_exporter; if (!$export->status) { return false; } if (empty($export->subscriptions)) { return false; } $subscription_statuses = wc_subs_exporter_get_subscription_statuses(); $fields = array(__('Order Id', 'wc-subs-exporter'), __('Order Status', 'wc-subs-exporter'), __('Subscription Status', 'wc-subs-exporter'), __('Subscription Description', 'wc-subs-exporter'), __('Subscription Start Date', 'wc-subs-exporter'), __('Subscription Expiration Date', 'wc-subs-exporter'), __('Subscription Last Payment', 'wc-subs-exporter'), __('Email', 'wc-subs-exporter'), __('Billing First Name', 'wc-subs-exporter'), __('Billing Last Name', 'wc-subs-exporter'), __('Billing Address 1', 'wc-subs-exporter'), __('Billing Address 2', 'wc-subs-exporter'), __('Billing City', 'wc-subs-exporter'), __('Billing State', 'wc-subs-exporter'), __('Billing Zip', 'wc-subs-exporter'), __('Billing Country', 'wc-subs-exporter'), __('Shipping First Name', 'wc-subs-exporter'), __('Shipping Last Name', 'wc-subs-exporter'), __('Shipping Address 1', 'wc-subs-exporter'), __('Shipping Address 2', 'wc-subs-exporter'), __('Shipping City', 'wc-subs-exporter'), __('Shipping State', 'wc-subs-exporter'), __('Shipping Zip', 'wc-subs-exporter'), __('Shipping Country', 'wc-subs-exporter'), __('Product SKU', 'wc-subs-exporter'), __('Product Description', 'wc-subs-exporter'), __('Quantity', 'wc-subs-exporter'), __('Date Ordered', 'wc-subs-exporter'), __('Coupon Code Used', 'wc-subs-exporter')); $csv = ''; if ($export->bom) { $csv .= chr(239) . chr(187) . chr(191) . ''; } foreach ($fields as $field) { $csv .= wc_subs_exporter_escape_csv_value($field, $export->delimiter, $export->escape_formatting) . $export->delimiter; } $csv = substr($csv, 0, -strlen($export->delimiter)) . "\n"; $processed_rows = 0; foreach ($export->subscriptions as $user => $user_subscriptions) { foreach ($user_subscriptions as $key => $subscription) { $order = new WC_Order($subscription['order_id']); $order_item = WC_Subscriptions_Order::get_item_by_product_id($order, $subscription['product_id']); $product = $order->get_product_from_item($order_item); if (empty($product)) { continue; } $product_sku = $product->get_sku(); $product_title = WC_Subscriptions_Order::get_item_name($subscription['order_id'], $subscription['product_id']); if (isset($product->variation_data)) { $product_description = woocommerce_get_formatted_variation($product->variation_data, true); } else { $product_description = $product_title; } $coupon_code_used = ''; $coupons = $order->get_items(array('coupon')); foreach ($coupons as $item_id => $item) { $coupon_code_used .= $item['name'] . ' '; } $start_date = substr($subscription['start_date'], 0, 10); if ($subscription['expiry_date']) { $end_date = substr($subscription['expiry_date'], 0, 10); } else { $end_date = '0000-00-00'; } if ($order->user_id > 0) { $user_info = get_userdata($order->user_id); } if (array_key_exists('completed_payments', $subscription) && is_array($subscription['completed_payments'])) { $recent_payment = substr(end($subscription['completed_payments']), 0, 10); } else { $recent_payment = ''; } $order_date = substr($order->completed_date, 0, 10); $csv .= wc_subs_exporter_escape_csv_value($order->id, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->status, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($subscription_statuses[$subscription['status']], $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($product_title, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($start_date, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($end_date, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($recent_payment, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($user_info->user_email, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_first_name, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_last_name, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_address_1, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_address_2, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_city, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_state, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_postcode, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->billing_country, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_first_name, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_last_name, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_address_1, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_address_2, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_city, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_state, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_postcode, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order->shipping_country, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($product_sku, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($product_description, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value(1, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value($order_date, $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= wc_subs_exporter_escape_csv_value(trim($coupon_code_used), $export->delimiter, $export->escape_formatting) . $export->delimiter; $csv .= "\n"; } } if (!$csv) { return false; } else { if (isset($wc_subs_exporter['debug']) && $wc_subs_exporter['debug']) { $wc_subs_exporter['debug_log'] = $csv; } else { return $csv; } } }
function woocommerce_add_order_item() { global $woocommerce, $wpdb; check_ajax_referer('add-order-item', 'security'); $index = trim(stripslashes($_POST['index'])); $item_to_add = trim(stripslashes($_POST['item_to_add'])); $post = ''; // Find the item if (is_numeric($item_to_add)) { $post = get_post($item_to_add); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { $post_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT post_id\n\t\t\tFROM {$wpdb->posts}\n\t\t\tLEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\tWHERE {$wpdb->postmeta}.meta_key = '_sku'\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\tAND {$wpdb->posts}.post_type = 'shop_product'\n\t\t\tAND {$wpdb->postmeta}.meta_value = %s\n\t\t\tLIMIT 1\n\t\t"), $item_to_add); $post = get_post($post_id); } if (!$post || $post->post_type !== 'product' && $post->post_type !== 'product_variation') { die; } if ($post->post_type == "product") { $_product = new WC_Product($post->ID); } else { $_product = new WC_Product_Variation($post->ID); } ?> <tr class="item" rel="<?php echo $index; ?> "> <td class="product-id"> <img class="tips" tip="<?php echo '<strong>' . __('Product ID:', 'woocommerce') . '</strong> ' . $_product->id; echo '<br/><strong>' . __('Variation ID:', 'woocommerce') . '</strong> '; if (isset($_product->variation_id) && $_product->variation_id) { echo $_product->variation_id; } else { echo '-'; } echo '<br/><strong>' . __('Product SKU:', 'woocommerce') . '</strong> '; if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> " src="<?php echo $woocommerce->plugin_url(); ?> /assets/images/tip.png" /> </td> <td class="sku"> <?php if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> <input type="hidden" class="item_id" name="item_id[<?php echo $index; ?> ]" value="<?php echo esc_attr($_product->id); ?> " /> <input type="hidden" name="item_name[<?php echo $index; ?> ]" value="<?php echo esc_attr($_product->get_title()); ?> " /> <input type="hidden" name="item_variation[<?php echo $index; ?> ]" value="<?php if (isset($_product->variation_id)) { echo $_product->variation_id; } ?> " /> </td> <td class="name"> <div class="row-actions"> <span class="trash"><a class="remove_row" href="#"><?php _e('Delete item', 'woocommerce'); ?> </a> | </span> <span class="view"><a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php _e('View product', 'woocommerce'); ?> </a> </div> <?php echo $_product->get_title(); ?> <?php if (isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> <table class="meta" cellspacing="0"> <tfoot> <tr> <td colspan="3"><button class="add_meta button"><?php _e('Add meta', 'woocommerce'); ?> </button></td> </tr> </tfoot> <tbody class="meta_items"></tbody> </table> </td> <?php do_action('woocommerce_admin_order_item_values', $_product); ?> <td class="tax_class" width="1%"> <select class="tax_class" name="item_tax_class[<?php echo $loop; ?> ]"> <?php $tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes')))); $classes_options = array(); $classes_options[''] = __('Standard', 'woocommerce'); if ($tax_classes) { foreach ($tax_classes as $class) { $classes_options[sanitize_title($class)] = $class; } } foreach ($classes_options as $value => $name) { echo '<option value="' . $value . '" ' . selected($value, $_product->get_tax_status(), false) . '>' . $name . '</option>'; } ?> </select> </td> <td class="quantity" width="1%"> <input type="text" name="item_quantity[<?php echo $index; ?> ]" placeholder="<?php _e('0', 'woocommerce'); ?> " value="1" size="2" class="quantity" /> </td> <td class="line_subtotal" width="1%"> <label><?php _e('Cost', 'woocommerce'); ?> : <input type="text" name="line_subtotal[<?php echo $index; ?> ]" placeholder="<?php _e('0.00', 'woocommerce'); ?> " value="<?php echo esc_attr(number_format($_product->get_price_excluding_tax(), 2, '.', '')); ?> " class="line_subtotal" /></label> <label><?php _e('Tax', 'woocommerce'); ?> : <input type="text" name="line_subtotal_tax[<?php echo $index; ?> ]" placeholder="<?php _e('0.00', 'woocommerce'); ?> " class="line_subtotal_tax" /></label> </td> <td class="line_total" width="1%"> <label><?php _e('Cost', 'woocommerce'); ?> : <input type="text" name="line_total[<?php echo $index; ?> ]" placeholder="<?php _e('0.00', 'woocommerce'); ?> " value="<?php echo esc_attr(number_format($_product->get_price_excluding_tax(), 2, '.', '')); ?> " class="line_total" /></label> <label><?php _e('Tax', 'woocommerce'); ?> : <input type="text" name="line_tax[<?php echo $index; ?> ]" placeholder="<?php _e('0.00', 'woocommerce'); ?> " class="line_tax" /></label> </td> </tr> <?php // Quit out die; }
/** * ecommerce tracking with piwik. * * @access public * @param int $order_id * @return void */ function woocommerce_ecommerce_tracking_piwik($order_id) { global $woocommerce; // Don't track admin if (current_user_can('manage_options')) { return; } // Call the Piwik ecommerce function if WP-Piwik is configured to add tracking codes to the page $wp_piwik_global_settings = get_option('wp-piwik_global-settings'); // Return if Piwik settings are not here, or if global is not set if (!isset($wp_piwik_global_settings['add_tracking_code']) || !$wp_piwik_global_settings['add_tracking_code']) { return; } if (!isset($GLOBALS['wp_piwik'])) { return; } // Get the order and get tracking code $order = new WC_Order($order_id); ob_start(); ?> try { // Add order items <?php if ($order->get_items()) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); ?> piwikTracker.addEcommerceItem( "<?php echo esc_js($_product->get_sku()); ?> ", // (required) SKU: Product unique identifier "<?php echo esc_js($item['name']); ?> ", // (optional) Product name "<?php if (isset($_product->variation_data)) { echo esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)); } ?> ", // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"] <?php echo esc_js($order->get_item_total($item)); ?> , // (recommended) Product price <?php echo esc_js($item['qty']); ?> // (optional, default to 1) Product quantity ); <?php } } ?> // Track order piwikTracker.trackEcommerceOrder( "<?php echo esc_js($order->get_order_number()); ?> ", // (required) Unique Order ID <?php echo esc_js($order->get_total()); ?> , // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount) false, // (optional) Order sub total (excludes shipping) <?php echo esc_js($order->get_total_tax()); ?> , // (optional) Tax amount <?php echo esc_js($order->get_shipping()); ?> , // (optional) Shipping amount false // (optional) Discount offered (set to false for unspecified parameter) ); } catch( err ) {} <?php $code = ob_get_clean(); $woocommerce->add_inline_js($code); }
/** * Order items meta box. * * Displays the order items meta box - for showing individual items in the order. */ function woocommerce_order_items_meta_box($post) { global $woocommerce; $order_items = (array) maybe_unserialize(get_post_meta($post->ID, '_order_items', true)); ?> <div class="woocommerce_order_items_wrapper"> <table cellpadding="0" cellspacing="0" class="woocommerce_order_items"> <thead> <tr> <th class="thumb" width="1%"><?php _e('Item', 'woocommerce'); ?> </th> <th class="sku"><?php _e('SKU', 'woocommerce'); ?> </th> <th class="name"><?php _e('Name', 'woocommerce'); ?> </th> <?php do_action('woocommerce_admin_order_item_headers'); ?> <th class="tax_class"><?php _e('Tax Class', 'woocommerce'); ?> <a class="tips" data-tip="<?php _e('Tax class for the line item', 'woocommerce'); ?> ." href="#">[?]</a></th> <th class="quantity"><?php _e('Qty', 'woocommerce'); ?> </th> <th class="line_subtotal"><?php _e('Line Subtotal', 'woocommerce'); ?> <a class="tips" data-tip="<?php _e('Line cost and line tax before pre-tax discounts', 'woocommerce'); ?> " href="#">[?]</a></th> <th class="line_total"><?php _e('Line Total', 'woocommerce'); ?> <a class="tips" data-tip="<?php _e('Line cost and line tax after pre-tax discounts', 'woocommerce'); ?> " href="#">[?]</a></th> </tr> </thead> <tbody id="order_items_list"> <?php $loop = 0; if (sizeof($order_items) > 0 && isset($order_items[0]['id'])) { foreach ($order_items as $item) { if (isset($item['variation_id']) && $item['variation_id'] > 0) { $_product = new WC_Product_Variation($item['variation_id']); } else { $_product = new WC_Product($item['id']); } // Totals - Backwards Compatibility if (!isset($item['line_total']) && isset($item['taxrate']) && isset($item['cost'])) { $item['line_tax'] = number_format($item['cost'] * $item['qty'] * ($item['taxrate'] / 100), 2, '.', ''); $item['line_total'] = $item['cost'] * $item['qty']; $item['line_subtotal_tax'] = $item['line_tax']; $item['line_subtotal'] = $item['line_total']; } ?> <tr class="item" rel="<?php echo $loop; ?> "> <td class="thumb"> <a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> " class="tips" data-tip="<?php echo '<strong>' . __('Product ID:', 'woocommerce') . '</strong> ' . $item['id']; echo '<br/><strong>' . __('Variation ID:', 'woocommerce') . '</strong> '; if ($item['variation_id']) { echo $item['variation_id']; } else { echo '-'; } echo '<br/><strong>' . __('Product SKU:', 'woocommerce') . '</strong> '; if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> "><?php echo $_product->get_image(); ?> </a> </td> <td class="sku" width="1%"> <?php if ($_product->sku) { echo $_product->sku; } else { echo '-'; } ?> <input type="hidden" class="item_id" name="item_id[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['id']); ?> " /> <input type="hidden" name="item_name[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['name']); ?> " /> <input type="hidden" name="item_variation[<?php echo $loop; ?> ]" value="<?php echo esc_attr($item['variation_id']); ?> " /> </td> <td class="name"> <div class="row-actions"> <span class="trash"><a class="remove_row" href="#"><?php _e('Delete item', 'woocommerce'); ?> </a> | </span> <span class="view"><a href="<?php echo esc_url(admin_url('post.php?post=' . $_product->id . '&action=edit')); ?> "><?php _e('View product', 'woocommerce'); ?> </a> </div> <?php echo $item['name']; ?> <?php if (isset($_product->variation_data)) { echo '<br/>' . woocommerce_get_formatted_variation($_product->variation_data, true); } ?> <table class="meta" cellspacing="0"> <tfoot> <tr> <td colspan="4"><button class="add_meta button"><?php _e('Add meta', 'woocommerce'); ?> </button></td> </tr> </tfoot> <tbody class="meta_items"> <?php if (isset($item['item_meta']) && is_array($item['item_meta']) && sizeof($item['item_meta']) > 0) { foreach ($item['item_meta'] as $key => $meta) { // Backwards compatibility if (is_array($meta) && isset($meta['meta_name'])) { $meta_name = $meta['meta_name']; $meta_value = $meta['meta_value']; } else { $meta_name = $key; $meta_value = $meta; } echo '<tr><td><input type="text" name="meta_name[' . $loop . '][]" value="' . esc_attr($meta_name) . '" /></td><td><input type="text" name="meta_value[' . $loop . '][]" value="' . esc_attr($meta_value) . '" /></td><td width="1%"><button class="remove_meta button">×</button></td></tr>'; } } ?> </tbody> </table> </td> <?php do_action('woocommerce_admin_order_item_values', $_product, $item); ?> <td class="tax_class" width="1%"> <select class="tax_class" name="item_tax_class[<?php echo $loop; ?> ]" title="<?php _e('Tax class', 'woocommerce'); ?> "> <?php $item_value = isset($item['tax_class']) ? sanitize_title($item['tax_class']) : ''; $tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes')))); $classes_options = array(); $classes_options[''] = __('Standard', 'woocommerce'); if ($tax_classes) { foreach ($tax_classes as $class) { $classes_options[sanitize_title($class)] = $class; } } foreach ($classes_options as $value => $name) { echo '<option value="' . $value . '" ' . selected($value, $item_value, false) . '>' . $name . '</option>'; } ?> </select> </td> <td class="quantity" width="1%"> <input type="text" name="item_quantity[<?php echo $loop; ?> ]" placeholder="0" value="<?php echo esc_attr($item['qty']); ?> " size="2" class="quantity" /> </td> <td class="line_subtotal" width="1%"> <label><?php _e('Cost', 'woocommerce'); ?> : <input type="text" name="line_subtotal[<?php echo $loop; ?> ]" placeholder="0.00" value="<?php if (isset($item['line_subtotal'])) { echo esc_attr($item['line_subtotal']); } ?> " class="line_subtotal" /></label> <label><?php _e('Tax', 'woocommerce'); ?> : <input type="text" name="line_subtotal_tax[<?php echo $loop; ?> ]" placeholder="0.00" value="<?php if (isset($item['line_subtotal_tax'])) { echo esc_attr($item['line_subtotal_tax']); } ?> " class="line_subtotal_tax" /></label> </td> <td class="line_total" width="1%"> <label><?php _e('Cost', 'woocommerce'); ?> : <input type="text" name="line_total[<?php echo $loop; ?> ]" placeholder="0.00" value="<?php if (isset($item['line_total'])) { echo esc_attr($item['line_total']); } ?> " class="line_total" /></label> <label><?php _e('Tax', 'woocommerce'); ?> : <input type="text" name="line_tax[<?php echo $loop; ?> ]" placeholder="0.00" value="<?php if (isset($item['line_tax'])) { echo esc_attr($item['line_tax']); } ?> " class="line_tax" /></label> </td> </tr> <?php $loop++; } } ?> </tbody> </table> </div> <p class="buttons"> <select id="add_item_id" name="add_item_id[]" class="ajax_chosen_select_products_and_variations" multiple="multiple" data-placeholder="<?php _e('Search for a product…', 'woocommerce'); ?> " style="width: 400px"></select> <button type="button" class="button add_shop_order_item"><?php _e('Add item(s)', 'woocommerce'); ?> </button> </p> <p class="buttons buttons-alt"> <button type="button" class="button calc_line_taxes"><?php _e('Calc line tax ↑', 'woocommerce'); ?> </button> <button type="button" class="button calc_totals"><?php _e('Calc totals →', 'woocommerce'); ?> </button> </p> <div class="clear"></div> <?php }
/** * Outputs the content for each column. * * @param array $item A singular item (one full row's worth of data) * @param array $column_name The name/slug of the column to be processed * @return string Text or HTML to be placed inside the column <td> * @since 1.0 */ public function column_default($item, $column_name) { global $woocommerce; $current_gmt_time = gmdate('U'); switch ($column_name) { case 'status': $actions = array(); $action_url = add_query_arg(array('page' => $_REQUEST['page'], 'user' => $item['user_id'], 'subscription' => $item['subscription_key'], '_wpnonce' => wp_create_nonce($item['subscription_key']))); if (isset($_REQUEST['status'])) { $action_url = add_query_arg(array('status' => $_REQUEST['status']), $action_url); } $order = new WC_Order($item['order_id']); $all_statuses = array('active' => __('Reactivate', WC_Subscriptions::$text_domain), 'on-hold' => __('Suspend', WC_Subscriptions::$text_domain), 'cancelled' => __('Cancel', WC_Subscriptions::$text_domain), 'trash' => __('Trash', WC_Subscriptions::$text_domain), 'deleted' => __('Delete Permanently', WC_Subscriptions::$text_domain)); foreach ($all_statuses as $status => $label) { if (WC_Subscriptions_Manager::can_subscription_be_changed_to($status, $item['subscription_key'], $item['user_id'])) { $action = 'deleted' == $status ? 'delete' : $status; // For built in CSS $actions[$action] = sprintf('<a href="%s">%s</a>', add_query_arg('new_status', $status, $action_url), $label); } } if ($item['status'] == 'pending') { unset($actions['active']); unset($actions['trash']); } elseif (!in_array($item['status'], array('cancelled', 'expired', 'suspended'))) { unset($actions['trash']); } $actions = apply_filters('woocommerce_subscriptions_list_table_actions', $actions, $item); $column_content = sprintf('<mark class="%s">%s</mark> %s', sanitize_title($item[$column_name]), WC_Subscriptions_Manager::get_status_to_display($item[$column_name], $item['subscription_key'], $item['user_id']), $this->row_actions($actions)); $column_content = apply_filters('woocommerce_subscriptions_list_table_column_status_content', $column_content, $item, $actions, $this); break; case 'title': //Return the title contents $column_content = sprintf('<a href="%s">%s</a>', get_edit_post_link($item['product_id']), WC_Subscriptions_Order::get_item_name($item['order_id'], $item['product_id'])); $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', 'product_id', $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item['product_id']); $order = new WC_Order($item['order_id']); $order_item = WC_Subscriptions_Order::get_item_by_product_id($order, $item['product_id']); $product = $order->get_product_from_item($order_item); if (isset($product->variation_data)) { $column_content .= '<br />' . woocommerce_get_formatted_variation($product->variation_data, true); } break; case 'order_id': $order = new WC_Order($item[$column_name]); $column_content = sprintf('<a href="%1$s">%2$s</a>', get_edit_post_link($item[$column_name]), sprintf(__('Order %s', WC_Subscriptions::$text_domain), $order->get_order_number())); $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', $column_name, $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item[$column_name]); break; case 'user': $user = get_user_by('id', $item['user_id']); $column_content = sprintf('<a href="%s">%s</a>', admin_url('user-edit.php?user_id=' . $user->ID), ucfirst($user->display_name)); $column_content .= sprintf('<input type="hidden" class="%1$s" name="%2$s[%3$s][%4$s][][%1$s]" value="%5$s" />', 'user_id', $this->_args['plural'], $item['user_id'], $item['subscription_key'], $item['user_id']); break; case 'start_date': case 'expiry_date': case 'end_date': if ($column_name == 'expiry_date' && $item[$column_name] == 0) { $column_content = __('Never', WC_Subscriptions::$text_domain); } else { if ($column_name == 'end_date' && $item[$column_name] == 0) { $column_content = __('Not yet ended', WC_Subscriptions::$text_domain); } else { $gmt_timestamp = strtotime($item[$column_name]); $user_timestamp = $gmt_timestamp + get_option('gmt_offset') * 3600; $column_content = sprintf('<time title="%s">%s</time>', esc_attr($gmt_timestamp), date_i18n(get_option('date_format'), $user_timestamp)); } } break; case 'trial_expiry_date': $trial_expiration = WC_Subscriptions_Manager::get_trial_expiration_date($item['subscription_key'], $item['user_id'], 'timestamp'); if (empty($trial_expiration)) { $column_content = '-'; } else { $column_content = sprintf('<time title="%s">%s</time>', esc_attr($trial_expiration), date_i18n(get_option('date_format'), $trial_expiration + get_option('gmt_offset') * 3600)); } break; case 'last_payment_date': if (empty($item['completed_payments'])) { $column_content = '-'; } else { $last_payment_timestamp = strtotime(array_pop($item['completed_payments'])); $time_diff = $current_gmt_time - $last_payment_timestamp; if ($time_diff > 0 && $time_diff < 7 * 24 * 60 * 60) { $last_payment = sprintf(__('%s ago', WC_Subscriptions::$text_domain), human_time_diff($last_payment_timestamp, $current_gmt_time)); } else { $last_payment = date_i18n(get_option('date_format'), $last_payment_timestamp + get_option('gmt_offset') * 3600); } $column_content = sprintf('<time title="%s">%s</time>', esc_attr($last_payment_timestamp), $last_payment); } break; case 'next_payment_date': $next_payment_timestamp = WC_Subscriptions_Manager::get_next_payment_date($item['subscription_key'], $item['user_id'], 'timestamp'); if ($next_payment_timestamp == 0) { $column_content = '-'; } else { // Convert to site time $time_diff = $next_payment_timestamp - $current_gmt_time; if ($time_diff > 0 && $time_diff < 7 * 24 * 60 * 60) { $next_payment = sprintf(__('In %s', WC_Subscriptions::$text_domain), human_time_diff($current_gmt_time, $next_payment_timestamp)); } else { $next_payment = date_i18n(get_option('date_format'), $next_payment_timestamp + get_option('gmt_offset') * 3600); } $column_content = sprintf('<time class="next-payment-date" title="%s">%s</time>', esc_attr($next_payment_timestamp), $next_payment); if (WC_Subscriptions_Manager::can_subscription_be_changed_to('new-payment-date', $item['subscription_key'], $item['user_id'])) { $column_content .= '<div class="edit-date-div row-actions hide-if-no-js">'; $column_content .= '<img class="date-picker-icon" src="' . admin_url('images/date-button.gif') . '" title="Date Picker Icon"/>'; $column_content .= '<a href="#edit_timestamp" class="edit-timestamp" tabindex="4">' . __('Change', WC_Subscriptions::$text_domain) . '</a>'; $column_content .= '<div class="date-picker-div hide-if-js">'; $column_content .= WC_Subscriptions_Manager::touch_time(array('date' => date('Y-m-d', $next_payment_timestamp), 'echo' => false, 'multiple' => true, 'include_time' => false)); $column_content .= '</div>'; $column_content .= '</form>'; } } break; case 'renewal_order_count': $count = WC_Subscriptions_Renewal_Order::get_renewal_order_count($item['order_id']); $column_content = sprintf('<a href="%1$s">%2$d</a>', admin_url('edit.php?post_status=all&post_type=shop_order&_renewal_order_parent_id=' . absint($item['order_id'])), $count); break; } return $column_content; }
/** * Returns a 'category' JSON line based on $product * @param object $product Product to pull info for * @return string Line of JSON */ private static function product_get_category_line($_product) { if (is_array($_product->variation_data) && !empty($_product->variation_data)) { $code = "'" . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . "',"; } else { $out = array(); $categories = get_the_terms($_product->id, 'product_cat'); if ($categories) { foreach ($categories as $category) { $out[] = $category->name; } } $code = "'" . esc_js(join("/", $out)) . "',"; } return $code; }
/** * ecommerce tracking with piwik */ function woocommerce_ecommerce_tracking_piwik($order_id) { global $woocommerce; if (is_admin()) { return; } // Don't track admin // Call the Piwik ecommerce function if WP-Piwik is configured to add tracking codes to the page $wp_piwik_global_settings = get_option('wp-piwik_global-settings'); // Return if Piwik settings are not here, or if global is not set if (!isset($wp_piwik_global_settings['add_tracking_code']) || !$wp_piwik_global_settings['add_tracking_code']) { return; } if (!isset($GLOBALS['wp_piwik'])) { return; } // Remove WP-Piwik from wp_footer and run it here instead, to get Piwik // loaded *before* we do our ecommerce tracking calls remove_action('wp_footer', array($GLOBALS['wp_piwik'], 'footer')); $GLOBALS['wp_piwik']->footer(); // Get the order and output tracking code $order = new WC_Order($order_id); ?> <script type="text/javascript"> try { // Add order items <?php if ($order->get_items()) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); ?> piwikTracker.addEcommerceItem( "<?php echo $_product->sku; ?> ", // (required) SKU: Product unique identifier "<?php echo $item['name']; ?> ", // (optional) Product name "<?php if (isset($_product->variation_data)) { echo woocommerce_get_formatted_variation($_product->variation_data, true); } ?> ", // (optional) Product category. You can also specify an array of up to 5 categories eg. ["Books", "New releases", "Biography"] <?php echo $item['line_cost'] / $item['qty']; ?> , // (recommended) Product price <?php echo $item['qty']; ?> // (optional, default to 1) Product quantity ); <?php } } ?> // Track order piwikTracker.trackEcommerceOrder( "<?php echo $order_id; ?> ", // (required) Unique Order ID <?php echo $order->order_total; ?> , // (required) Order Revenue grand total (includes tax, shipping, and subtracted discount) false, // (optional) Order sub total (excludes shipping) <?php echo $order->get_total_tax(); ?> , // (optional) Tax amount <?php echo $order->get_shipping(); ?> , // (optional) Shipping amount false // (optional) Discount offered (set to false for unspecified parameter) ); } catch( err ) {} </script> <?php }
function sm_woo_get_packing_slip($purchase_ids, $purchase_id_arr) { if (!empty($purchase_ids) && !empty($purchase_id_arr)) { ?> <style type="text/css"> body { font-family:"Helvetica Neue", Helvetica, Arial, Verdana, sans-serif; } h1 span { font-size:0.75em; } h2 { color: #333; } .no-page-break { page-break-after: avoid; } #wrapper { margin:0 auto; width:95%; page-break-after: always; } #wrapper_last { margin:0 auto; width:95%; page-break-after: avoid; } .address{ width:98%; border-top:1px; border-right:1px; margin:1em auto; border-collapse:collapse; } .address_border{ border-bottom:1px; border-left:1px ; padding:.2em 1em; text-align:left; } table { width:98%; border-top:1px solid #e5eff8; border-right:1px solid #e5eff8; margin:1em auto; border-collapse:collapse; font-size:10pt; } td { border-bottom:1px solid #e5eff8; border-left:1px solid #e5eff8; padding:.3em 1em; text-align:center; } tr.odd td, tr.odd .column1 { background:#f4f9fe url(background.gif) no-repeat; } .column1 { background:#f4f9fe; } thead th { background:#f4f9fe; text-align:center; font:bold 1.2em/2em "Century Gothic","Trebuchet MS",Arial,Helvetica,sans-serif; } .datagrid { position: relative; top:-30pt; } .producthead{ text-align: left; } .pricehead{ text-align: right; } .sm_address_div{ position: relative; left:28pt; } .sm_email_span{ position: relative; left:10pt; } </style> <?php $counter = 0; foreach ($purchase_id_arr as $purchase_id_value) { $order = new WC_Order($purchase_id_value); $date_format = get_option('date_format'); if (is_plugin_active('woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php')) { $purchase_display_id = isset($order->order_custom_fields['_order_number_formatted'][0]) ? $order->order_custom_fields['_order_number_formatted'][0] : $purchase_id_value; } else { $purchase_display_id = $purchase_id_value; } $counter++; if (count($purchase_id_arr) == $counter) { echo '<div id="wrapper_last">'; } else { echo '<div id="wrapper">'; } echo smart_manager_print_logo(); echo '<div style="margin-top:-0.8em;">'; echo '<h4 style="font:bold 1.2em/2em "Century Gothic","Trebuchet MS",Arial,Helvetica,sans-serif; position:relative; 12pt;"> ' . get_bloginfo('name') . '</h4>'; echo '<table class="address" style="position:relative; top:-22pt; left:-35pt;">'; echo '<tr><td class="address_border" colspan="2" valign="top" width="50%"><span style="position:relative; left:27pt; top:10pt;"> <b>Order # ' . $purchase_display_id . ' - ' . date($date_format, strtotime($order->order_date)) . '</b></span><br/></td></tr>'; echo '<tr><td class="address_border" width="35%" align="center"><br/><div class="sm_address_div">'; $formatted_billing_address = $order->get_formatted_billing_address(); if ($formatted_billing_address != '') { echo '<b>' . __('Billing Address', 'smart-manager') . '</b><p>'; echo $formatted_billing_address; echo '</p></td>'; } $formatted_shipping_address = $order->get_formatted_shipping_address(); if ($formatted_shipping_address != '') { echo '<td class="address_border" width="30%"><br/><div style="position:relative; top:3pt;"><b>' . __('Shipping Address', 'smart-manager') . '</b><p>'; echo $formatted_shipping_address; echo '</p></div></td>'; } echo '</tr>'; echo '<tr><td colspan="2" class="address_border"><span class="sm_email_span"><table class="address"><tr><td colspan="2" class="address_border" > <b>' . __('Email id', 'smart-manager') . ':</b> ' . $order->billing_email . '</td></tr> <tr><td class="address_border"><b>' . __('Tel', 'smart-manager') . ' :</b> ' . $order->billing_phone . '</td></tr></table> </span></td></tr>'; echo '</table>'; echo '<div class="datagrid"><table><tr class="column1"> <td class="producthead">' . __('Product', 'smart-manager') . '</td><td>' . __('SKU', 'smart-manager') . '</td> <td>' . __('Quantity', 'smart-manager') . '</td><td class="pricehead">' . __('Price', 'smart-manager') . '</td></tr>'; foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); $sku = $variation = ''; $sku = $_product->get_sku(); $formatted_variation = woocommerce_get_formatted_variation($_product->variation_data, true); $variation = !empty($formatted_variation) ? ' (' . $formatted_variation . ')' : ''; $item_total = $_product->get_price() * $item['item_meta']['_qty'][0]; $total_order += $item_total; echo '<tr><td class="producthead">'; echo $item['name'] . $variation; echo '</td><td>' . $sku . '</td><td>'; echo $item['item_meta']['_qty'][0]; echo '</td><td class="pricehead">'; echo woocommerce_price($item_total); echo '</td></tr>'; } echo '<tr><td colspan="2" rowspan="5" class="address_border" valign="top"><br/> <i>' . ($order->customer_note != '' ? __('Order Notes', 'smart-manager') . ' : ' . $order->customer_note : '') . '</i></td><td style="text-align:right;" class="address_border" valign="top"> <b>Subtotal </b></td><td class="pricehead">' . $order->get_subtotal_to_display() . '</td></tr>'; echo '<tr><td style="text-align:right;" class="address_border"><b>' . __('Shipping', 'smart-manager') . ' </b></td><td class="pricehead">' . $order->get_shipping_to_display() . '</td></tr>'; if ($order->cart_discount > 0) { echo '<tr><td style="text-align:right;" class="address_border">' . __('Cart Discount', 'smart-manager') . '</td><td style="text-align:right;">'; echo woocommerce_price($order->cart_discount); echo '</td></tr>'; } if ($order->order_discount > 0) { echo '<tr><td style="text-align:right;" class="address_border"><b>' . __('Order Discount', 'smart-manager') . ' </b></td>'; echo '<td class="pricehead">' . woocommerce_price($order->order_discount) . '</td></tr>'; } echo '<tr><td style="text-align:right;" class="address_border"><b>' . __('Tax', 'smart-manager') . ' </b></td><td class="pricehead">' . woocommerce_price($order->get_total_tax()) . '</td></tr>'; echo '<tr><td class="column1" style="text-align:right;"><b>' . __('Total', 'smart-manager') . ' </b></td><td class="column1" style="text-align:right;">' . woocommerce_price($order->order_total) . ' -via ' . $order->payment_method_title . '</td></tr>'; echo '</table></div></div></div>'; } } exit; }
function get_data_woo($post, $offset, $limit, $is_export = false) { global $wpdb, $woocommerce, $post_status, $parent_sort_id, $order_by, $post_type, $variation_name, $from_variation, $parent_name, $attributes; $_POST = $post; // Fix: PHP 5.4 $products = array(); // getting the active module $active_module = isset($_POST['active_module']) ? $_POST['active_module'] : 'Products'; // $active_module = $_POST ['active_module']; variation_query_params(); // Restricting LIMIT for export CSV if ($is_export === true) { $limit_string = ""; $image_size = "full"; } else { $limit_string = "LIMIT {$offset},{$limit}"; $image_size = "thumbnail"; } $wpdb->query("SET SESSION group_concat_max_len=999999"); // To increase the max length of the Group Concat Functionality $view_columns = json_decode(stripslashes($_POST['viewCols'])); if ($active_module == 'Products') { // <-products $tax_status = array('taxable' => __('Taxable', 'smart-manager'), 'shipping' => __('Shipping only', 'smart-manager'), 'none' => __('None', 'smart-manager')); if (isset($_POST['incVariation']) && $_POST['incVariation'] === 'true') { $show_variation = true; } else { $parent_name = ''; $post_status = "('publish', 'draft')"; $post_type = "('product')"; $parent_sort_id = ''; $order_by = " ORDER BY products.id desc"; $show_variation = false; } // if max-join-size issue occurs $query = "SET SQL_BIG_SELECTS=1;"; $wpdb->query($query); //Query for getting all the distinct attribute meta key names $query_variation = "SELECT DISTINCT meta_key as variation\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key like 'attribute_%'"; $variation = $wpdb->get_col($query_variation); //Query to get all the distinct term names along with their slug names $query = "SELECT terms.slug as slug, terms.name as term_name FROM {$wpdb->prefix}terms AS terms\n\t\t\t\t\tJOIN {$wpdb->prefix}postmeta AS postmeta ON ( postmeta.meta_value = terms.slug AND postmeta.meta_key LIKE 'attribute_%' ) GROUP BY terms.slug"; $attributes_terms = $wpdb->get_results($query, 'ARRAY_A'); $attributes = array(); foreach ($attributes_terms as $attributes_term) { $attributes[$attributes_term['slug']] = $attributes_term['term_name']; } //Query to get the term_taxonomy_id for all the product categories $query_terms = "SELECT terms.name, wt.term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy AS wt\n JOIN {$wpdb->prefix}terms AS terms ON (wt.term_id = terms.term_id)\n WHERE wt.taxonomy like 'product_cat'"; $results = $wpdb->get_results($query_terms, 'ARRAY_A'); $rows_terms = $wpdb->num_rows; if (!empty($results)) { for ($i = 0; $i < sizeof($results); $i++) { $term_taxonomy_id[$i] = $results[$i]['term_taxonomy_id']; $term_taxonomy[$results[$i]['term_taxonomy_id']] = $results[$i]['name']; } //Imploding the term_taxonomy_id to be used in the main query of the products module $term_taxonomy_id_query = "AND wtr.term_taxonomy_id IN (" . implode(",", $term_taxonomy_id) . ")"; } else { $term_taxonomy_id_query = ''; } $results_trash = array(); //Code to get the ids of all the products whose post_status is thrash $query_trash = "SELECT ID FROM {$wpdb->prefix}posts \n WHERE post_status = 'trash'\n AND post_type IN ('product')"; $results_trash = $wpdb->get_col($query_trash); $rows_trash = $wpdb->num_rows; $query_deleted = "SELECT distinct products.post_parent \n FROM {$wpdb->prefix}posts as products \n WHERE NOT EXISTS (SELECT * FROM {$wpdb->prefix}posts WHERE ID = products.post_parent) \n AND products.post_parent > 0 \n AND products.post_type = 'product_variation'"; $results_deleted = $wpdb->get_col($query_deleted); $rows_deleted = $wpdb->num_rows; for ($i = sizeof($results_trash), $j = 0; $j < sizeof($results_deleted); $i++, $j++) { $results_trash[$i] = $results_deleted[$j]; } if ($rows_trash > 0 || $rows_deleted > 0) { $trash_id = " AND products.post_parent NOT IN (" . implode(",", $results_trash) . ")"; } else { $trash_id = ""; } // Query to delete the unwanted '_regular_price' meta_key from variations if ($_POST['SM_IS_WOO16'] == "true") { $query_delete_variations = "DELETE FROM {$wpdb->prefix}postmeta \n WHERE meta_key = '_regular_price'\n AND post_id IN (SELECT id FROM {$wpdb->prefix}posts\n WHERE post_parent > 0\n AND post_type IN ('product_variation')\n AND post_status IN ('publish', 'draft'))"; $wpdb->query($query_delete_variations); } //Code to get the attribute terms for all attributes $query_attribute_names = "SELECT terms.name AS attribute_terms,\n taxonomy.taxonomy as attribute_name,\n taxonomy.term_taxonomy_id as term_taxonomy_id\n FROM {$wpdb->prefix}terms as terms\n JOIN {$wpdb->prefix}term_taxonomy as taxonomy ON (taxonomy.term_id = terms.term_id)\n WHERE taxonomy.taxonomy LIKE 'pa_%'\n GROUP BY taxonomy.taxonomy, taxonomy.term_taxonomy_id"; $results_attribute_names = $wpdb->get_results($query_attribute_names, 'ARRAY_A'); $product_attributes = array(); $temp_attribute_nm = ""; foreach ($results_attribute_names as $results_attribute_name) { if ($results_attribute_name['attribute_name'] != $temp_attribute_nm) { $product_attributes[$results_attribute_name['attribute_name']] = array(); } $product_attributes[$results_attribute_name['attribute_name']][$results_attribute_name['term_taxonomy_id']] = $results_attribute_name['attribute_terms']; $temp_attribute_nm = $results_attribute_name['attribute_name']; } $query_attribute_label = "SELECT attribute_name, attribute_label\n FROM {$wpdb->prefix}woocommerce_attribute_taxonomies"; $results_attribute_label = $wpdb->get_results($query_attribute_label, 'ARRAY_A'); $attributes_label = array(); foreach ($results_attribute_label as $results_attribute_label1) { $attributes_label['pa_' . $results_attribute_label1['attribute_name']] = $results_attribute_label1['attribute_label']; } //Query to get the Category Ids $query_categories = "SELECT products.id as id,\n GROUP_CONCAT(distinct wtr.term_taxonomy_id order by wtr.object_id SEPARATOR '###') AS term_taxonomy_id\n FROM {$wpdb->prefix}posts as products\n JOIN {$wpdb->prefix}term_relationships as wtr ON (products.id = wtr.object_id)\n WHERE products.post_status IN {$post_status}\n AND products.post_type IN {$post_type}\n {$trash_id}\n GROUP BY id"; $records_categories = $wpdb->get_results($query_categories, 'ARRAY_A'); $category_ids_all = array(); foreach ($records_categories as $records_category) { $category_ids_all[$records_category['id']] = $records_category['term_taxonomy_id']; } // GROUP_CONCAT(distinct wtr.term_taxonomy_id order by wtr.object_id SEPARATOR '###') AS term_taxonomy_id, $select = "SELECT SQL_CALC_FOUND_ROWS products.id,\n\t\t\t\t\tproducts.post_title,\n products.post_title as post_title_search,\n\t\t\t\t\tproducts.post_content,\n\t\t\t\t\tproducts.post_excerpt,\n\t\t\t\t\tproducts.post_status,\n\t\t\t\t\tproducts.post_parent,\n\t\t\t\t\tGROUP_CONCAT(prod_othermeta.meta_key order by prod_othermeta.meta_id SEPARATOR '###') AS prod_othermeta_key,\n\t\t\t\t\tGROUP_CONCAT(prod_othermeta.meta_value order by prod_othermeta.meta_id SEPARATOR '###') AS prod_othermeta_value\n\t\t\t\t\t{$parent_sort_id}"; //Used as an alternative to the SQL_CALC_FOUND_ROWS function of MYSQL Database $select_count = "SELECT COUNT(*) as count"; // To get the count of the number of rows generated from the above select query $search = ""; $search_condn = ""; if (isset($_POST['searchText']) && $_POST['searchText'] != '') { $search_on = trim($_POST['searchText']); $search = ""; $product_type = wp_get_object_terms($records[$i]['id'], 'product_type', array('fields' => 'slugs')); $count_all_double_quote = substr_count($search_on, '"'); if ($count_all_double_quote > 0) { $search_ons = array_filter(array_map('trim', explode($wpdb->_real_escape('"'), $search_on))); $search_on = implode(",", $search_ons); } else { $search_on = $wpdb->_real_escape($search_on); $search_ons = explode(' ', $search_on); } //Function to prepare the conditions for the query function prepare_cond($search_ons, $column_nm) { $cond = ""; foreach ($search_ons as $search_on) { $cond .= $column_nm . " LIKE '%" . $search_on . "%'"; $cond .= " OR "; } return substr($cond, 0, -3); } //Query for getting the slug name for the term name typed in the search box of the products module $query_terms = "SELECT slug FROM {$wpdb->prefix}terms WHERE (" . prepare_cond($search_ons, "name") . ") AND name IN ('" . implode("','", $attributes) . "');"; $records_slug = $wpdb->get_col($query_terms); $rows = $wpdb->num_rows; $search_text = $search_ons; // if($rows > 0){ if ($rows > 0 && !empty($records_slug)) { $search_text = $records_slug; } //Query to get the term_taxonomy_id for the category name typed in the search text box of the products module $query_category = "SELECT tr.object_id FROM {$wpdb->prefix}term_relationships AS tr\n JOIN {$wpdb->prefix}term_taxonomy AS wt ON (wt.term_taxonomy_id = tr.term_taxonomy_id)\n JOIN {$wpdb->prefix}terms AS terms ON (wt.term_id = terms.term_id)\n WHERE wt.taxonomy like 'product_cat'\n AND (" . prepare_cond($search_ons, "terms.name") . ")"; $results_category = $wpdb->get_col($query_category); $rows_category = $wpdb->num_rows; if ($rows_category > 0) { $search_category = " OR products.ID IN (" . implode(",", $results_category) . ") OR products.post_parent IN (" . implode(",", $results_category) . ")"; } else { $search_category = ""; } //Query to get the post id if title or status or content or excerpt matches $query_title = "SELECT ID FROM {$wpdb->prefix}posts \n WHERE post_type IN ('product')\n AND (" . prepare_cond($search_ons, "post_title") . "OR " . prepare_cond($search_ons, "post_status") . "OR " . prepare_cond($search_ons, "post_content") . "OR " . prepare_cond($search_ons, "post_excerpt") . ")"; $results_title = $wpdb->get_col($query_title); $rows_title = $wpdb->num_rows; if ($rows_title > 0) { $search_title = " OR products.ID IN (" . implode(",", $results_title) . ") OR products.post_parent IN (" . implode(",", $results_title) . ")"; } else { $search_title = ""; } $visible = stristr("Catalog & Search", $search_on); if ($visible === FALSE) { $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta \n WHERE meta_key IN ('_tax_status','_visibility')\n AND meta_value LIKE '%{$search_on}%'"; } else { if (count($search_ons) > 1) { $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta \n WHERE meta_key IN ('_visibility')\n AND (meta_value LIKE '%visible%')"; } else { $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta \n WHERE meta_key IN ('_visibility')\n AND (meta_value LIKE '%visible%'\n OR meta_value LIKE '%{$search_on}%')"; } } $results_tax_visible = $wpdb->get_col($query_tax_visible); $rows_tax_visible = $wpdb->num_rows; if ($rows_tax_visible > 0) { $search_tax_visible = " OR products.ID IN (" . implode(",", $results_tax_visible) . ") OR products.post_parent IN (" . implode(",", $results_tax_visible) . ")"; } else { $search_tax_visible = ""; } if (is_array($search_ons) && count($search_ons) >= 1) { $search_condn = " HAVING "; foreach ($search_ons as $search_on) { $search_condn .= " (concat(' ',REPLACE(REPLACE(post_title_search,'(',''),')','')) LIKE '%{$search_on}%'\n OR post_content LIKE '%{$search_on}%'\n OR post_excerpt LIKE '%{$search_on}%'\n\n OR prod_othermeta_value LIKE '%{$search_on}%')\n\n "; $search_condn .= " OR"; } if ($rows == 1) { $query_ids1 = "SELECT GROUP_CONCAT(post_id ORDER BY post_id SEPARATOR ',') as id FROM {$wpdb->prefix}postmeta WHERE meta_value IN ('" . implode("','", $search_text) . "') AND meta_key like 'attribute_%'"; $records_id1 = implode(",", $wpdb->get_col($query_ids1)); $search_condn .= " products.id IN ({$records_id1})"; $search_condn .= " OR"; } $search_condn_count = " AND(" . substr($search_condn_count, 0, -2) . ")"; $search_condn = substr($search_condn, 0, -2); $search_condn .= $search_title . $search_category . $search_tax_visible; } else { $search_condn = " HAVING concat(' ',REPLACE(REPLACE(post_title_search,'(',''),')','')) LIKE '%{$search_on}%'\n OR post_content LIKE '%{$search_on}%'\n OR post_excerpt LIKE '%{$search_on}%'\n\n OR prod_othermeta_value LIKE '%{$search_on}%'\n\n {$search_title}\n {$search_category}\n {$search_tax_visible}\n "; if ($rows == 1) { $query_ids1 = "SELECT GROUP_CONCAT(post_id ORDER BY post_id SEPARATOR ',') as id FROM {$wpdb->prefix}postmeta WHERE meta_value IN ('" . implode("','", $search_text) . "') AND meta_key like 'attribute_%';"; $records_id1 = implode(",", $wpdb->get_col($query_ids1)); if (!empty($records_id1)) { $search_condn .= " OR products.id IN ({$records_id1})"; } } } } // LEFT JOIN {$wpdb->prefix}term_relationships as wtr ON (products.id = wtr.object_id // )"; // Removed $term_taxonomy_id_query as it was conflicting with Attributes Column $from = "FROM {$wpdb->prefix}posts as products\n\t\t\t\t\t\tJOIN {$wpdb->prefix}postmeta as prod_othermeta ON (prod_othermeta.post_id = products.id and\n\t\t\t\t\t\tprod_othermeta.meta_key IN ('_regular_price','_sale_price','_sale_price_dates_from','_sale_price_dates_to','_sku','_stock','_weight','_height','_length','_width','_price','_thumbnail_id','_tax_status','_min_variation_regular_price','_min_variation_sale_price','_visibility','_product_attributes','" . implode("','", $variation) . "') )"; $where = " WHERE products.post_status IN {$post_status}\n\t\t\t\t\t\tAND products.post_type IN {$post_type}\n {$trash_id}\n {$search}"; $group_by = " GROUP BY products.id "; //Query for getting the actual data loaded into the smartManager $query = "{$select} {$from} {$where} {$group_by} {$search_condn} {$order_by} {$limit_string};"; $records = $wpdb->get_results($query, 'ARRAY_A'); $num_rows = $wpdb->num_rows; //Query for getting the count of the number of products loaded into the smartManager $recordcount_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $recordcount_result[0]['count']; if ($num_rows <= 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', 'smart-manager'); } else { for ($i = 0; $i < $num_rows; $i++) { $records[$i]['post_content'] = str_replace('"', '\'', $records[$i]['post_content']); $records[$i]['post_excerpt'] = str_replace('"', '\'', $records[$i]['post_excerpt']); $prod_meta_values = explode('###', $records[$i]['prod_othermeta_value']); $prod_meta_key = explode('###', $records[$i]['prod_othermeta_key']); if (count($prod_meta_values) != count($prod_meta_key)) { continue; } unset($records[$i]['prod_othermeta_value']); unset($records[$i]['prod_othermeta_key']); $prod_meta_key_values = array_combine($prod_meta_key, $prod_meta_values); $product_type = wp_get_object_terms($records[$i]['id'], 'product_type', array('fields' => 'slugs')); // Code to get the Category Name from the term_taxonomy_id if (isset($category_ids_all[$records[$i]['id']])) { //$category_id = explode('###', $records[$i]['term_taxonomy_id']); $category_names = ""; // unset($records[$i]['term_taxonomy_id']); $category_id = explode('###', $category_ids_all[$records[$i]['id']]); for ($j = 0; $j < sizeof($category_id); $j++) { if (isset($term_taxonomy[$category_id[$j]])) { $category_names .= $term_taxonomy[$category_id[$j]] . ', '; } } if ($category_names != "") { $category_names = substr($category_names, 0, -2); $records[$i]['category'] = $category_names; } } else { $records[$i]['category'] = ""; } $records[$i]['category'] = $records[$i]['post_parent'] > 0 && $product_type[0] == 'simple' || $records[$i]['post_parent'] == 0 ? $records[$i]['category'] : ''; // To hide category name from Product's variations //Attributes Column if (isset($prod_meta_key_values['_product_attributes']) && $prod_meta_key_values['_product_attributes'] != "") { $prod_attr = unserialize($prod_meta_key_values['_product_attributes']); $attributes_list = ""; foreach ($prod_attr as $prod_attr1) { $attribute_terms = ""; if (isset($attributes_label[$prod_attr1['name']]) && isset($product_attributes[$prod_attr1['name']])) { foreach ($category_id as $category_id1) { if (isset($product_attributes[$prod_attr1['name']][$category_id1])) { $attribute_terms .= $product_attributes[$prod_attr1['name']][$category_id1] . ', '; } } if ($attribute_terms != "") { $attribute_terms = substr($attribute_terms, 0, -2); } $attributes_list .= $attributes_label[$prod_attr1['name']] . ": [" . $attribute_terms . "]"; } else { $attributes_list .= $prod_attr1['name'] . ": [" . str_replace(" |", ",", $prod_attr1['value']) . "]"; } if ($attributes_list != "") { $attributes_list .= "<br>"; } } // $records[$i]['product_attributes'] = substr( $attributes_list, 0, -3); $records[$i]['product_attributes'] = $attributes_list; } else { $records[$i]['product_attributes'] = ""; } if (isset($prod_meta_key_values['_sale_price_dates_from']) && !empty($prod_meta_key_values['_sale_price_dates_from'])) { $prod_meta_key_values['_sale_price_dates_from'] = date('Y-m-d', (int) $prod_meta_key_values['_sale_price_dates_from']); } if (isset($prod_meta_key_values['_sale_price_dates_to']) && !empty($prod_meta_key_values['_sale_price_dates_to'])) { $prod_meta_key_values['_sale_price_dates_to'] = date('Y-m-d', (int) $prod_meta_key_values['_sale_price_dates_to']); } $records[$i] = array_merge((array) $records[$i], $prod_meta_key_values); $thumbnail = isset($records[$i]['_thumbnail_id']) ? wp_get_attachment_image_src($records[$i]['_thumbnail_id'], $image_size) : ''; $records[$i]['thumbnail'] = $thumbnail[0] != '' ? $thumbnail[0] : false; $records[$i]['_tax_status'] = !empty($prod_meta_key_values['_tax_status']) ? $prod_meta_key_values['_tax_status'] : ''; // Setting product type for grouped products if ($records[$i]['post_parent'] != 0) { $product_type_parent = wp_get_object_terms($records[$i]['post_parent'], 'product_type', array('fields' => 'slugs')); if ($product_type_parent[0] == "grouped") { $records[$i]['product_type'] = $product_type_parent[0]; } } else { $records[$i]['product_type'] = $product_type[0]; } if ($show_variation === true) { if ($records[$i]['post_parent'] != 0 && $product_type_parent[0] != "grouped") { $records[$i]['post_status'] = get_post_status($records[$i]['post_parent']); if ($_POST['SM_IS_WOO16'] == "true") { $records[$i]['_regular_price'] = $records[$i]['_price']; } $variation_names = ''; foreach ($variation as $slug) { $variation_names .= isset($attributes[$prod_meta_key_values[$slug]]) && !empty($attributes[$prod_meta_key_values[$slug]]) ? $attributes[$prod_meta_key_values[$slug]] . ', ' : ucfirst($prod_meta_key_values[$slug]) . ', '; } $records[$i]['post_title'] = get_the_title($records[$i]['post_parent']) . " - " . trim($variation_names, ", "); } else { if ($records[$i]['post_parent'] == 0 && $product_type[0] == 'variable') { $records[$i]['_regular_price'] = ""; $records[$i]['_sale_price'] = ""; } else { $records[$i]['_regular_price'] = trim($records[$i]['_regular_price']); if (empty($records[$i]['_regular_price'])) { $records[$i]['_regular_price'] = $records[$i]['_price']; } } } $products[$records[$i]['id']]['post_title'] = $records[$i]['post_title']; $products[$records[$i]['id']]['variation'] = $variation_names; } elseif ($show_variation === false && SMPRO) { if ($product_type[0] == 'variable') { $records[$i]['_regular_price'] = $records[$i]['_min_variation_regular_price']; $records[$i]['_sale_price'] = $records[$i]['_min_variation_sale_price']; } else { $records[$i]['_regular_price'] = trim($records[$i]['_regular_price']); // if ( empty( $records[$i]['_regular_price'] ) ) { // $records[$i]['_regular_price'] = $records[$i]['_price']; // } $records[$i]['_sale_price'] = trim($records[$i]['_sale_price']); } } else { $records[$i]['_regular_price'] = $records[$i]['_regular_price']; $records[$i]['_sale_price'] = $records[$i]['_sale_price']; } unset($records[$i]['prod_othermeta_value']); unset($records[$i]['prod_othermeta_key']); } } } elseif ($active_module == 'Customers') { //BOF Customer's module $search_condn = customers_query($_POST['searchText']); $query_terms = "SELECT id FROM {$wpdb->prefix}posts AS posts\n JOIN {$wpdb->prefix}term_relationships AS term_relationships \n ON term_relationships.object_id = posts.ID \n JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n JOIN {$wpdb->prefix}terms AS terms \n ON term_taxonomy.term_id = terms.term_id\n WHERE terms.name IN ('completed','processing','on-hold','pending')\n AND posts.post_status IN ('publish')"; $terms_post = implode(",", $wpdb->get_col($query_terms)); //Query for getting the max of post id for all the Guest Customers $query_post_guest = "SELECT post_ID FROM {$wpdb->prefix}postmeta\n WHERE meta_key ='_customer_user' AND meta_value=0\n AND post_id IN ({$terms_post})"; $post_id_guest = $wpdb->get_col($query_post_guest); $num_guest = $wpdb->num_rows; if ($num_guest > 0) { $query_max_id = "SELECT GROUP_CONCAT(distinct postmeta1.post_ID \n ORDER BY posts.post_date DESC SEPARATOR ',' ) AS all_id,\n GROUP_CONCAT(postmeta2.meta_value \n ORDER BY posts.post_date DESC SEPARATOR ',' ) AS order_total, \n date_format(max(posts.post_date),'%b %e %Y, %r') AS date,\n count(postmeta1.post_id) as count,\n sum(postmeta2.meta_value) as total\n \n FROM {$wpdb->prefix}postmeta AS postmeta1\n JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta1.post_id)\n INNER JOIN {$wpdb->prefix}postmeta AS postmeta2\n ON (postmeta2.post_ID = postmeta1.post_ID AND postmeta2.meta_key IN ('_order_total'))\n\n WHERE postmeta1.meta_key IN ('_billing_email')\n AND postmeta1.post_ID IN (" . implode(",", $post_id_guest) . ") \n GROUP BY postmeta1.meta_value\n ORDER BY date desc"; $result_max_id = $wpdb->get_results($query_max_id, 'ARRAY_A'); } //Query for getting the max of post id for all the Registered Customers $query_post_user = "******"; $post_id_user = $wpdb->get_col($query_post_user); $num_user = $wpdb->num_rows; if ($num_user > 0) { $query_max_user = "******" . implode(",", $post_id_user) . ") \n GROUP BY postmeta1.meta_value\n ORDER BY date"; $result_max_user = $wpdb->get_results($query_max_user, 'ARRAY_A'); } //Code for generating the total orders, count of orders , max ids and last order total arrays for ($i = 0; $i < sizeof($result_max_id); $i++) { $temp = explode(",", $result_max_id[$i]['all_id']); $max_ids[$i] = $temp[0]; $order_count[$max_ids[$i]] = $result_max_id[$i]['count']; $order_total[$max_ids[$i]] = $result_max_id[$i]['total']; //Code for getting the last Order Total $temp = explode(",", $result_max_id[$i]['order_total']); $last_order_total[$max_ids[$i]] = $temp[0]; } if (!empty($result_max_id)) { $j = sizeof($max_ids); $k = sizeof($order_count); $l = sizeof($order_total); $m = sizeof($last_order_total); } for ($i = 0; $i < sizeof($result_max_user); $i++, $j++, $k++, $l++, $m++) { $temp = explode(",", $result_max_user[$i]['all_id']); $max_ids[$j] = $temp[0]; $order_count[$max_ids[$j]] = $result_max_user[$i]['count']; $order_total[$max_ids[$j]] = $result_max_user[$i]['total']; $temp = explode(",", $result_max_user[$i]['order_total']); $last_order_total[$max_ids[$j]] = $temp[0]; } $max_id = implode(",", $max_ids); $customers_query = "SELECT SQL_CALC_FOUND_ROWS\n DISTINCT(GROUP_CONCAT( postmeta.meta_value\n ORDER BY postmeta.meta_id SEPARATOR '###' ) )AS meta_value,\n GROUP_CONCAT(distinct postmeta.meta_key\n ORDER BY postmeta.meta_id SEPARATOR '###' ) AS meta_key,\n date_format(max(posts.post_date),'%b %e %Y, %r') AS date,\n posts.ID AS id\n\n FROM {$wpdb->prefix}posts AS posts\n RIGHT JOIN {$wpdb->prefix}postmeta AS postmeta\n ON (posts.ID = postmeta.post_id AND postmeta.meta_key IN\n ('_billing_first_name' , '_billing_last_name' , '_billing_email',\n '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_state',\n '_billing_country','_billing_postcode', '_billing_phone','_customer_user'))"; $where = " WHERE posts.post_type LIKE 'shop_order' \n\t\t\t\t\t AND posts.post_status IN ('publish')\n\t\t\t\t\t AND posts.ID IN ({$max_id})"; $group_by = " GROUP BY posts.ID"; $limit_query = " ORDER BY FIND_IN_SET(posts.ID,'{$max_id}') {$limit_string}"; $query = "{$customers_query} {$where} {$group_by} {$search_condn} {$limit_query};"; $result = $wpdb->get_results($query, 'ARRAY_A'); $num_rows = $wpdb->num_rows; //To get Total count $customers_count_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $customers_count_result[0]['count']; if ($num_records == 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', 'smart-manager'); } else { $postmeta = array(); $j = 0; $k = 0; for ($i = 0; $i < sizeof($result); $i++) { $meta_value = explode('###', $result[$i]['meta_value']); $meta_key = explode('###', $result[$i]['meta_key']); //note: while merging the array, $data as to be the second arg if (count($meta_key) == count($meta_value)) { $temp[$i] = array_combine($meta_key, $meta_value); } if ($temp[$i]['_customer_user'] == 0) { $postmeta[$j] = $temp[$i]; $j++; } elseif ($temp[$i]['_customer_user'] > 0) { $user[$k] = $temp[$i]['_customer_user']; $k++; } unset($meta_value); unset($meta_key); } //Query for getting th Registered Users data from wp_usermeta and wp_users table if (!is_null($user)) { $user_ids = implode(",", $user); $query_users = "SELECT users.ID,users.user_email,\n GROUP_CONCAT( usermeta.meta_value ORDER BY usermeta.umeta_id SEPARATOR '###' ) AS meta_value,\n GROUP_CONCAT(distinct usermeta.meta_key\n ORDER BY usermeta.umeta_id SEPARATOR '###_' ) AS meta_key\n FROM {$wpdb->users} AS users\n JOIN {$wpdb->usermeta} AS usermeta\n ON (users.ID = usermeta.user_id AND usermeta.meta_key IN\n ('billing_first_name' , 'billing_last_name' , 'billing_email',\n 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state',\n 'billing_country','billing_postcode', 'billing_phone'))\n WHERE users.ID IN ({$user_ids})\n GROUP BY users.ID\n ORDER BY FIND_IN_SET(users.ID,'{$user_ids}');"; $result_users = $wpdb->get_results($query_users, 'ARRAY_A'); $num_rows_users = $wpdb->num_rows; for ($i = 0, $j = sizeof($postmeta); $i < sizeof($result_users); $i++, $j++) { $meta_value = explode('###', $result_users[$i]['meta_value']); $result_users[$i]['meta_key'] = "_" . $result_users[$i]['meta_key']; $meta_key = explode('###', $result_users[$i]['meta_key']); //note: while merging the array, $data as to be the second arg if (count($meta_key) == count($meta_value)) { $postmeta[$j] = array_combine($meta_key, $meta_value); $postmeta[$j]['_customer_user'] = $result_users[$i]['ID']; $postmeta[$j]['_billing_email'] = $result_users[$i]['user_email']; } unset($meta_value); unset($meta_key); } } $user_id = array(); for ($i = 0; $i < sizeof($postmeta); $i++) { if ($postmeta[$i]['_customer_user'] == 0) { $user_email[$i] = "'" . $postmeta[$i]['_billing_email'] . "'"; } elseif ($postmeta[$i]['_customer_user'] > 0) { $user_id[$i] = $postmeta[$i]['_customer_user']; } } for ($i = 0; $i < sizeof($postmeta); $i++) { $postmeta[$i]['id'] = $max_ids[$i]; if (SMPRO == true) { $result[$i]['_order_total'] = $last_order_total[$result[$i]['id']]; $postmeta[$i]['count_orders'] = $order_count[$result[$i]['id']]; $postmeta[$i]['total_orders'] = $order_total[$result[$i]['id']]; $result[$i]['last_order'] = $result[$i]['date']; } else { $postmeta[$i]['count_orders'] = 'Pro only'; $postmeta[$i]['total_orders'] = 'Pro only'; $result[$i]['_order_total'] = 'Pro only'; $result[$i]['last_order'] = 'Pro only'; } $result[$i]['_billing_address'] = isset($postmeta[$i]['_billing_address_1']) ? $postmeta[$i]['_billing_address_1'] . ', ' . $postmeta[$i]['_billing_address_2'] : $postmeta[$i]['_billing_address_2']; $postmeta[$i]['_billing_state'] = isset($woocommerce->countries->states[$postmeta[$i]['_billing_country']][$postmeta[$i]['_billing_state']]) ? $woocommerce->countries->states[$postmeta[$i]['_billing_country']][$postmeta[$i]['_billing_state']] : $postmeta[$i]['_billing_state']; $postmeta[$i]['_billing_country'] = isset($woocommerce->countries->countries[$postmeta[$i]['_billing_country']]) ? $woocommerce->countries->countries[$postmeta[$i]['_billing_country']] : $postmeta[$i]['_billing_country']; unset($result[$i]['date']); unset($result[$i]['meta_key']); unset($result[$i]['meta_value']); unset($postmeta[$i]['_billing_address_1']); unset($postmeta[$i]['_billing_address_2']); //NOTE: storing old email id in an extra column in record so useful to indentify record with emailid during updates. if ($postmeta[$i]['_billing_email'] != '' || $postmeta[$i]['_billing_email'] != null) { $records[] = array_merge($postmeta[$i], $result[$i]); } } } unset($result); unset($postmeta); } elseif ($active_module == 'Orders') { if (SMPRO == true && function_exists('sm_woo_get_packing_slip') && $_POST['label'] == 'getPurchaseLogs') { $log_ids_arr = json_decode(stripslashes($_POST['log_ids'])); if (is_array($log_ids_arr)) { $log_ids = implode(', ', $log_ids_arr); } sm_woo_get_packing_slip($log_ids, $log_ids_arr); } //Code to get all the term_names along with the term_taxonomy_id in an array $query_terms = "SELECT terms.name,term_taxonomy.term_taxonomy_id \n FROM {$wpdb->prefix}term_taxonomy AS term_taxonomy\n JOIN {$wpdb->prefix}terms AS terms ON terms.term_id = term_taxonomy.term_id\n WHERE taxonomy LIKE 'shop_order_status'"; $terms = $wpdb->get_results($query_terms, 'ARRAY_A'); for ($i = 0; $i < sizeof($terms); $i++) { $terms_name[$terms[$i]['term_taxonomy_id']] = $terms[$i]['name']; $terms_id[$i] = $terms[$i]['term_taxonomy_id']; } $terms_post = implode(",", $terms_id); //Code for Sequential Orders compatibility if (is_plugin_active('woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php')) { $order_formatted = ", '_order_number_formatted'"; } else { $order_formatted = ""; } $select_query = "SELECT SQL_CALC_FOUND_ROWS posts.ID as id,\n posts.post_excerpt as order_note,\n\t\t\t\t\t\t\t\tdate_format(posts.post_date,'%b %e %Y, %r') AS date,\n\t\t\t\t\t\t\t\tGROUP_CONCAT( postmeta.meta_value \n\t\t\t\t\t\t\t\tORDER BY postmeta.meta_id\n\t\t\t\t\t\t\t\tSEPARATOR '###' ) AS meta_value,\n\t\t\t\t\t\t\t\tGROUP_CONCAT(distinct postmeta.meta_key\n\t\t\t\t\t\t\t\tORDER BY postmeta.meta_id \n\t\t\t\t\t\t\t\tSEPARATOR '###' ) AS meta_key,\n\t\t\t\t\t\t\t\tterm_relationships.term_taxonomy_id AS term_taxonomy_id\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}posts AS posts \n\t\t\t\t\t\t\t\t\tJOIN {$wpdb->prefix}term_relationships AS term_relationships \n\t\t\t\t\t\t\t\t\t\t\tON term_relationships.object_id = posts.ID \n\t\t\t\t\t\t\t\t\tRIGHT JOIN {$wpdb->prefix}postmeta AS postmeta \n\t\t\t\t\t\t\t\t\t\t\tON (posts.ID = postmeta.post_id AND postmeta.meta_key IN \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t('_billing_first_name' , '_billing_last_name' , '_billing_email',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'_shipping_first_name', '_shipping_last_name', '_shipping_address_1', '_shipping_address_2',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'_shipping_city', '_shipping_state', '_shipping_country','_shipping_postcode',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'_shipping_method', '_payment_method', '_order_items', '_order_total',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'_shipping_method_title', '_payment_method_title','_customer_user','_billing_phone',\n '_order_shipping', '_order_discount', '_cart_discount', '_order_tax', '_order_shipping_tax', '_order_currency', 'coupons'" . $order_formatted . "))"; $group_by = " GROUP BY posts.ID"; $limit_query = " ORDER BY posts.ID DESC {$limit_string} ;"; $where = " WHERE posts.post_type LIKE 'shop_order' \n\t\t\t\t\tAND posts.post_status IN ('publish','draft','auto-draft')\n AND term_relationships.term_taxonomy_id IN ({$terms_post})"; if (isset($_POST['fromDate'])) { $from_date = date('Y-m-d H:i:s', (int) strtotime($_POST['fromDate'])); $date_start = date('Y-m-d', (int) strtotime($_POST['fromDate'])); $date = date('Y-m-d', (int) strtotime($_POST['toDate'])); if ($date_start == $date && $date == date('Y-m-d')) { $curr_time_gmt = date('H:i:s', time() - date("Z")); $new_date = $date . " " . $curr_time_gmt; $to_date = date('Y-m-d H:i:s', (int) strtotime($new_date) + get_option('gmt_offset') * HOUR_IN_SECONDS); } else { $to_date = $date . " 23:59:59"; } if (SMPRO == true) { $where .= " AND posts.post_date BETWEEN '{$from_date}' AND '{$to_date}'"; } } if (isset($_POST['searchText']) && $_POST['searchText'] != '') { $multiple_search_terms = explode('\\"', trim($_POST['searchText'])); $search_on = $wpdb->_real_escape(trim($_POST['searchText'])); //Query for getting the user_id based on the email enetered in the Search Box $query_user_email = "SELECT id FROM {$wpdb->prefix}users \n WHERE user_email like '%{$search_on}%'"; $result_user_email = $wpdb->get_col($query_user_email); $num_rows_email = $wpdb->num_rows; if ($num_rows_email == 0) { $query_user_email = "SELECT DISTINCT p2.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value > 0\n AND p1.meta_value like '%{$search_on}%'"; $result_user_email = $wpdb->get_col($query_user_email); $num_rows_email1 = $wpdb->num_rows; } //Query for getting the user_id based on the Customer phone number enetered in the Search Box $query_user_phone = "SELECT user_id FROM {$wpdb->prefix}usermeta \n WHERE meta_key='billing_phone' \n AND meta_value like '%{$search_on}%'"; $result_user_phone = $wpdb->get_col($query_user_phone); $num_rows_phone = $wpdb->num_rows; if ($num_rows_phone == 0) { $query_user_phone = "SELECT DISTINCT p2.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_phone'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value > 0\n AND p1.meta_value like '%{$search_on}%'"; $result_user_phone = $wpdb->get_col($query_user_phone); $num_rows_phone1 = $wpdb->num_rows; } $query_terms = "SELECT term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy\n WHERE term_id IN (SELECT term_id FROM {$wpdb->prefix}terms"; // name like '%$search_on%') // $multiple_search_terms = explode( '\"', $search_on ); if (!empty($multiple_search_terms)) { $query_terms .= " WHERE"; foreach ($multiple_search_terms as $search_status) { $search_status = trim($search_status); if (!empty($search_status)) { $query_terms .= " name like '%{$search_status}%' OR"; } } $query_terms = trim($query_terms, ' OR'); } $query_terms .= ")"; $result_terms = implode(",", $wpdb->get_col($query_terms)); $num_terms = $wpdb->num_rows; // Start: Query for searching product names in order if ($_POST['SM_IS_WOO16'] == "false") { $query_product_names = "SELECT order_id\n FROM {$wpdb->prefix}woocommerce_order_items"; if (!empty($multiple_search_terms)) { $query_product_names .= " WHERE"; foreach ($multiple_search_terms as $product_name) { $product_name = trim($product_name); if (!empty($product_name)) { $query_product_names .= " order_item_name LIKE '%{$product_name}%' OR"; } } $query_product_names = trim($query_product_names, ' OR'); } } else { $query_product_names = "SELECT post_id\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key LIKE '%_order_items%'"; if (!empty($multiple_search_terms)) { $query_product_names .= " AND ("; foreach ($multiple_search_terms as $product_name) { $product_name = trim($product_name); if (!empty($product_name)) { $query_product_names .= " meta_value LIKE '%{$product_name}%' OR"; } } $query_product_names = trim($query_product_names, ' OR'); $query_product_names .= ")"; } } $result_product_ids = $wpdb->get_col($query_product_names); $num_product_ids = $wpdb->num_rows; // End: Query for searching product names in order //Query to get the post_id of the products whose sku code matches with the one type in the search text box of the Orders Module $query_sku = "SELECT post_id FROM {$wpdb->prefix}postmeta\n WHERE meta_key = '_sku'\n AND meta_value like '%{$search_on}%'"; $result_sku = $wpdb->get_col($query_sku); $rows_sku = $wpdb->num_rows; //Code for handling the Search functionality of the Orders Module using the SKU code of the product if ($rows_sku > 0) { if ($_POST['SM_IS_WOO16'] == "false") { $query_order_by_sku = "SELECT order_id\n FROM {$wpdb->prefix}woocommerce_order_items AS woocommerce_order_items\n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woocommerce_order_itemmeta USING ( order_item_id )\n WHERE woocommerce_order_itemmeta.meta_key IN ( '_product_id', '_variation_id' )\n AND woocommerce_order_itemmeta.meta_value IN ( " . implode(',', $result_sku) . " )"; $results_order_by_sku = $wpdb->get_col($query_order_by_sku); $num_order_by_sku = $wpdb->num_rows; if ($num_order_by_sku > 0) { $search_condn = " HAVING id IN ( " . implode(',', $results_order_by_sku) . " )"; } } else { //Query for getting all the distinct attribute meta key names $query_variation = "SELECT DISTINCT meta_key as variation\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key like 'attribute_%'"; $variation = $wpdb->get_col($query_variation); //Query to get all the product title's as displayed in the products module along wih the post_id and SKU code in an array $query_product = "SELECT posts.id, posts.post_title, posts.post_parent, \n GROUP_CONCAT( postmeta.meta_value \n ORDER BY postmeta.meta_id\n SEPARATOR ',' ) AS meta_value\n FROM {$wpdb->prefix}posts AS posts\n JOIN {$wpdb->prefix}postmeta AS postmeta\n ON (posts.ID = postmeta.post_id\n AND postmeta.meta_key IN ('_sku','" . implode("','", $variation) . "'))\n GROUP BY posts.id"; $result_product = $wpdb->get_results($query_product, 'ARRAY_A'); //Code to store all the products title in an array with the post_id as the array index for ($i = 0; $i < sizeof($result_product); $i++) { $product_title[$result_product[$i]['id']]['post_title'] = $result_product[$i]['post_title']; $product_title[$result_product[$i]['id']]['variation_title'] = $result_product[$i]['meta_value']; $product_title[$result_product[$i]['id']]['post_parent'] = $result_product[$i]['post_parent']; } $post_title = array(); $variation_title = array(); $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_sku); $i++) { $product_type = wp_get_object_terms($result_sku[$i], 'product_type', array('fields' => 'slugs')); // Getting the type of the product //Code to prepare the search condition for the search using SKU Code if ($product_title[$result_sku[$i]]['post_parent'] == 0) { $post_title[$i] = $product_title[$result_sku[$i]]['post_title']; $search_condn .= " meta_value like '%s:4:\"name\"%\"{$post_title[$i]}\"%' "; $search_condn .= "OR"; } elseif ($product_title[$result_sku[$i]]['post_parent'] > 0) { $temp = explode(",", $product_title[$result_sku[$i]]['variation_title']); $post_title[$i] = $product_title[$product_title[$result_sku[$i]]['post_parent']]['post_title']; $search_condn .= " meta_value like '%s:4:\"name\"%\"{$post_title[$i]}\"%' "; $search_condn .= "AND ("; for ($j = 1; $j < sizeof($temp); $j++) { $search_condn .= " meta_value like '%s:10:\"meta_value\"%\"{$temp[$j]}\"%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2) . ")"; $search_condn .= "OR"; } } $variation_title = array_unique($variation_title); $search_condn = substr($search_condn, 0, -2); } } elseif ($num_product_ids > 0) { $search_condn = " HAVING id IN ( " . implode(',', $result_product_ids) . " )"; } elseif ($num_rows_email > 0) { // Query to bring the matching email of the Guest uers $query = "SELECT DISTINCT p1.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value = 0\n AND p1.meta_value like '%{$search_on}%'"; $result_email_guest = $wpdb->get_col($query); $rows_email_guest = $wpdb->num_rows; $query_email = "SELECT DISTINCT(p1.meta_value)\n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value IN (" . implode(",", $result_user_email) . ")"; $result_email = $wpdb->get_col($query_email); if ($rows_email_guest > 0) { for ($i = 0, $j = sizeof($result_email); $i < sizeof($result_email_guest); $i++, $j++) { $result_email[$j] = $result_email_guest[$i]; } } $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_email); $i++) { $search_condn .= " meta_value like '%{$result_email[$i]}%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2); } elseif ($num_rows_phone > 0) { // Query to bring the matching Phone No. of the Guest uers $query = "SELECT DISTINCT p1.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_phone'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value = 0\n AND p1.meta_value like '%{$search_on}%'"; $result_phone_guest = $wpdb->get_col($query); $rows_phone_guest = $wpdb->num_rows; $query_phone = "SELECT DISTINCT(p1.meta_value)\n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value IN (" . implode(",", $result_user_phone) . ")"; $result_phone = $wpdb->get_col($query_phone); if ($rows_phone_guest > 0) { for ($i = 0, $j = sizeof($result_phone); $i < sizeof($result_phone_guest); $i++, $j++) { $result_phone[$j] = $result_phone_guest[$i]; } } $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_phone); $i++) { $search_condn .= " meta_value like '%{$result_phone[$i]}%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2); } elseif ($num_rows_email1 > 0 || $num_rows_phone1 > 0) { $search_condn = " HAVING id = 0"; } elseif ($num_terms > 0) { $search_condn = " HAVING term_taxonomy_id IN ({$result_terms})"; } else { $search_condn = " HAVING id like '{$search_on}%'\n\t\t\t\t\t\t\t\t OR date like '%{$search_on}%'\n\t\t\t\t\t\t\t\t OR meta_value like '%{$search_on}%'"; } } //get the state id if the shipping state is numeric or blank $query = "{$select_query} {$where} {$group_by} {$search_condn} {$limit_query}"; $results = $wpdb->get_results($query, 'ARRAY_A'); //To get the total count $orders_count_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $orders_count_result[0]['count']; //Query to get the email id from the wp_users table for the Registered Customers $query_users = "SELECT users.ID,users.user_email,usermeta.meta_value\n FROM {$wpdb->prefix}users AS users, {$wpdb->prefix}usermeta AS usermeta\n WHERE usermeta.user_id = users.id \n AND usermeta.meta_key = 'billing_phone'\n GROUP BY users.ID"; $result_users = $wpdb->get_results($query_users, 'ARRAY_A'); if ($num_records == 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', 'smart-manager'); } else { foreach ($results as $data) { $order_ids[] = $data['id']; } if ($_POST['SM_IS_WOO16'] == "false") { $order_id = implode(",", $order_ids); $query_order_items = "SELECT order_items.order_item_id,\n order_items.order_id ,\n order_items.order_item_name AS order_prod,\n GROUP_CONCAT(order_itemmeta.meta_key\n ORDER BY order_itemmeta.meta_id \n SEPARATOR '###' ) AS meta_key,\n GROUP_CONCAT(order_itemmeta.meta_value\n ORDER BY order_itemmeta.meta_id \n SEPARATOR '###' ) AS meta_value\n FROM {$wpdb->prefix}woocommerce_order_items AS order_items \n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta \n ON (order_items.order_item_id = order_itemmeta.order_item_id)\n WHERE order_items.order_id IN ({$order_id})\n AND order_items.order_item_type LIKE 'line_item'\n GROUP BY order_items.order_item_id\n ORDER BY FIND_IN_SET(order_items.order_id,'{$order_id}')"; $results_order_items = $wpdb->get_results($query_order_items, 'ARRAY_A'); $num_rows_order_items = $wpdb->num_rows; //code for formatting order items array if ($num_rows_order_items > 0) { $order_items = array(); foreach ($results_order_items as $results_order_item) { if (!isset($order_items[$results_order_item['order_id']])) { $order_items[$results_order_item['order_id']] = array(); } $order_items[$results_order_item['order_id']][] = $results_order_item; } } $query_order_coupons = "SELECT order_id,\n GROUP_CONCAT(order_item_name\n ORDER BY order_item_id \n SEPARATOR ', ' ) AS coupon_used\n FROM {$wpdb->prefix}woocommerce_order_items\n WHERE order_id IN ({$order_id})\n AND order_item_type LIKE 'coupon'\n GROUP BY order_id\n ORDER BY FIND_IN_SET(order_id,'{$order_id}')"; $results_order_coupons = $wpdb->get_results($query_order_coupons, 'ARRAY_A'); $num_rows_coupons = $wpdb->num_rows; if ($num_rows_coupons > 0) { $order_coupons = array(); foreach ($results_order_coupons as $results_order_coupon) { $order_coupons[$results_order_coupon['order_id']] = $results_order_coupon['coupon_used']; } } $query_variation_ids = "SELECT order_itemmeta.meta_value \n FROM {$wpdb->prefix}woocommerce_order_items AS order_items \n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta \n ON (order_items.order_item_id = order_itemmeta.order_item_id)\n WHERE order_itemmeta.meta_key LIKE '_variation_id'\n AND order_itemmeta.meta_value > 0\n AND order_items.order_id IN ({$order_id})"; $result_variation_ids = $wpdb->get_col($query_variation_ids); if (count($result_variation_ids) > 0) { $query_variation_att = "SELECT postmeta.post_id AS post_id,\n GROUP_CONCAT(postmeta.meta_value\n ORDER BY postmeta.meta_id \n SEPARATOR ',' ) AS meta_value\n FROM {$wpdb->prefix}postmeta AS postmeta\n WHERE postmeta.meta_key LIKE 'attribute_%'\n AND postmeta.post_id IN (" . implode(",", $result_variation_ids) . ")\n GROUP BY postmeta.post_id"; // $results_variation_att = $wpdb->get_results($query_variation_att, 'ARRAY_A'); } $query_terms = "SELECT terms.slug as slug, terms.name as term_name\n FROM {$wpdb->prefix}terms AS terms\n JOIN {$wpdb->prefix}postmeta AS postmeta \n ON ( postmeta.meta_value = terms.slug \n AND postmeta.meta_key LIKE 'attribute_%' ) \n GROUP BY terms.slug"; $attributes_terms = $wpdb->get_results($query_terms, 'ARRAY_A'); $attributes = array(); foreach ($attributes_terms as $attributes_term) { $attributes[$attributes_term['slug']] = $attributes_term['term_name']; } $variation_att_all = array(); if (is_array($results_variation_att) && count($results_variation_att) > 0) { for ($i = 0; $i < sizeof($results_variation_att); $i++) { $variation_attributes = explode(", ", $results_variation_att[$i]['meta_value']); $attributes_final = array(); foreach ($variation_attributes as $variation_attribute) { $attributes_final[] = isset($attributes[$variation_attribute]) ? $attributes[$variation_attribute] : ucfirst($variation_attribute); } $results_variation_att[$i]['meta_value'] = implode(", ", $attributes_final); $variation_att_all[$results_variation_att[$i]['post_id']] = $results_variation_att[$i]['meta_value']; } } } foreach ($results as $data) { $meta_key = explode('###', $data['meta_key']); $meta_value = explode('###', $data['meta_value']); if (count($meta_key) == count($meta_value)) { $postmeta = array_combine($meta_key, $meta_value); //Code to replace the email of the Registered Customers with the one from the wp_users if ($postmeta['_customer_user'] > 0) { for ($index = 0; $index < sizeof($result_users); $index++) { if ($postmeta['_customer_user'] == $result_users[$index]['ID']) { $postmeta['_billing_email'] = $result_users[$index]['user_email']; $postmeta['_billing_phone'] = $result_users[$index]['meta_value']; break; } } } if ($_POST['SM_IS_WOO16'] == "true") { if (is_serialized($postmeta['_order_items'])) { $order_items = unserialize(trim($postmeta['_order_items'])); foreach ((array) $order_items as $order_item) { if (isset($order_item['item_meta']) && count($order_item['item_meta']) > 0) { $variation_data = array(); foreach ($order_item['item_meta'] as $meta) { $variation_data['attribute_' . $meta['meta_name']] = $meta['meta_value']; } $variation_details = woocommerce_get_formatted_variation($variation_data, true); } $data['details'] += $order_item['qty']; $data['order_total_ex_tax'] += $order_item['line_total']; $product_id = $order_item['variation_id'] > 0 ? $order_item['variation_id'] : $order_item['id']; $sm_sku = get_post_meta($product_id, '_sku', true); if (!empty($sm_sku)) { $sku_detail = '[SKU: ' . $sm_sku . ']'; } else { $sku_detail = ''; } $product_full_name = !empty($variation_details) ? $order_item['name'] . ' (' . $variation_details . ')' : $order_item['name']; $data['products_name'] .= $product_full_name . ' ' . $sku_detail . '[' . __('Qty', 'smart-manager') . ': ' . $order_item['qty'] . '][' . __('Price', 'smart-manager') . ': ' . $order_item['line_total'] / $order_item['qty'] . '], '; } isset($data['details']) ? $data['details'] .= ' items' : ($data['details'] = ''); $data['products_name'] = substr($data['products_name'], 0, -2); //To remove extra comma ', ' from returned string } else { $data['details'] = 'Details'; } } else { if (!empty($order_items[$data['id']])) { foreach ($order_items[$data['id']] as $order_item) { $prod_meta_values = explode('###', $order_item['meta_value']); $prod_meta_key = explode('###', $order_item['meta_key']); if (count($prod_meta_values) != count($prod_meta_key)) { continue; } unset($order_item['meta_value']); unset($order_item['meta_key']); update_post_meta($index, $sku_detail, $meta_value); $prod_meta_key_values = array_combine($prod_meta_key, $prod_meta_values); // if ($data['id'] == $order_item['order_id']) { $data['details'] += $prod_meta_key_values['_qty']; $data['order_total_ex_tax'] += $prod_meta_key_values['_line_total']; $product_id = $prod_meta_key_values['_variation_id'] > 0 ? $prod_meta_key_values['_variation_id'] : $prod_meta_key_values['_product_id']; $sm_sku = get_post_meta($product_id, '_sku', true); if (!empty($sm_sku)) { $sku_detail = '[SKU: ' . $sm_sku . ']'; } else { $sku_detail = ''; } $variation_att = isset($variation_att_all[$prod_meta_key_values['_variation_id']]) && !empty($variation_att_all[$prod_meta_key_values['_variation_id']]) ? $variation_att_all[$prod_meta_key_values['_variation_id']] : ''; $product_full_name = !empty($variation_att) ? $order_item['order_prod'] . ' (' . $variation_att . ')' : $order_item['order_prod']; $data['products_name'] .= $product_full_name . ' ' . $sku_detail . '[' . __('Qty', 'smart-manager') . ': ' . $prod_meta_key_values['_qty'] . '][' . __('Price', 'smart-manager') . ': ' . $prod_meta_key_values['_line_total'] / $prod_meta_key_values['_qty'] . '], '; $data['coupons'] = isset($order_coupons[$order_item['order_id']]) ? $order_coupons[$order_item['order_id']] : ""; // } } isset($data['details']) ? $data['details'] .= ' items' : ($data['details'] = ''); $data['products_name'] = substr($data['products_name'], 0, -2); //To remove extra comma ', ' from returned string } } //Code to get the Order_Status using the $terms_name array $data['order_status'] = $terms_name[$data['term_taxonomy_id']]; $name_emailid[0] = "<font class=blue>" . $postmeta['_billing_first_name'] . "</font>"; $name_emailid[1] = "<font class=blue>" . $postmeta['_billing_last_name'] . "</font>"; $name_emailid[2] = "(" . $postmeta['_billing_email'] . ")"; //email comes at 7th position. $data['name'] = implode(' ', $name_emailid); //in front end,splitting is done with this space. $data['_shipping_address'] = $postmeta['_shipping_address_1'] . ', ' . $postmeta['_shipping_address_2']; unset($data['meta_value']); $postmeta['_shipping_method'] = isset($postmeta['_shipping_method_title']) ? $postmeta['_shipping_method_title'] : $postmeta['_shipping_method']; $postmeta['_payment_method'] = isset($postmeta['_payment_method_title']) ? $postmeta['_payment_method_title'] : $postmeta['_payment_method']; $postmeta['_shipping_state'] = isset($woocommerce->countries->states[$postmeta['_shipping_country']][$postmeta['_shipping_state']]) ? $woocommerce->countries->states[$postmeta['_shipping_country']][$postmeta['_shipping_state']] : $postmeta['_shipping_state']; $postmeta['_shipping_country'] = isset($woocommerce->countries->countries[$postmeta['_shipping_country']]) ? $woocommerce->countries->countries[$postmeta['_shipping_country']] : $postmeta['_shipping_country']; $data['display_id'] = $data['id']; //Code for Sequential Orders compatibility if ($order_formatted != "" && isset($postmeta['_order_number_formatted'])) { $data['display_id'] = $postmeta['_order_number_formatted']; } $records[] = array_merge($postmeta, $data); } } unset($meta_value); unset($meta_key); unset($postmeta); unset($results); } } if (!isset($_POST['label']) && $_POST['label'] != 'getPurchaseLogs') { $encoded['items'] = $records; $encoded['totalCount'] = $num_records; unset($records); return $encoded; } }
function sr_items_to_values($all_order_items = array()) { global $wpdb; if (count($all_order_items) <= 0 || !defined('SR_IS_WOO16') || !defined('SR_IS_WOO22')) { return $all_order_items; } $values = array(); $attributes_name_to_slug = sr_get_attributes_name_to_slug(); $prefix = defined('SR_IS_WOO16') && SR_IS_WOO16 == "true" ? '' : '_'; if (!empty($all_order_items['order_date'])) { $order_date = $all_order_items['order_date']; } if (!empty($all_order_items['order_status'])) { $order_status = $all_order_items['order_status']; } unset($all_order_items['order_date']); unset($all_order_items['order_status']); foreach ($all_order_items as $order_id => $order_items) { foreach ($order_items as $item) { $order_item = array(); $order_item['order_id'] = $order_id; if (!function_exists('get_product')) { $product_id = !empty($prefix) && !empty($item[$prefix . 'id']) ? $item[$prefix . 'id'] : $item['id']; } else { $product_id = !empty($item['product_id']) ? $item['product_id'] : ''; $product_id = !empty($prefix) && !empty($item[$prefix . 'product_id']) ? $item[$prefix . 'product_id'] : $product_id; } // end if $order_item['product_name'] = get_the_title($product_id); $variation_id = !empty($item['variation_id']) ? $item['variation_id'] : ''; $variation_id = !empty($prefix) && !empty($item[$prefix . 'variation_id']) ? $item[$prefix . 'variation_id'] : $variation_id; $order_item['product_id'] = $variation_id > 0 ? $variation_id : $product_id; if ($variation_id > 0) { $variation_name = array(); if (!function_exists('get_product') && count($item['item_meta']) > 0) { foreach ($item['item_meta'] as $items) { $variation_name['attribute_' . $items['meta_name']] = $items['meta_value']; } } else { $att_name_to_slug_prod = !empty($attributes_name_to_slug[$product_id]) ? $attributes_name_to_slug[$product_id] : array(); foreach ($item as $item_meta_key => $item_meta_value) { if (array_key_exists($item_meta_key, $att_name_to_slug_prod)) { $variation_name['attribute_' . $item_meta_key] = is_array($item_meta_value) && !empty($item_meta_value[0]) ? $item_meta_value[0] : $item_meta_value; } elseif (in_array($item_meta_key, $att_name_to_slug_prod)) { $variation_name['attribute_' . $item_meta_key] = is_array($item_meta_value) && !empty($item_meta_value[0]) ? $item_meta_value[0] : $item_meta_value; } } } $order_item['product_name'] .= ' (' . woocommerce_get_formatted_variation($variation_name, true) . ')'; } $qty = !empty($item['qty']) ? $item['qty'] : ''; $order_item['quantity'] = !empty($prefix) && !empty($item[$prefix . 'qty']) ? $item[$prefix . 'qty'] : $qty; $line_total = !empty($item['line_total']) ? $item['line_total'] : ''; $line_total = !empty($prefix) && !empty($item[$prefix . 'line_total']) ? $item[$prefix . 'line_total'] : $line_total; $order_item['sales'] = $line_total; $line_subtotal = !empty($item['line_subtotal']) ? $item['line_subtotal'] : ''; $line_subtotal = !empty($prefix) && !empty($item[$prefix . 'line_subtotal']) ? $item[$prefix . 'line_subtotal'] : $line_subtotal; $order_item['order_date'] = !empty($item['order_date']) ? $item['order_date'] : $order_date; $order_item['order_status'] = !empty($item['order_status']) ? $item['order_status'] : $order_status; $order_item['discount'] = $line_subtotal - $line_total; if (!empty($item['sku'])) { $order_item['sku'] = $item['sku']; } else { $prod_sku = get_post_meta($product_id, '_sku', true); $order_item['sku'] = !empty($prod_sku) ? $prod_sku : ''; } if (!empty($item['category'])) { $order_item['category'] = $item['category']; } else { $category = get_the_terms($product_id, 'product_cat'); $order_item['category'] = !empty($category) ? $category[0]->name : ''; } if (empty($order_item['product_id']) || empty($order_item['order_id']) || empty($order_item['quantity'])) { continue; } $values[] = "( {$order_item['product_id']}, {$order_item['order_id']},'{$order_item['order_date']}', '{$order_item['order_status']}', '{$order_item['product_name']}', '{$order_item['sku']}' , '{$order_item['category']}' , {$order_item['quantity']}, " . (empty($order_item['sales']) ? 0 : $order_item['sales']) . ", " . (empty($order_item['discount']) ? 0 : $order_item['discount']) . " )"; } } return $values; }
public function av8_manage_cart_columns($column, $post_id = '') { global $post; $cart = new AV8_Cart_Receipt(); $cart->load_receipt($post->ID); $cart->set_guest_details(); $title = ''; switch ($column) { /* If displaying the 'duration' column. */ case 'cartname': $suffix = "'s Cart"; if ($cart->is_guest_order() && $cart->has_guest_details()) { $fullname = ucwords($cart->get_guest_details('billing_first_name')) . ' ' . ucwords($cart->get_guest_details('billing_last_name')); if ($fullname != ' ') { $title .= $fullname; } else { $title .= "Guest"; } $title .= $suffix . " (Guest)"; } elseif ($cart->is_guest_order() && $cart->status() == "Converted" && isset($cart->order)) { $title = ucwords($cart->order->billing_first_name) . ' ' . ucwords($cart->order->billing_last_name) . $suffix . " (Guest)"; } elseif ($cart->is_guest_order()) { $title = "Guest" . $suffix; } elseif ($cart->full_name() != false) { $title = ucwords($cart->full_name()) . $suffix; } $post_url = admin_url('post.php?post=' . $post->ID . '&action=edit'); echo __("<a href='{$post_url}'>" . $title . "</a>"); break; case 'post__in': /* Get the post meta. */ $show_custom_state = $cart->status(); $filter_link = admin_url('edit.php?post_type=carts&status=' . $show_custom_state); echo __('<div class="index_status"><mark class="' . strtolower($show_custom_state) . '_index">' . $show_custom_state . '</mark></div>'); break; /* If displaying the 'genre' column. */ /* If displaying the 'genre' column. */ case 'updated': /* Get the genres for the post. */ the_modified_date('F j, Y'); echo " at "; the_modified_date('g:i a'); break; /* Just break out of the switch statement for everything else. */ /* Just break out of the switch statement for everything else. */ case 'products': //$products = $this->extract_cart_products(); global $woocommerce; $cartitems = get_post_meta($post->ID, 'av8_cartitems', true); $items_arr = str_replace(array('O:17:"WC_Product_Simple"', 'O:10:"WC_Product"'), 'O:8:"stdClass"', $cartitems); if (isset($cartitems) && $cartitems != false) { $order_items = (array) maybe_unserialize($items_arr); } else { break; } $loop = 0; if (sizeof($order_items) > 0 && $order_items != false) { foreach ($order_items as $item) { if (function_exists('get_product')) { if (isset($item['variation_id']) && $item['variation_id'] > 0) { $_product = get_product($item['variation_id']); } else { $_product = get_product($item['product_id']); } } else { if (isset($item['variation_id']) && $item['variation_id'] > 0) { $_product = new WC_Product_Variation($item['variation_id']); } else { $_product = new WC_Product($item['product_id']); } } if (isset($_product) && $_product != false) { echo "<a href='" . get_admin_url('', 'post.php?post=' . $_product->id . '&action=edit') . "'>" . $_product->get_title() . "</a>"; if (isset($_product->variation_data)) { echo ' (' . woocommerce_get_formatted_variation($_product->variation_data, true) . ')'; } if ($item['quantity'] > 1) { echo " x" . $item['quantity']; } } if ($loop < sizeof($order_items) - 1) { echo ", "; } $loop++; } } else { echo "<span style='color:lightgray;'>" . __("No Products", "woocommerce_cart_reports") . "</span>"; } break; case 'actions': $cart->print_cart_actions($cart->status(), $cart->is_guest_order()); break; default: break; } }
/** * Callback for the [subscriptions] shortcode that displays subscription names for a particular user. * * @param array $attributes Shortcode attributes. * @return string */ public static function do_subscriptions_shortcode($attributes) { $attributes = wp_parse_args($attributes, array('user_id' => 0, 'status' => 'active')); $status = $attributes['status']; $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($attributes['user_id']); if (empty($subscriptions)) { return '<ul class="user-subscriptions no-user-subscriptions"> <li>No subscriptions found.</li> </ul>'; } $list = '<ul class="user-subscriptions">'; foreach ($subscriptions as $subscription) { if ($subscription['status'] == $status || $status == 'all') { $subscription_details = WC_Subscriptions_Order::get_item_name($subscription['order_id'], $subscription['product_id']); $order = new WC_Order($subscription['order_id']); $order_item = WC_Subscriptions_Order::get_item_by_product_id($subscription['order_id'], $subscription['product_id']); $product = $order->get_product_from_item($order_item); if (isset($product->variation_data)) { $subscription_details .= ' <span class="subscription-variation-data">(' . woocommerce_get_formatted_variation($product->variation_data, true) . ')</span>'; } $list .= sprintf('<li>%s</li>', $subscription_details); } } $list .= '</ul>'; return $list; }
function sr_query_sales($start_date, $end_date_query, $date_series, $select, $group_by, $select_top_prod, $select_top_abandoned_prod, $terms_post, $post) { global $wpdb; $monthly_sales = array(); $cumm_top_prod_graph_data = array(); $results_top_prod = array(); $top_prod_ids = array(); $top_prod_graph_data = array(); $top_gateway_graph_data = array(); $sr_currency_symbol = isset($post['SR_CURRENCY_SYMBOL']) ? $post['SR_CURRENCY_SYMBOL'] : ''; $sr_decimal_places = isset($post['SR_DECIMAL_PLACES']) ? $post['SR_DECIMAL_PLACES'] : ''; //Query for getting the cumm sales $query_monthly_sales = "SELECT SUM( postmeta.meta_value ) AS todays_sales,\n\t \t\t\t\t\t\tCOUNT(posts.ID) AS total_orders,\n\t \t\t\t\t\t\t{$select}\n\t\t FROM `{$wpdb->prefix}postmeta` AS postmeta\n\t\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = postmeta.post_id )\n\t\t WHERE postmeta.meta_key IN ('_order_total')\n\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.ID IN ({$terms_post})\n\t GROUP BY {$group_by}"; $results_monthly_sales = $wpdb->get_results($query_monthly_sales, 'ARRAY_A'); $rows_monthly_sales = $wpdb->num_rows; //Query for Top 5 Customers //Reg Customers $query_reg_cumm = "SELECT ID FROM `{$wpdb->users}` \n\t WHERE user_registered BETWEEN '{$start_date}' AND '{$end_date_query}'"; $reg_cumm_ids = $wpdb->get_col($query_reg_cumm); $rows_reg_cumm_ids = $wpdb->num_rows; $query_cumm_top_cust_guest = "SELECT postmeta1.meta_value AS billing_email,\n\t GROUP_CONCAT(DISTINCT postmeta2.post_id\n\t ORDER BY postmeta2.meta_id DESC SEPARATOR ',' ) AS post_id,\n\t MAX(postmeta2.post_id) AS post_id_max,\n\t SUM(postmeta2.meta_value) as total\n\t \n\t FROM {$wpdb->prefix}postmeta AS postmeta1\n\t JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta1.post_id)\n\t INNER JOIN {$wpdb->prefix}postmeta AS postmeta2\n\t ON (postmeta2.post_ID = postmeta1.post_ID AND postmeta2.meta_key IN ('_order_total'))\n\t WHERE postmeta1.meta_key IN ('_billing_email')\n\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t AND posts.id IN (SELECT post_id FROM {$wpdb->prefix}postmeta\n\t WHERE meta_key IN ('_customer_user')\n\t AND meta_value = 0\n\t AND post_id IN ({$terms_post}))\n\t GROUP BY postmeta1.meta_value\n\t ORDER BY total DESC\n\t LIMIT 5"; $results_cumm_top_cust_guest = $wpdb->get_results($query_cumm_top_cust_guest, 'ARRAY_A'); $rows_cumm_top_cust_guest = $wpdb->num_rows; if ($rows_cumm_top_cust_guest > 0) { $post_id = array(); $results_cumm_top_cust = array(); foreach ($results_cumm_top_cust_guest as $results_cumm_top_cust_guest1) { $post_id[] = $results_cumm_top_cust_guest1['post_id_max']; } $post_id_imploded = implode(",", $post_id); $query_cumm_top_cust_guest_detail = "SELECT postmeta.post_id as post_id,\n\t GROUP_CONCAT(postmeta.meta_key\n\t ORDER BY postmeta.meta_id DESC SEPARATOR '###' ) AS meta_key,\n\t GROUP_CONCAT(postmeta.meta_value\n\t ORDER BY postmeta.meta_id DESC SEPARATOR '###' ) AS meta_value\n\t FROM {$wpdb->prefix}postmeta AS postmeta\n\t WHERE postmeta.post_id IN ({$post_id_imploded})\n\t AND postmeta.meta_key IN ('_billing_first_name' , '_billing_last_name')\n\t GROUP BY postmeta.post_id\n\t ORDER BY FIND_IN_SET(postmeta.post_id,'{$post_id_imploded}')"; $results_cumm_top_cust_guest_detail = $wpdb->get_results($query_cumm_top_cust_guest_detail, 'ARRAY_A'); for ($i = 0; $i < sizeof($results_cumm_top_cust_guest_detail); $i++) { $results_cumm_top_cust[$i] = array(); $guest_meta_values = explode('###', $results_cumm_top_cust_guest_detail[$i]['meta_value']); $guest_meta_key = explode('###', $results_cumm_top_cust_guest_detail[$i]['meta_key']); if (count($guest_meta_values) != count($guest_meta_key)) { continue; } unset($results_cumm_top_cust_guest_detail[$i]['meta_value']); unset($results_cumm_top_cust_guest_detail[$i]['meta_key']); $guest_meta_key_values = array_combine($guest_meta_key, $guest_meta_values); $results_cumm_top_cust[$i]['total'] = $sr_currency_symbol . sr_number_format($results_cumm_top_cust_guest[$i]['total'], $sr_decimal_places); $results_cumm_top_cust[$i]['calc_total'] = floatval($results_cumm_top_cust_guest[$i]['total']); // value used only for sorting purpose $results_cumm_top_cust[$i]['name'] = $guest_meta_key_values['_billing_first_name'] . " " . $guest_meta_key_values['_billing_last_name']; $results_cumm_top_cust[$i]['billing_email'] = $results_cumm_top_cust_guest[$i]['billing_email']; $results_cumm_top_cust[$i]['post_ids'] = json_encode($results_cumm_top_cust_guest[$i]['post_id']); } } $query_cumm_top_cust_reg = "SELECT postmeta1.meta_value AS user_id,\n\t GROUP_CONCAT(DISTINCT postmeta1.post_id\n\t ORDER BY postmeta1.meta_id DESC SEPARATOR ',' ) AS post_id,\n\t SUM(postmeta2.meta_value) as total\n\t \n\t FROM {$wpdb->prefix}postmeta AS postmeta1\n\t JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta1.post_id)\n\t INNER JOIN {$wpdb->prefix}postmeta AS postmeta2\n\t ON (postmeta2.post_ID = postmeta1.post_ID AND postmeta2.meta_key IN ('_order_total'))\n\t WHERE postmeta1.meta_key IN ('_customer_user')\n\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t AND posts.id IN (SELECT post_id FROM {$wpdb->prefix}postmeta\n\t WHERE meta_key IN ('_customer_user')\n\t AND meta_value > 0\n\t AND post_id IN ({$terms_post}))\n\t GROUP BY postmeta1.meta_value\n\t ORDER BY total DESC\n\t LIMIT 5"; $results_cumm_top_cust_reg = $wpdb->get_results($query_cumm_top_cust_reg, 'ARRAY_A'); $rows_cumm_top_cust_reg = $wpdb->num_rows; if ($rows_cumm_top_cust_reg > 0) { foreach ($results_cumm_top_cust_reg as $results_cumm_top_cust_reg1) { $user_id[] = $results_cumm_top_cust_reg1['user_id']; } if (!empty($user_id)) { $user_ids_imploded = implode(",", $user_id); } $query_reg_details = "SELECT users.ID as cust_id,\n\t users.user_email as email,\n\t GROUP_CONCAT(usermeta.meta_key\n\t ORDER BY usermeta.umeta_id DESC SEPARATOR '###' ) AS meta_key,\n\t GROUP_CONCAT(usermeta.meta_value\n\t ORDER BY usermeta.umeta_id DESC SEPARATOR '###' ) AS meta_value\n\t FROM {$wpdb->users} as users\n\t JOIN {$wpdb->usermeta} as usermeta ON (users.ID = usermeta.user_id)\n\t WHERE users.ID IN ({$user_ids_imploded})\n\t AND usermeta.meta_key IN ('first_name','last_name')\n\t GROUP BY users.ID\n\t ORDER BY FIND_IN_SET('users.ID','{$user_ids_imploded}')"; $results_reg_details = $wpdb->get_results($query_reg_details, 'ARRAY_A'); for ($i = sizeof($results_cumm_top_cust), $j = 0; $j < sizeof($results_reg_details); $i++, $j++) { $results_cumm_top_cust[$i] = array(); $reg_meta_values = explode('###', $results_reg_details[$j]['meta_value']); $reg_meta_key = explode('###', $results_reg_details[$j]['meta_key']); if (count($reg_meta_values) != count($reg_meta_key)) { continue; } unset($results_reg_details[$j]['meta_value']); unset($results_reg_details[$j]['meta_key']); $reg_meta_key_values = array_combine($reg_meta_key, $reg_meta_values); $results_cumm_top_cust[$i]['total'] = $sr_currency_symbol . sr_number_format($results_cumm_top_cust_reg[$j]['total'], $sr_decimal_places); $results_cumm_top_cust[$i]['calc_total'] = floatval($results_cumm_top_cust_reg[$j]['total']); // value used only for sorting purpose $results_cumm_top_cust[$i]['name'] = $reg_meta_key_values['first_name'] . " " . $reg_meta_key_values['last_name']; $results_cumm_top_cust[$i]['billing_email'] = $results_reg_details[$j]['email']; $results_cumm_top_cust[$i]['post_ids'] = json_encode($results_cumm_top_cust_reg[$j]['post_id']); } } if (!empty($results_cumm_top_cust)) { usort($results_cumm_top_cust, 'usort_callback'); $results_cumm_top_cust = array_slice($results_cumm_top_cust, 0, 5); } else { $results_cumm_top_cust = ""; } //Top 5 Products //Query to get the Top 5 Products $query_top_prod = "SELECT order_item.product_id as product_id,\n\t order_item.product_name as product_name,\n\t SUM( order_item.sales ) AS product_sales ,\n\t SUM( order_item.quantity ) AS product_qty\n\t FROM `{$wpdb->prefix}sr_woo_order_items` AS order_item\n\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = order_item.order_id )\n\t WHERE posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t AND posts.id IN ({$terms_post})\n\t GROUP BY order_item.product_id\n\t ORDER BY product_sales DESC\n\t LIMIT 5"; $results_top_prod = $wpdb->get_results($query_top_prod, 'ARRAY_A'); $rows_top_prod = $wpdb->num_rows; if ($rows_top_prod > 0) { foreach (array_keys($results_top_prod) as $results_top_prod1) { $top_prod_ids[] = $results_top_prod[$results_top_prod1]['product_id']; if (isset($post['top_prod_option'])) { $results_top_prod[$results_top_prod1]['product_sales_display'] = $sr_currency_symbol . sr_number_format($results_top_prod[$results_top_prod1]['product_sales'], $sr_decimal_places); } } if (!empty($top_prod_ids)) { $top_prod_ids1 = implode(",", $top_prod_ids); } //Query to get the Top 5 Products graph related data $query_top_prod_graph = "SELECT order_item.product_id as product_id,\n\t SUM( order_item.sales ) AS product_sales,\n\t SUM( order_item.quantity ) AS product_qty,\n\t {$select}\n\t FROM `{$wpdb->prefix}sr_woo_order_items` AS order_item\n\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = order_item.order_id )\n\t WHERE posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t AND order_item.product_id IN ({$top_prod_ids1})\n\t GROUP BY order_item.product_id,{$group_by}\n\t ORDER BY FIND_IN_SET(order_item.product_id,'{$top_prod_ids1}')"; $results_top_prod_graph = $wpdb->get_results($query_top_prod_graph, 'ARRAY_A'); $rows_top_prod_graph = $wpdb->num_rows; if ($rows_top_prod_graph > 0) { foreach ($results_top_prod_graph as $results_top_prod_graph1) { $top_prod_graph_temp[] = $results_top_prod_graph1['product_id']; } for ($i = 0, $j = 0, $k = 0; $i < sizeof($results_top_prod_graph); $i++) { if ($i > 0) { if ($results_top_prod_graph[$i]['product_id'] == $prod_id) { $j++; $top_prod_graph_data[$k][$j]['product_sales'] = $results_top_prod_graph[$i]['product_sales']; $top_prod_graph_data[$k][$j]['product_qty'] = $results_top_prod_graph[$i]['product_qty']; $top_prod_graph_data[$k][$j][$group_by] = $results_top_prod_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_prod_graph_data[$k][$j]['display_time'] = $results_top_prod_graph[$i]['display_time']; $top_prod_graph_data[$k][$j]['comp_time'] = $results_top_prod_graph[$i]['comp_time']; } $prod_id = $results_top_prod_graph[$i]['product_id']; } else { $k++; $j = 0; $top_prod_graph_data[$k] = array(); $top_prod_graph_data[$k][$j]['product_sales'] = $results_top_prod_graph[$i]['product_sales']; $top_prod_graph_data[$k][$j]['product_qty'] = $results_top_prod_graph[$i]['product_qty']; $top_prod_graph_data[$k][$j][$group_by] = $results_top_prod_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_prod_graph_data[$k][$j]['display_time'] = $results_top_prod_graph[$i]['display_time']; $top_prod_graph_data[$k][$j]['comp_time'] = $results_top_prod_graph[$i]['comp_time']; } $prod_id = $results_top_prod_graph[$i]['product_id']; } } else { $top_prod_graph_data[$k] = array(); $top_prod_graph_data[$k][$j]['product_sales'] = $results_top_prod_graph[$i]['product_sales']; $top_prod_graph_data[$k][$j]['product_qty'] = $results_top_prod_graph[$i]['product_qty']; $top_prod_graph_data[$k][$j][$group_by] = $results_top_prod_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_prod_graph_data[$k][$j]['display_time'] = $results_top_prod_graph[$i]['display_time']; $top_prod_graph_data[$k][$j]['comp_time'] = $results_top_prod_graph[$i]['comp_time']; } $prod_id = $results_top_prod_graph[$i]['product_id']; } } } } $monthly_sales_temp = $date_series; $max_sales = 0; $total_monthly_sales = 0; $tot_cumm_orders = 0; $tot_cumm_orders_qty = 0; $total_orders = 0; if ($rows_monthly_sales > 0) { foreach ($results_monthly_sales as $results_monthly_sale) { if ($group_by == "display_date_time") { $monthly_sales_temp[$results_monthly_sale['comp_time']]['post_date'] = date("Y-m-d", strtotime($start_date)) . ' ' . $results_monthly_sale['display_time']; $monthly_sales_temp[$results_monthly_sale['comp_time']]['sales'] = floatval($results_monthly_sale['todays_sales']); } else { $monthly_sales_temp[$results_monthly_sale[$group_by]]['sales'] = floatval($results_monthly_sale['todays_sales']); } if ($max_sales < $results_monthly_sale['todays_sales']) { $max_sales = $results_monthly_sale['todays_sales']; } $total_monthly_sales = $total_monthly_sales + $results_monthly_sale['todays_sales']; $total_orders = $total_orders + $results_monthly_sale['total_orders']; } foreach ($monthly_sales_temp as $monthly_sales_temp1) { $monthly_sales[] = $monthly_sales_temp1; } } //Top 5 Products Graph $cumm_top_prod_graph_data = array(); $index = 0; $max_values = array(); if (!empty($top_prod_graph_data)) { foreach ($top_prod_graph_data as $results_top_prod_graph1) { $cumm_top_prod_graph_data[$index] = array(); $temp = array(); $cumm_date = $date_series; $max = 0; for ($j = 0; $j < sizeof($results_top_prod_graph1); $j++) { if ($group_by == "display_date_time") { $cumm_date[$results_top_prod_graph1[$j]['comp_time']]['post_date'] = date("Y-m-d", strtotime($start_date)) . ' ' . $results_top_prod_graph1[$j]['display_time']; } if (isset($post['top_prod_option'])) { if ($post['top_prod_option'] == 'sr_opt_top_prod_price') { if ($results_top_prod_graph1[$j]['product_sales'] > $max) { $max = floatval($results_top_prod_graph1[$j]['product_sales']); } if ($group_by == "display_date_time") { $cumm_date[$results_top_prod_graph1[$j]['comp_time']]['sales'] = floatval($results_top_prod_graph1[$j]['product_sales']); } else { $cumm_date[$results_top_prod_graph1[$j][$group_by]]['sales'] = floatval($results_top_prod_graph1[$j]['product_sales']); } } else { if ($post['top_prod_option'] == 'sr_opt_top_prod_qty') { if ($results_top_prod_graph1[$j]['product_qty'] > $max) { $max = intval($results_top_prod_graph1[$j]['product_qty']); } if ($group_by == "display_date_time") { $cumm_date[$results_top_prod_graph1[$j]['comp_time']]['sales'] = intval($results_top_prod_graph1[$j]['product_qty']); } else { $cumm_date[$results_top_prod_graph1[$j][$group_by]]['sales'] = intval($results_top_prod_graph1[$j]['product_qty']); } } } } else { if ($results_top_prod_graph1[$j]['product_sales'] > $max) { $max = floatval($results_top_prod_graph1[$j]['product_sales']); } $cumm_date[$results_top_prod_graph1[$j][$group_by]]['sales'] = floatval($results_top_prod_graph1[$j]['product_sales']); } $product_sales_display = $results_top_prod_graph1[$j]['product_sales']; } foreach ($cumm_date as $cumm_date1) { $temp[] = $cumm_date1; } if (isset($post['option'])) { // Condition to handle the change of graph on option select $cumm_top_prod_graph_data[$index]['graph_data'] = $temp; $cumm_top_prod_graph_data[$index]['max_value'] = $max; } else { $results_top_prod[$index]['graph_data'] = $temp; $results_top_prod[$index]['max_value'] = $max; } $index++; } } //Query for Avg. Items Per Customer $query_cumm_reg_cust_count = "SELECT COUNT(DISTINCT postmeta.meta_value) AS cust_orders\n\t\t FROM {$wpdb->prefix}postmeta AS postmeta\n\t\t JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta.post_id)\n\t\t WHERE postmeta.meta_key IN ('_customer_user')\n\t\t AND postmeta.meta_value > 0\n\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.id IN ({$terms_post})\n\t\t GROUP BY postmeta.meta_value"; $results_cumm_reg_cust_count = $wpdb->get_col($query_cumm_reg_cust_count); $rows_cumm_reg_cust_count = $wpdb->num_rows; if ($rows_cumm_reg_cust_count > 0) { $reg_cust_count = $results_cumm_reg_cust_count[0]; } else { $reg_cust_count = 0; } $query_cumm_guest_cust_count = "SELECT COUNT(DISTINCT postmeta1.meta_value) AS cust_orders\n\t\t FROM {$wpdb->prefix}postmeta AS postmeta1\n\t\t JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta1.post_id)\n\t\t INNER JOIN {$wpdb->prefix}postmeta AS postmeta2 \n\t\t ON (postmeta2.post_ID = postmeta1.post_ID AND postmeta2.meta_key IN ('_customer_user'))\n\t\t WHERE postmeta1.meta_key IN ('_billing_email')\n\t\t AND postmeta2.meta_value = 0\n\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.id IN ({$terms_post})\n\t\t GROUP BY postmeta1.meta_value"; $results_cumm_guest_cust_count = $wpdb->get_col($query_cumm_guest_cust_count); $rows_cumm_guest_cust_count = $wpdb->num_rows; if ($rows_cumm_guest_cust_count > 0) { $guest_cust_count = $results_cumm_guest_cust_count[0]; } else { $guest_cust_count = 0; } $total_cumm_cust_count = $reg_cust_count + $guest_cust_count; //Query for Avg. Order Total and Avg. Order Items $query_cumm_avg_order_tot_items = "SELECT COUNT(DISTINCT order_item.order_id) as no_orders,\n\t\t\t\t SUM( order_item.quantity ) AS cumm_quantity\n\t\t\t FROM `{$wpdb->prefix}sr_woo_order_items` AS order_item\n\t\t\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = order_item.order_id )\n\t\t\t WHERE posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t\t \tAND posts.ID IN ({$terms_post})"; $results_cumm_avg_order_tot_items = $wpdb->get_results($query_cumm_avg_order_tot_items, 'ARRAY_A'); $rows_cumm_avg_order_tot_items = $wpdb->num_rows; if ($rows_cumm_avg_order_tot_items > 0) { $tot_cumm_orders = $results_cumm_avg_order_tot_items[0]['no_orders']; $tot_cumm_orders_qty = $results_cumm_avg_order_tot_items[0]['cumm_quantity']; } else { $tot_cumm_orders = 0; $tot_cumm_orders_qty = 0; } //Total Discount Sales Widget $query_cumm_discount_sales = "SELECT SUM( postmeta.meta_value ) AS discount_sales,\n\t \t\t\t\t\t\t{$select}\n\t\t FROM `{$wpdb->prefix}postmeta` AS postmeta\n\t\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = postmeta.post_id )\n\t\t WHERE postmeta.meta_key IN ('_order_discount','_cart_discount')\n\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.ID IN ({$terms_post})\n\t GROUP BY {$group_by}"; $results_cumm_discount_sales = $wpdb->get_results($query_cumm_discount_sales, 'ARRAY_A'); $rows_cumm_discount_sales = $wpdb->num_rows; $cumm_discount_sales_temp = $date_series; $cumm_discount_sales = array(); $max_discount_total = 0; $total_discount_sales = 0; if ($rows_cumm_discount_sales > 0) { foreach ($results_cumm_discount_sales as $results_cumm_discount_sale) { if ($group_by == "display_date_time") { $cumm_discount_sales_temp[$results_cumm_discount_sale['comp_time']]['post_date'] = date("Y-m-d", strtotime($start_date)) . ' ' . $results_cumm_discount_sale['display_time']; $cumm_discount_sales_temp[$results_cumm_discount_sale['comp_time']]['sales'] = floatval($results_cumm_discount_sale['discount_sales']); } else { $cumm_discount_sales_temp[$results_cumm_discount_sale[$group_by]]['sales'] = floatval($results_cumm_discount_sale['discount_sales']); } if ($max_discount_total < $results_cumm_discount_sale['discount_sales']) { $max_discount_total = $results_cumm_discount_sale['discount_sales']; } $total_discount_sales = $total_discount_sales + $results_cumm_discount_sale['discount_sales']; } foreach ($cumm_discount_sales_temp as $cumm_discount_sales_temp1) { $cumm_discount_sales[] = $cumm_discount_sales_temp1; } } //Top Coupons Widget $query_cumm_coupon_count = "SELECT COUNT( order_items.order_item_name ) AS coupon_count,\n\t \t\t\t\t\t\t\tSUM(order_itemmeta.meta_value) AS coupon_amount,\n\t \t\t\t\t\t\t\torder_items.order_item_name AS coupon_name,\n\t \t\t\t\t\t\t\tGROUP_CONCAT(DISTINCT order_items.order_id\n\t ORDER BY order_items.order_item_id DESC SEPARATOR ',' ) AS order_ids\n\t\t FROM `{$wpdb->prefix}posts` AS posts\n\t\t \tJOIN {$wpdb->prefix}woocommerce_order_items as order_items ON ( posts.ID = order_items.order_id )\n\t\t \tJOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_itemmeta \n\t\t \t\tON (order_items.order_item_id = order_itemmeta.order_item_id \n\t\t \t\t\t\tAND order_itemmeta.meta_key IN ('discount_amount') )\n\t\t WHERE posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.ID IN ({$terms_post})\n\t\t AND order_items.order_item_type IN ('coupon')\n\t GROUP BY order_items.order_item_name\n\t ORDER BY coupon_count DESC, coupon_amount DESC\n\t LIMIT 5"; $results_cumm_coupon_count = $wpdb->get_results($query_cumm_coupon_count, 'ARRAY_A'); $rows_cumm_coupon_count = $wpdb->num_rows; foreach ($results_cumm_coupon_count as &$results_cumm_coupon_count1) { $results_cumm_coupon_count1['coupon_amount'] = $sr_currency_symbol . sr_number_format($results_cumm_coupon_count1['coupon_amount'], $sr_decimal_places); $results_cumm_coupon_count1['coupon_count'] = sr_number_format($results_cumm_coupon_count1['coupon_count'], $sr_decimal_places); } // % Orders Containing Coupons $sr_per_order_containing_coupons = 0; $query_cumm_orders_coupon_count = "SELECT COUNT( posts.ID ) AS total_coupon_orders\n\t\t \t\t\t\t\t\t\t\t\tFROM `{$wpdb->prefix}posts` AS posts\n\t\t\t \t\t\t\tJOIN {$wpdb->prefix}woocommerce_order_items as order_items ON ( posts.ID = order_items.order_id )\n\t\t\t \t\t\tWHERE posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t\t\t\t\t AND posts.ID IN ({$terms_post})\n\t\t\t\t\t \tAND order_items.order_item_type IN ('coupon')"; $results_cumm_orders_coupon_count = $wpdb->get_col($query_cumm_orders_coupon_count); $rows_cumm_orders_coupon_count = $wpdb->num_rows; if ($rows_cumm_orders_coupon_count > 0 && $total_orders > 0) { $sr_per_order_containing_coupons = $results_cumm_orders_coupon_count[0] / $total_orders * 100; } //Orders By Payment Gateways $query_top_payment_gateway = "SELECT postmeta1.meta_value AS payment_method,\n\t\t \t\t\t\t\t\t\tSUM(postmeta2.meta_value) AS sales_total,\n\t\t \t\t\t\t\t\t\tCOUNT(posts.ID) AS sales_count,\n\t\t \t\t\t\t\t\t\tGROUP_CONCAT(posts.ID ORDER BY posts.ID DESC SEPARATOR ',' ) AS order_ids\n\t\t\t\t FROM {$wpdb->prefix}posts AS posts \n\t\t\t\t LEFT JOIN `{$wpdb->prefix}postmeta` AS postmeta1 ON ( posts.ID = postmeta1.post_id )\n\t\t\t\t LEFT JOIN `{$wpdb->prefix}postmeta` AS postmeta2 ON ( posts.ID = postmeta2.post_id )\n\t\t\t\t WHERE postmeta1.meta_key IN ('_payment_method')\n\t\t\t\t \tAND postmeta2.meta_key IN ('_order_total')\n\t\t\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t\t\t AND posts.ID IN ({$terms_post})\n\t\t\t GROUP BY payment_method\n\t\t\t ORDER BY sales_total DESC\n\t\t\t LIMIT 5"; $results_top_payment_gateway = $wpdb->get_results($query_top_payment_gateway, 'ARRAY_A'); $rows_top_payment_gateway = $wpdb->num_rows; if ($rows_top_payment_gateway > 0) { foreach ($results_top_payment_gateway as &$results_top_payment_gateway1) { $top_payment_gateway[] = $results_top_payment_gateway1['payment_method']; if (isset($post['top_prod_option'])) { $results_top_payment_gateway1['gateway_sales_display'] = $sr_currency_symbol . sr_number_format($results_top_payment_gateway1['sales_total'], $sr_decimal_places); $results_top_payment_gateway1['gateway_sales_percent'] = sr_number_format($results_top_payment_gateway1['sales_total'] / $total_monthly_sales * 100, $sr_decimal_places) . '%'; } } if (!empty($top_payment_gateway)) { $top_payment_gateway_imploded = "'" . implode("','", $top_payment_gateway) . "'"; } } //Query to get the Top 5 Products graph related data $query_top_gateways_graph = "SELECT postmeta1.meta_value AS payment_method,\n\t \t\t\t\t\t\t\tSUM(postmeta2.meta_value) AS sales_total,\n\t \t\t\t\t\t\t\tCOUNT(posts.ID) AS sales_count,\n\t \t\t\t\t\t\t\t{$select}\n\t\t\t FROM {$wpdb->prefix}posts AS posts \n\t\t\t LEFT JOIN `{$wpdb->prefix}postmeta` AS postmeta1 ON ( posts.ID = postmeta1.post_id )\n\t\t\t LEFT JOIN `{$wpdb->prefix}postmeta` AS postmeta2 ON ( posts.ID = postmeta2.post_id )\n\t\t\t WHERE postmeta1.meta_key IN ('_payment_method')\n\t\t\t \tAND postmeta2.meta_key IN ('_order_total')\n\t\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t\t AND posts.ID IN ({$terms_post})\n\t\t\t AND postmeta1.meta_value IN ({$top_payment_gateway_imploded})\n\t\t GROUP BY payment_method, {$group_by}\n\t\t ORDER BY FIND_IN_SET(postmeta1.meta_value,'" . implode(",", $top_payment_gateway) . "')"; $results_top_gateways_graph = $wpdb->get_results($query_top_gateways_graph, 'ARRAY_A'); $rows_top_gateways_graph = $wpdb->num_rows; $cumm_payment_gateway_temp = $date_series; $cumm_payment_gateway_sales = array(); if ($rows_top_gateways_graph > 0) { for ($i = 0, $j = 0, $k = 0; $i < sizeof($results_top_gateways_graph); $i++) { if ($i > 0) { if ($results_top_gateways_graph[$i]['payment_method'] == $payment_method) { $j++; $top_gateway_graph_data[$k][$j]['gateway_sales_amt'] = $results_top_gateways_graph[$i]['sales_total']; $top_gateway_graph_data[$k][$j]['gateway_sales_count'] = $results_top_gateways_graph[$i]['sales_count']; $top_gateway_graph_data[$k][$j][$group_by] = $results_top_gateways_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_gateway_graph_data[$k][$j]['display_time'] = $results_top_gateways_graph[$i]['display_time']; $top_gateway_graph_data[$k][$j]['comp_time'] = $results_top_gateways_graph[$i]['comp_time']; } $payment_method = $results_top_gateways_graph[$i]['payment_method']; } else { $k++; $j = 0; $top_gateway_graph_data[$k] = array(); $top_gateway_graph_data[$k][$j]['gateway_sales_amt'] = $results_top_gateways_graph[$i]['sales_total']; $top_gateway_graph_data[$k][$j]['gateway_sales_count'] = $results_top_gateways_graph[$i]['sales_count']; $top_gateway_graph_data[$k][$j][$group_by] = $results_top_gateways_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_gateway_graph_data[$k][$j]['display_time'] = $results_top_gateways_graph[$i]['display_time']; $top_gateway_graph_data[$k][$j]['comp_time'] = $results_top_gateways_graph[$i]['comp_time']; } $payment_method = $results_top_gateways_graph[$i]['payment_method']; } } else { $top_gateway_graph_data[$k] = array(); $top_gateway_graph_data[$k][$j]['gateway_sales_amt'] = $results_top_gateways_graph[$i]['sales_total']; $top_gateway_graph_data[$k][$j]['gateway_sales_count'] = $results_top_gateways_graph[$i]['sales_count']; $top_gateway_graph_data[$k][$j][$group_by] = $results_top_gateways_graph[$i][$group_by]; if ($group_by == "display_date_time") { $top_gateway_graph_data[$k][$j]['display_time'] = $results_top_gateways_graph[$i]['display_time']; $top_gateway_graph_data[$k][$j]['comp_time'] = $results_top_gateways_graph[$i]['comp_time']; } $payment_method = $results_top_gateways_graph[$i]['payment_method']; } } } //Query to get the Payment Gateway Title $query_gateway_title = "SELECT DISTINCT postmeta1.meta_value as gateway_title,\n \t\t\t\t\t\t\tpostmeta2.meta_value as gateway_method\n\t \t\t\t\t\t\tFROM `{$wpdb->prefix}postmeta` AS postmeta1\n\t \t\t\t\t\t\t\tJOIN `{$wpdb->prefix}postmeta` AS postmeta2 ON ( postmeta1.post_id = postmeta2.post_id )\n \t\t\t\t\t\t\tWHERE postmeta1.meta_key IN ('_payment_method_title')\n \t\t\t\t\t\t\t\tAND postmeta2.meta_key IN ('_payment_method')\n \t\t\t\t\t\t\t\tAND postmeta2.meta_value IN ({$top_payment_gateway_imploded})\n \t\t\t\t\t\t\tORDER BY FIND_IN_SET(postmeta1.meta_value,'" . implode(",", $top_payment_gateway) . "')"; $result_gateway_title = $wpdb->get_results($query_gateway_title, 'ARRAY_A'); $gateway_title = array(); foreach ($result_gateway_title as $result_gateway_title1) { $gateway_title[$result_gateway_title1['gateway_method']] = $result_gateway_title1['gateway_title']; } //Top 5 Products Graph $cumm_top_gateway_graph_data = array(); $index = 0; $max_values = array(); if (!empty($top_gateway_graph_data)) { foreach ($top_gateway_graph_data as $top_gateway_graph_data1) { $cumm_top_gateway_amt_graph_data[$index] = array(); $temp_gateway_sales_amt = array(); $temp_gateway_sales_count = array(); $cumm_date_amt = $date_series; $cumm_date_count = $date_series; $max_amt = 0; $max_count = 0; for ($j = 0; $j < sizeof($top_gateway_graph_data1); $j++) { if ($group_by == "display_date_time") { $cumm_date_amt[$top_gateway_graph_data1[$j]['comp_time']]['post_date'] = date("Y-m-d", strtotime($start_date)) . ' ' . $top_gateway_graph_data1[$j]['display_time']; $cumm_date_count[$top_gateway_graph_data1[$j]['comp_time']]['post_date'] = date("Y-m-d", strtotime($start_date)) . ' ' . $top_gateway_graph_data1[$j]['display_time']; $cumm_date_amt[$top_gateway_graph_data1[$j]['comp_time']]['sales'] = floatval($top_gateway_graph_data1[$j]['gateway_sales_amt']); $cumm_date_count[$top_gateway_graph_data1[$j]['comp_time']]['sales'] = floatval($top_gateway_graph_data1[$j]['gateway_sales_count']); } else { $cumm_date_amt[$top_gateway_graph_data1[$j][$group_by]]['sales'] = floatval($top_gateway_graph_data1[$j]['gateway_sales_amt']); $cumm_date_count[$top_gateway_graph_data1[$j][$group_by]]['sales'] = floatval($top_gateway_graph_data1[$j]['gateway_sales_count']); } //Payment Gateways Sales Amt if ($top_gateway_graph_data1[$j]['gateway_sales_amt'] > $max_amt) { $max_amt = floatval($top_gateway_graph_data1[$j]['gateway_sales_amt']); } //Payment Gateways Sales Count if ($top_gateway_graph_data1[$j]['gateway_sales_count'] > $max_count) { $max_count = floatval($top_gateway_graph_data1[$j]['gateway_sales_count']); } } foreach ($cumm_date_amt as $cumm_date_amt1) { $temp_gateway_sales_amt[] = $cumm_date_amt1; } foreach ($cumm_date_count as $cumm_date_count1) { $temp_gateway_sales_count[] = $cumm_date_count1; } $results_top_payment_gateway[$index]['graph_data_sales_amt'] = $temp_gateway_sales_amt; $results_top_payment_gateway[$index]['max_value_sales_amt'] = $max_amt; $results_top_payment_gateway[$index]['graph_data_sales_count'] = $temp_gateway_sales_count; $results_top_payment_gateway[$index]['max_value_sales_count'] = $max_count; $results_top_payment_gateway[$index]['payment_method'] = $gateway_title[$results_top_payment_gateway[$index]['payment_method']]; $index++; } } //Query for getting the cumm taxes $query_cumm_taxes = "SELECT GROUP_CONCAT(postmeta.meta_key order by postmeta.meta_id SEPARATOR '###') AS prod_othermeta_key,\n\t\t\t\t\t\t\t\t\tGROUP_CONCAT(postmeta.meta_value order by postmeta.meta_id SEPARATOR '###') AS prod_othermeta_value\n\t\t FROM `{$wpdb->prefix}postmeta` AS postmeta\n\t\t LEFT JOIN {$wpdb->prefix}posts AS posts ON ( posts.ID = postmeta.post_id )\n\t\t WHERE postmeta.meta_key IN ('_order_total','_order_shipping','_order_shipping_tax','_order_tax')\n\t\t AND posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}'\n\t\t AND posts.ID IN ({$terms_post})\n\t\t GROUP BY posts.ID"; $results_cumm_taxes = $wpdb->get_results($query_cumm_taxes, 'ARRAY_A'); $rows_cumm_taxes = $wpdb->num_rows; if ($rows_cumm_taxes > 0) { $tax = 0; $shipping_tax = 0; $shipping = 0; $order_total = 0; foreach ($results_cumm_taxes as $results_cumm_tax) { $prod_meta_values = explode('###', $results_cumm_tax['prod_othermeta_value']); $prod_meta_key = explode('###', $results_cumm_tax['prod_othermeta_key']); if (count($prod_meta_values) != count($prod_meta_key)) { continue; } $prod_meta_key_values = array_combine($prod_meta_key, $prod_meta_values); $tax = $tax + $prod_meta_key_values['_order_tax']; $shipping_tax = $shipping_tax + $prod_meta_key_values['_order_shipping_tax']; $shipping = $shipping + $prod_meta_key_values['_order_shipping']; $order_total = $order_total + $prod_meta_key_values['_order_total']; } $tax_data['tax'] = $tax; $tax_data['shipping_tax'] = $shipping_tax; $tax_data['shipping'] = $shipping; $tax_data['net_sales'] = $order_total - ($tax + $shipping_tax + $shipping); $tax_data['total_sales'] = $order_total; } //Query to get Top Abandoned Products $current_time = current_time('timestamp'); $cut_off_time = get_option('sr_abandoned_cutoff_time') ? get_option('sr_abandoned_cutoff_time') : 0; $cart_cut_off_time = $cut_off_time * 60; $compare_time = $current_time - $cart_cut_off_time; //Query to update the abandoned product status $query_abandoned_status = "UPDATE {$wpdb->prefix}sr_woo_abandoned_items\n\t \t\t\t\t\t\t\tSET product_abandoned = 1\n\t \t\t\t\t\t\t\tWHERE order_id IS NULL\n\t \t\t\t\t\t\t\t\tAND abandoned_cart_time < " . $compare_time; $wpdb->query($query_abandoned_status); //Query to get the Top Abandoned Products $query_top_abandoned_products = "SELECT SUM(quantity) as abondoned_qty,\n\t\t\t\t\t\t\t\t\t\t\tGROUP_CONCAT(quantity order by id SEPARATOR '###') AS abandoned_quantity,\n\t\t\t\t\t\t\t\t\t\t\tproduct_id as id,\n\t\t\t\t\t\t\t\t\t\t\t{$select_top_abandoned_prod}\n\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items\n\t\t\t\t\t\t\t\t\t\tWHERE order_id IS NULL \n\t\t\t\t\t\t\t\t\t\t\tAND product_abandoned = 1\n\t\t\t\t\t\t\t\t\t\t\tAND abandoned_cart_time BETWEEN '" . strtotime($start_date) . "' AND '" . strtotime($end_date_query) . "'\n\t\t\t\t\t\t\t\t\t\tGROUP BY product_id\n\t\t\t\t\t\t\t\t\t\tORDER BY abondoned_qty DESC\n\t\t\t\t\t\t\t\t\t\tLIMIT 5"; $results_top_abandoned_products = $wpdb->get_results($query_top_abandoned_products, 'ARRAY_A'); $rows_top_abandoned_products = $wpdb->num_rows; if ($rows_top_abandoned_products > 0) { $prod_id = array(); foreach ($results_top_abandoned_products as $results_top_abandoned_product) { $prod_id[] = $results_top_abandoned_product['id']; } $prod_id = implode(",", $prod_id); $query_prod_abandoned_rate = "SELECT SUM(quantity) as abondoned_rate\n\t\t\t\t\t\t\t\t\t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items\n\t\t\t\t\t\t\t\t\t\t\tWHERE product_abandoned = 1\n\t\t\t\t\t\t\t\t\t\t\t\tAND abandoned_cart_time BETWEEN '" . strtotime($start_date) . "' AND '" . strtotime($end_date_query) . "'\n\t\t\t\t\t\t\t\t\t\t\t\tAND product_id IN (" . $prod_id . ")\n\t\t\t\t\t\t\t\t\t\t\tGROUP BY product_id\n\t\t\t\t\t\t\t\t\t\t\tORDER BY FIND_IN_SET(product_id,'{$prod_id}') "; $results_prod_abandoned_rate = $wpdb->get_col($query_prod_abandoned_rate); $rows_prod_abandoned_rate = $wpdb->num_rows; $j = 0; $total_prod_abandoned_qty = 0; $total_prod_qty = 0; //Query to get the variation Attributes in a formatted manner $query_attributes = "SELECT post_id,\n\t\t \t\t\t\t\t\t\tGROUP_CONCAT(meta_key order by meta_id SEPARATOR '###') AS meta_key,\n\t\t \t\t\t\t\t\t\tGROUP_CONCAT(meta_value order by meta_id SEPARATOR '###') AS meta_value\n\t\t\t\t \t\t\tFROM {$wpdb->prefix}postmeta\n\t\t\t\t \t\t\tWHERE meta_key like 'attribute_%'\n\t\t\t\t \t\t\t\tAND post_id IN ({$prod_id})\n\t\t\t\t \t\t\tGROUP BY post_id"; $results_attributes = $wpdb->get_results($query_attributes, 'ARRAY_A'); $rows_attributes = $wpdb->num_rows; $variation_attributes = array(); foreach ($results_attributes as $results_attribute) { $meta_key = explode('###', $results_attribute['meta_key']); $meta_value = explode('###', $results_attribute['meta_value']); if (count($meta_key) != count($meta_value)) { continue; } $variation_attributes[$results_attribute['post_id']] = woocommerce_get_formatted_variation(array_combine($meta_key, $meta_value), true); } foreach ($results_top_abandoned_products as &$results_top_abandoned_product) { $abandoned_quantity = explode('###', $results_top_abandoned_product['abandoned_quantity']); $abandoned_dates = explode('###', $results_top_abandoned_product['abandoned_dates']); if ($group_by == "display_date_time") { $abandoned_dates_comp = explode('###', $results_top_abandoned_product['comp_time']); } if (count($abandoned_quantity) != count($abandoned_dates)) { continue; } unset($results_top_abandoned_product['abandoned_quantity']); unset($results_top_abandoned_product['abandoned_dates']); if ($group_by == "display_date_time") { unset($results_top_abandoned_product['comp_time']); } $abandoned_date_series = $date_series; if ($group_by == "display_date_time") { for ($i = 0; $i < sizeof($abandoned_dates_comp); $i++) { $abandoned_date_series[$abandoned_dates_comp[$i]]['post_date'] = $abandoned_dates[$i]; $abandoned_date_series[$abandoned_dates_comp[$i]]['sales'] = $abandoned_date_series[$abandoned_dates_comp[$i]]['sales'] + $abandoned_quantity[$i]; } } else { for ($i = 0; $i < sizeof($abandoned_dates); $i++) { $abandoned_date_series[$abandoned_dates[$i]]['sales'] = $abandoned_date_series[$abandoned_dates[$i]]['sales'] + $abandoned_quantity[$i]; } } $results_top_abandoned_product['graph_data'] = array(); foreach ($abandoned_date_series as $abandoned_date_series1) { $results_top_abandoned_product['graph_data'][] = $abandoned_date_series1; } $results_top_abandoned_product['price'] = get_post_meta($results_top_abandoned_product['id'], '_price', true) * $results_top_abandoned_product['abondoned_qty']; $results_top_abandoned_product['abondoned_qty'] = floatval($results_top_abandoned_product['abondoned_qty']); $abandoned_rate = $results_top_abandoned_product['abondoned_qty'] / $results_prod_abandoned_rate[$j] * 100; $results_top_abandoned_product['abandoned_rate'] = round($abandoned_rate, get_option('woocommerce_price_num_decimals')) . "%"; //Code for formatting the product name $post_parent = wp_get_post_parent_id($results_top_abandoned_product['id']); if ($post_parent > 0) { $results_top_abandoned_product['prod_name'] = get_the_title($post_parent) . " (" . $variation_attributes[$results_top_abandoned_product['id']] . ")"; } else { $results_top_abandoned_product['prod_name'] = get_the_title($results_top_abandoned_product['id']); } $total_prod_abandoned_qty = $total_prod_abandoned_qty + $results_top_abandoned_product['abondoned_qty']; $total_prod_qty = $total_prod_qty + $results_prod_abandoned_rate[$j]; $j++; } } $query_min_abandoned_date = "SELECT MIN(abandoned_cart_time) AS min_abandoned_date\n\t \t\t\t\t\t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items"; $results_min_abandoned_date = $wpdb->get_col($query_min_abandoned_date); $rows_min_abandoned_date = $wpdb->num_rows; $min_abandoned_date = ''; if ($results_min_abandoned_date[0] != '') { $min_abandoned_date = date('Y-m-d', (int) $results_min_abandoned_date[0]); } //Cumm Cart Abandonment Rate $query_total_cart = "SELECT COUNT(DISTINCT cart_id) as total_cart_count\n\t\t\t \t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items\n\t\t\t \t\t\tWHERE abandoned_cart_time >= " . strtotime($start_date) . " AND abandoned_cart_time <=" . strtotime($end_date_query); $total_cart_count = $wpdb->get_col($query_total_cart); $rows_total_cart = $wpdb->num_rows; $query_total_abandoned_cart = "SELECT COUNT(DISTINCT cart_id) as total_cart_abandoned_count\n\t\t\t\t\t\t \t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items\n\t\t\t\t\t\t \t\t\tWHERE abandoned_cart_time >= " . strtotime($start_date) . " AND abandoned_cart_time <=" . strtotime($end_date_query) . "\n\t\t\t\t\t\t \t\t\t\tAND product_abandoned = 1\n\t\t\t\t\t\t \t\t\t\tAND order_id IS NULL"; $total_abandoned_cart_count = $wpdb->get_col($query_total_abandoned_cart); $rows_total_abandoned_cart = $wpdb->num_rows; if ($rows_total_abandoned_cart > 0) { $cumm_cart_abandoned_rate = round($total_abandoned_cart_count[0] / $total_cart_count[0] * 100, get_option('woocommerce_price_num_decimals')); } else { $cumm_cart_abandoned_rate = 0; } //Sales Funnel $cumm_sales_funnel = array(); //Query to get the total products added to cart if ($rows_total_cart > 0) { $query_products_added_cart = "SELECT SUM(quantity) as total_prod_added_cart\n\t\t\t\t\t\t \t\t\tFROM {$wpdb->prefix}sr_woo_abandoned_items\n\t\t\t\t\t\t \t\t\tWHERE abandoned_cart_time BETWEEN '" . strtotime($start_date) . "' AND '" . strtotime($end_date_query) . "'"; $total_products_added_cart = $wpdb->get_col($query_products_added_cart); $cumm_sales_funnel['total_cart_count'] = floatval($total_cart_count[0]); $cumm_sales_funnel['total_products_added_cart'] = floatval($total_products_added_cart[0]); } else { $cumm_sales_funnel['total_cart_count'] = 0; $cumm_sales_funnel['total_products_added_cart'] = 0; } //Query to get the placed order ids $query_orders_placed = "SELECT DISTINCT id as completed_order_ids\n\t \t\t\t\t\t\t\tFROM {$wpdb->prefix}posts AS posts\n\t JOIN {$wpdb->prefix}term_relationships AS term_relationships \n\t ON term_relationships.object_id = posts.ID \n\t JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n\t ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n\t JOIN {$wpdb->prefix}terms AS terms \n\t ON term_taxonomy.term_id = terms.term_id\n\t\t WHERE posts.post_status IN ('publish')\n\t\t AND posts.post_type IN ('shop_order')\n\t\t AND (posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}')"; $results_orders_placed = $wpdb->get_col($query_orders_placed); $rows_orders_placed = $wpdb->num_rows; if ($rows_orders_placed > 0) { $cumm_sales_funnel['orders_placed_count'] = floatval(sizeof($results_orders_placed)); //Query to get the count of the products purchased $query_products_purchased = "SELECT SUM(quantity) as query_products_sold\n\t \t\t\t\t\t\t\tFROM {$wpdb->prefix}sr_woo_order_items\n\t \t\t\t\t\t\t\tWHERE order_id IN (" . implode(",", $results_orders_placed) . ")"; $results_products_purchased = $wpdb->get_col($query_products_purchased); $rows_products_purchased = $wpdb->num_rows; $cumm_sales_funnel['products_purchased_count'] = floatval($results_products_purchased[0]); } else { $cumm_sales_funnel['orders_placed_count'] = 0; $cumm_sales_funnel['products_purchased_count'] = 0; } //Query to get the completed order ids $query_orders_completed = "SELECT DISTINCT id as completed_order_ids\n\t \t\t\t\t\t\t\tFROM {$wpdb->prefix}posts AS posts\n\t JOIN {$wpdb->prefix}term_relationships AS term_relationships \n\t ON term_relationships.object_id = posts.ID \n\t JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n\t ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n\t JOIN {$wpdb->prefix}terms AS terms \n\t ON term_taxonomy.term_id = terms.term_id\n\t\t WHERE terms.name IN ('completed')\n\t\t AND posts.post_status IN ('publish')\n\t\t AND posts.post_type IN ('shop_order')\n\t\t AND (posts.post_date BETWEEN '{$start_date}' AND '{$end_date_query}')"; $results_orders_completed = $wpdb->get_col($query_orders_completed); $rows_orders_completed = $wpdb->num_rows; if ($rows_orders_completed > 0) { $cumm_sales_funnel['orders_completed_count'] = floatval(sizeof($results_orders_completed)); //Query to get the count of the products sold $query_products_sold = "SELECT SUM(quantity) as query_products_sold\n\t \t\t\t\t\t\t\tFROM {$wpdb->prefix}sr_woo_order_items\n\t \t\t\t\t\t\t\tWHERE order_id IN (" . implode(",", $results_orders_completed) . ")"; $results_products_sold = $wpdb->get_col($query_products_sold); $rows_products_sold = $wpdb->num_rows; $cumm_sales_funnel['products_sold_count'] = floatval($results_products_sold[0]); } else { $cumm_sales_funnel['orders_completed_count'] = 0; $cumm_sales_funnel['products_sold_count'] = 0; } if (isset($post['option'])) { // Condition to get the data when the Top Products Toggle button is clicked $results[0] = $cumm_top_prod_graph_data; } else { $results[0] = $monthly_sales; $results[1] = $total_monthly_sales; $results[2] = $results_top_prod; $results[3] = $results_cumm_top_cust; if ($total_monthly_sales == 0) { $results[4] = floatval(0); } else { if ($tot_cumm_orders == 0) { $results[] = floatval($total_monthly_sales); } else { $results[4] = floatval($total_monthly_sales / $tot_cumm_orders); } } if ($tot_cumm_orders_qty == 0) { $results[5] = floatval(0); } else { if ($total_cumm_cust_count == 0) { $results[5] = floatval($tot_cumm_orders_qty); } else { $results[5] = floatval($tot_cumm_orders_qty / $total_cumm_cust_count); } } $results[6] = floatval($max_sales + 100); if ($total_discount_sales > 0) { $results[7] = $cumm_discount_sales; $results[8] = $total_discount_sales; } else { $results[7] = ''; $results[8] = ''; } $results[9] = $results_cumm_coupon_count; $results[10] = floatval($max_discount_total + 100); $results[11] = $sr_per_order_containing_coupons; $results[12] = $results_top_payment_gateway; $results[13] = $tax_data; $results[14] = $results_top_abandoned_products; $results[15] = $min_abandoned_date != '' && $min_abandoned_date <= $start_date ? $cumm_cart_abandoned_rate : ''; $results[16] = $min_abandoned_date != '' && $min_abandoned_date <= $start_date ? $cumm_sales_funnel : ''; } return $results; }
/** * Google Analytics eCommerce tracking * * @access public * @param mixed $order_id * @return void */ function ecommerce_tracking_code($order_id) { global $woocommerce; if ($this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') || get_post_meta($order_id, '_ga_tracked', true) == 1) { return; } $tracking_id = $this->ga_id; if (!$tracking_id) { return; } // Doing eCommerce tracking so unhook standard tracking from the footer remove_action('wp_footer', array($this, 'google_tracking_code')); // Get the order and output tracking code $order = new WC_Order($order_id); $loggedin = is_user_logged_in() ? 'yes' : 'no'; if (is_user_logged_in()) { $user_id = get_current_user_id(); $current_user = get_user_by('id', $user_id); $username = $current_user->user_login; } else { $user_id = ''; $username = __('Guest', 'woocommerce'); } if (!empty($this->ga_set_domain_name)) { $set_domain_name = "['_setDomainName', '" . esc_js($this->ga_set_domain_name) . "'],"; } else { $set_domain_name = ''; } $code = "\r\n\t\t\tvar _gaq = _gaq || [];\r\n\r\n\t\t\t_gaq.push(\r\n\t\t\t\t['_setAccount', '" . esc_js($tracking_id) . "'], " . $set_domain_name . "\r\n\t\t\t\t['_setCustomVar', 1, 'logged-in', '" . esc_js($loggedin) . "', 1],\r\n\t\t\t\t['_trackPageview']\r\n\t\t\t);\r\n\r\n\t\t\t_gaq.push(['_addTrans',\r\n\t\t\t\t'" . esc_js($order->get_order_number()) . "', // order ID - required\r\n\t\t\t\t'" . esc_js(get_bloginfo('name')) . "', \t// affiliation or store name\r\n\t\t\t\t'" . esc_js($order->get_total()) . "', \t // total - required\r\n\t\t\t\t'" . esc_js($order->get_total_tax()) . "', // tax\r\n\t\t\t\t'" . esc_js($order->get_shipping()) . "',\t // shipping\r\n\t\t\t\t'" . esc_js($order->billing_city) . "', // city\r\n\t\t\t\t'" . esc_js($order->billing_state) . "', // state or province\r\n\t\t\t\t'" . esc_js($order->billing_country) . "' // country\r\n\t\t\t]);\r\n\t\t"; // Order items if ($order->get_items()) { foreach ($order->get_items() as $item) { $_product = $order->get_product_from_item($item); $code .= "_gaq.push(['_addItem',"; $code .= "'" . esc_js($order->get_order_number()) . "',"; $code .= "'" . esc_js($_product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $_product->get_sku() : $_product->id) . "',"; $code .= "'" . esc_js($item['name']) . "',"; if (isset($_product->variation_data)) { $code .= "'" . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . "',"; } else { $out = array(); $categories = get_the_terms($_product->id, 'product_cat'); if ($categories) { foreach ($categories as $category) { $out[] = $category->name; } } $code .= "'" . esc_js(join("/", $out)) . "',"; } $code .= "'" . esc_js($order->get_item_total($item, true, true)) . "',"; $code .= "'" . esc_js($item['qty']) . "'"; $code .= "]);"; } } $code .= "\r\n\t\t\t_gaq.push(['_trackTrans']); \t\t\t\t\t// submits transaction to the Analytics servers\r\n\r\n\t\t\t(function() {\r\n\t\t\t\tvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\r\n\t\t\t\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\r\n\t\t\t\tvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\r\n\t\t\t})();\r\n\t\t"; echo '<script type="text/javascript">' . $code . '</script>'; update_post_meta($order_id, '_ga_tracked', 1); }
function cg_header_add_to_cart_fragment($fragments) { global $woocommerce; global $cg_options; $cg_cart_icon_type = ''; if (isset($cg_options['cg_cart_icon_type'])) { $cg_cart_icon_type = $cg_options['cg_cart_icon_type']; } ob_start(); ?> <ul class="tiny-cart"> <li> <a class="cart_dropdown_link cart-parent" href="<?php echo $woocommerce->cart->get_cart_url(); ?> " title="<?php _e('View your shopping cart', 'commercegurus'); ?> "> <div class="cg-header-cart-icon-wrap"> <?php if ($cg_cart_icon_type == "bag") { ?> <div class="icon cg-icon-bag-shopping-2"></div> <?php } elseif ($cg_cart_icon_type == "basket") { ?> <div class="icon cg-icon-basket-1"></div> <?php } else { ?> <div class="icon cg-icon-shopping-1"></div> <?php } ?> <span class="cg-cart-count"><?php echo WC()->cart->cart_contents_count; ?> </span> </div> <span class='cart_subtotal'><?php echo $woocommerce->cart->get_cart_subtotal(); ?> </span> </a> <?php echo '<ul class="cart_list">'; if (sizeof($woocommerce->cart->cart_contents) > 0) { foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $_product = $cart_item['data']; if ($_product->exists() && $cart_item['quantity'] > 0) { echo '<li class="cart_list_product">'; ?> <div class="container"> <div class="row"> <div class="col-lg-2"> <?php echo apply_filters('woocommerce_cart_item_remove_link', sprintf('<a href="%s" class="cg-cart-remove" title="%s">x</a>', esc_url($woocommerce->cart->get_remove_url($cart_item_key)), __('Remove this item', 'woocommerce')), $cart_item_key); ?> </div> <div class="col-lg-10"> <?php echo '<a href="' . get_permalink($cart_item['product_id']) . '">'; echo $_product->get_image(); echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product) . '</a>'; if ($_product instanceof woocommerce_product_variation && is_array($cart_item['variation'])) { echo woocommerce_get_formatted_variation($cart_item['variation']); } echo '<span class="quantities">' . $cart_item['quantity'] . ' × ' . woocommerce_price($_product->get_price()) . '</span>'; ?> </div> </div> </div> </li> <?php } } } else { echo '<li class="empty">' . __('No products in the cart.', 'commercegurus') . '</li>'; } if (sizeof($woocommerce->cart->cart_contents) > 0) { echo '<li class="total"><strong>'; if (get_option('js_prices_include_tax') == 'yes') { _e('Total', 'commercegurus'); } else { _e('Subtotal', 'commercegurus'); } echo ':</strong>' . $woocommerce->cart->get_cart_subtotal(); '</li>'; echo '<li class="buttons"><a href="' . $woocommerce->cart->get_cart_url() . '" class="button">' . __('View Cart', 'commercegurus') . '</a> <a href="' . $woocommerce->cart->get_checkout_url() . '" class="button checkout">' . __('Checkout', 'commercegurus') . '</a></li>'; } echo '</ul>'; ?> </li> </ul> <?php $fragments['ul.tiny-cart'] = ob_get_clean(); return $fragments; }
/** * lesson_course_meta_box_content function. * * @access public * @return void */ public function lesson_course_meta_box_content() { global $post; // Setup Lesson Meta Data $selected_lesson_course = 0; if (0 < $post->ID) { $selected_lesson_course = get_post_meta($post->ID, '_lesson_course', true); } // End If Statement // Handle preselected course if (isset($_GET['course_id']) && 0 < absint($_GET['course_id'])) { $selected_lesson_course = absint($_GET['course_id']); } // End If Statement // Get the Lesson Posts $post_args = array('post_type' => 'course', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => 0); $posts_array = get_posts($post_args); // Buid the HTML to Output $html = ''; // Nonce $html .= wp_nonce_field('sensei-save-post-meta', 'woo_' . $this->token . '_nonce', true, false); // Select the course for the lesson $drop_down_args = array('name' => 'lesson_course', 'id' => 'lesson-course-options'); $courses = WooThemes_Sensei_Course::get_all_courses(); $courses_options = array(); foreach ($courses as $course) { $courses_options[$course->ID] = get_the_title($course); } $html .= Sensei_Utils::generate_drop_down($selected_lesson_course, $courses_options, $drop_down_args); // Course Actions Panel if (current_user_can('publish_courses')) { $html .= '<div id="lesson-course-actions">'; $html .= '<p>'; // Add a course action link $html .= '<a id="lesson-course-add" href="#course-add" class="lesson-add-course">+ ' . __('Add New Course', 'woothemes-sensei') . '</a>'; $html .= '</p>'; $html .= '</div>'; // Add a course input fields $html .= '<div id="lesson-course-details" class="hidden">'; $html .= '<p>'; // Course Title input $html .= '<label>' . __('Course Title', 'woothemes-sensei') . '</label> '; $html .= '<input type="text" id="course-title" name="course_title" value="" size="25" class="widefat" />'; // Course Description input $html .= '<label>' . __('Description', 'woothemes-sensei') . '</label> '; $html .= '<textarea rows="10" cols="40" id="course-content" name="course_content" value="" size="300" class="widefat"></textarea>'; // Course Prerequisite $html .= '<label>' . __('Course Prerequisite', 'woothemes-sensei') . '</label> '; $html .= '<select id="course-prerequisite-options" name="course_prerequisite" class="chosen_select widefat">' . "\n"; $html .= '<option value="">' . __('None', 'woothemes-sensei') . '</option>'; foreach ($posts_array as $post_item) { $html .= '<option value="' . esc_attr(absint($post_item->ID)) . '">' . esc_html($post_item->post_title) . '</option>' . "\n"; } // End For Loop $html .= '</select>' . "\n"; // Course Product if (Sensei_WC::is_woocommerce_active()) { // Get the Products $select_course_woocommerce_product = get_post_meta($post_item->ID, '_course_woocommerce_product', true); $product_args = array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'DESC', 'post_status' => array('publish', 'private', 'draft'), 'tax_query' => array(array('taxonomy' => 'product_type', 'field' => 'slug', 'terms' => array('variable', 'grouped'), 'operator' => 'NOT IN')), 'suppress_filters' => 0); $products_array = get_posts($product_args); $html .= '<label>' . __('WooCommerce Product', 'woothemes-sensei') . '</label> '; $html .= '<select id="course-woocommerce-product-options" name="course_woocommerce_product" class="chosen_select widefat">' . "\n"; $html .= '<option value="-">' . __('None', 'woothemes-sensei') . '</option>'; $prev_parent_id = 0; foreach ($products_array as $products_item) { if ('product_variation' == $products_item->post_type) { $product_object = get_product($products_item->ID); $parent_id = wp_get_post_parent_id($products_item->ID); $product_name = ucwords(woocommerce_get_formatted_variation($product_object->variation_data, true)); } else { $parent_id = false; $prev_parent_id = 0; $product_name = $products_item->post_title; } // Show variations in groups if ($parent_id && $parent_id != $prev_parent_id) { if (0 != $prev_parent_id) { $html .= '</optgroup>'; } $html .= '<optgroup label="' . get_the_title($parent_id) . '">'; $prev_parent_id = $parent_id; } elseif (!$parent_id && 0 == $prev_parent_id) { $html .= '</optgroup>'; } $html .= '<option value="' . esc_attr(absint($products_item->ID)) . '">' . esc_html($products_item->post_title) . '</option>' . "\n"; } // End For Loop $html .= '</select>' . "\n"; } else { // Default $html .= '<input type="hidden" name="course_woocommerce_product" id="course-woocommerce-product-options" value="-" />'; } // Course Category $html .= '<label>' . __('Course Category', 'woothemes-sensei') . '</label> '; $cat_args = array('echo' => false, 'hierarchical' => true, 'show_option_none' => __('None', 'woothemes-sensei'), 'taxonomy' => 'course-category', 'orderby' => 'name', 'id' => 'course-category-options', 'name' => 'course_category', 'class' => 'widefat'); $html .= wp_dropdown_categories(apply_filters('widget_course_categories_dropdown_args', $cat_args)) . "\n"; // Save the course action button $html .= '<a title="' . esc_attr(__('Save Course', 'woothemes-sensei')) . '" href="#add-course-metadata" class="lesson_course_save button button-highlighted">' . esc_html(__('Add Course', 'woothemes-sensei')) . '</a>'; $html .= ' '; // Cancel action link $html .= '<a href="#course-add-cancel" class="lesson_course_cancel">' . __('Cancel', 'woothemes-sensei') . '</a>'; $html .= '</p>'; $html .= '</div>'; } // End If Statement // Output the HTML echo $html; }
function get_data_woo($post, $offset, $limit, $is_export = false) { global $wpdb, $woocommerce, $post_status, $parent_sort_id, $order_by, $post_type, $variation_name, $from_variation, $parent_name, $attributes; $_POST = $post; // Fix: PHP 5.4 $products = array(); // getting the active module $active_module = isset($_POST['active_module']) ? $_POST['active_module'] : 'Products'; // $active_module = $_POST ['active_module']; variation_query_params(); // Restricting LIMIT for export CSV if ($is_export === true) { $limit_string = ""; $image_size = "full"; } else { $limit_string = "LIMIT {$offset},{$limit}"; $image_size = "thumbnail"; } $wpdb->query("SET SESSION group_concat_max_len=999999"); // To increase the max length of the Group Concat Functionality $view_columns = !empty($_POST['viewCols']) ? json_decode(stripslashes($_POST['viewCols'])) : ''; if ($active_module == 'Products') { // <-products $tax_status = array('taxable' => __('Taxable', $sm_text_domain), 'shipping' => __('Shipping only', $sm_text_domain), 'none' => __('None', $sm_text_domain)); if (isset($_POST['incVariation']) && $_POST['incVariation'] === 'true') { $show_variation = true; } else { $parent_name = ''; $post_status = "('publish', 'pending', 'draft')"; $post_type = "('product')"; $parent_sort_id = ''; $order_by = " ORDER BY {$wpdb->prefix}posts.id desc"; $show_variation = false; } // if max-join-size issue occurs $query = "SET SQL_BIG_SELECTS=1;"; $wpdb->query($query); //Query for getting all the distinct attribute meta key names $query_variation = "SELECT DISTINCT meta_key as variation\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key like 'attribute_%'"; $variation = $wpdb->get_col($query_variation); //Query to get all the distinct term names along with their slug names $query = "SELECT terms.slug as slug, terms.name as term_name FROM {$wpdb->prefix}terms AS terms\n JOIN {$wpdb->prefix}postmeta AS postmeta ON ( postmeta.meta_value = terms.slug AND postmeta.meta_key LIKE 'attribute_%' ) GROUP BY terms.slug"; $attributes_terms = $wpdb->get_results($query, 'ARRAY_A'); $attributes = array(); foreach ($attributes_terms as $attributes_term) { $attributes[$attributes_term['slug']] = $attributes_term['term_name']; } //Query to get the term_taxonomy_id for all the product categories $query_terms = "SELECT terms.name, wt.term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy AS wt\n JOIN {$wpdb->prefix}terms AS terms ON (wt.term_id = terms.term_id)\n WHERE wt.taxonomy like 'product_cat'"; $results = $wpdb->get_results($query_terms, 'ARRAY_A'); $rows_terms = $wpdb->num_rows; if (!empty($results)) { for ($i = 0; $i < sizeof($results); $i++) { $term_taxonomy_id[$i] = $results[$i]['term_taxonomy_id']; $term_taxonomy[$results[$i]['term_taxonomy_id']] = $results[$i]['name']; } //Imploding the term_taxonomy_id to be used in the main query of the products module $term_taxonomy_id_query = "AND wtr.term_taxonomy_id IN (" . implode(",", $term_taxonomy_id) . ")"; } else { $term_taxonomy_id_query = ''; } $results_trash = array(); //Code to get the ids of all the products whose post_status is thrash $query_trash = "SELECT ID FROM {$wpdb->prefix}posts \n WHERE post_status = 'trash'\n AND post_type IN ('product')"; $results_trash = $wpdb->get_col($query_trash); $rows_trash = $wpdb->num_rows; //Code to get the taxonomy id for 'simple' product_type $query_taxonomy_id = "SELECT taxonomy.term_taxonomy_id as term_taxonomy_id\n FROM {$wpdb->prefix}terms as terms\n JOIN {$wpdb->prefix}term_taxonomy as taxonomy ON (taxonomy.term_id = terms.term_id)\n WHERE taxonomy.taxonomy = 'product_type'\n AND terms.slug = 'variable'"; $variable_taxonomy_id = $wpdb->get_var($query_taxonomy_id); if (!empty($variable_taxonomy_id)) { $query_post_parent_not_variable = "SELECT distinct products.post_parent \n FROM {$wpdb->prefix}posts as products \n WHERE NOT EXISTS (SELECT * \n FROM {$wpdb->prefix}term_relationships \n WHERE object_id = products.post_parent\n AND term_taxonomy_id = " . $variable_taxonomy_id . ") \n AND products.post_parent > 0 \n AND products.post_type = 'product_variation'"; $results_post_parent_not_variable = $wpdb->get_col($query_post_parent_not_variable); $rows_post_parent_not_variable = $wpdb->num_rows; for ($i = sizeof($results_trash), $j = 0; $j < sizeof($results_post_parent_not_variable); $i++, $j++) { $results_trash[$i] = $results_post_parent_not_variable[$j]; } } if ($rows_trash > 0 || $rows_post_parent_not_variable > 0) { $trash_id = " AND {$wpdb->prefix}posts.post_parent NOT IN (" . implode(",", $results_trash) . ")"; } else { $trash_id = ""; } // Query to delete the unwanted '_regular_price' meta_key from variations if ($_POST['SM_IS_WOO16'] == "true") { $query_delete_variations = "DELETE FROM {$wpdb->prefix}postmeta \n WHERE meta_key = '_regular_price'\n AND post_id IN (SELECT id FROM {$wpdb->prefix}posts\n WHERE post_parent > 0\n AND post_type IN ('product_variation')\n AND post_status IN ('publish', 'pending', 'draft'))"; $wpdb->query($query_delete_variations); } //Code to get the attribute terms for all attributes $query_attribute_names = "SELECT terms.name AS attribute_terms,\n taxonomy.taxonomy as attribute_name,\n taxonomy.term_taxonomy_id as term_taxonomy_id\n FROM {$wpdb->prefix}terms as terms\n JOIN {$wpdb->prefix}term_taxonomy as taxonomy ON (taxonomy.term_id = terms.term_id)\n WHERE taxonomy.taxonomy LIKE 'pa_%'\n GROUP BY taxonomy.taxonomy, taxonomy.term_taxonomy_id"; $results_attribute_names = $wpdb->get_results($query_attribute_names, 'ARRAY_A'); $product_attributes = array(); $temp_attribute_nm = ""; foreach ($results_attribute_names as $results_attribute_name) { if ($results_attribute_name['attribute_name'] != $temp_attribute_nm) { $product_attributes[$results_attribute_name['attribute_name']] = array(); } $product_attributes[$results_attribute_name['attribute_name']][$results_attribute_name['term_taxonomy_id']] = $results_attribute_name['attribute_terms']; $temp_attribute_nm = $results_attribute_name['attribute_name']; } $query_attribute_label = "SELECT attribute_name, attribute_label\n FROM {$wpdb->prefix}woocommerce_attribute_taxonomies"; $results_attribute_label = $wpdb->get_results($query_attribute_label, 'ARRAY_A'); $attributes_label = array(); foreach ($results_attribute_label as $results_attribute_label1) { $attributes_label['pa_' . $results_attribute_label1['attribute_name']] = $results_attribute_label1['attribute_label']; } //Query to get the Category Ids $query_categories = "SELECT {$wpdb->prefix}posts.id as id,\n GROUP_CONCAT(distinct {$wpdb->prefix}term_relationships.term_taxonomy_id order by {$wpdb->prefix}term_relationships.object_id SEPARATOR ' #sm# ') AS term_taxonomy_id\n FROM {$wpdb->prefix}posts\n JOIN {$wpdb->prefix}term_relationships ON ({$wpdb->prefix}posts.id = {$wpdb->prefix}term_relationships.object_id)\n WHERE {$wpdb->prefix}posts.post_status IN {$post_status}\n AND {$wpdb->prefix}posts.post_type IN {$post_type}\n {$trash_id}\n GROUP BY id"; $records_categories = $wpdb->get_results($query_categories, 'ARRAY_A'); $category_ids_all = array(); foreach ($records_categories as $records_category) { $category_ids_all[$records_category['id']] = $records_category['term_taxonomy_id']; } if (!empty($_POST['SM_IS_WOO21']) && $_POST['SM_IS_WOO21'] == "true" || !empty($_POST['SM_IS_WOO22']) && $_POST['SM_IS_WOO22'] == "true") { $query_taxonomy_id_variable = "SELECT term_taxonomy.term_taxonomy_id\n FROM {$wpdb->prefix}term_taxonomy AS term_taxonomy \n JOIN {$wpdb->prefix}terms AS terms ON (term_taxonomy.term_id = terms.term_id)\n WHERE terms.name LIKE 'variable'"; $results_taxonomy_id_variable = $wpdb->get_col($query_taxonomy_id_variable); if (!empty($results_taxonomy_id_variable)) { $cond_taxonomy_id_variable = "AND term_relationships.term_taxonomy_id IN (" . implode(",", $results_taxonomy_id_variable) . ")"; } else { $cond_taxonomy_id_variable = ""; } $query_variation_reg_price = "SELECT postmeta.post_id as variation_parent_id,\n postmeta.meta_value as variation_id\n FROM {$wpdb->prefix}postmeta as postmeta\n JOIN {$wpdb->prefix}term_relationships AS term_relationships \n ON (term_relationships.object_id = postmeta.post_id)\n WHERE postmeta.meta_key IN ('_min_price_variation_id')\n {$cond_taxonomy_id_variable}\n GROUP BY postmeta.post_id"; $results_variation_reg_price = $wpdb->get_results($query_variation_reg_price, 'ARRAY_A'); $rows_variation_reg_price = $wpdb->num_rows; $variation_reg_price = array(); if ($rows_variation_reg_price > 0) { foreach ($results_variation_reg_price as $results_variation_reg_price1) { $variation_reg_price[$results_variation_reg_price1['variation_parent_id']] = $results_variation_reg_price1['variation_id']; } } } // GROUP_CONCAT(distinct wtr.term_taxonomy_id order by wtr.object_id SEPARATOR ' #sm# ') AS term_taxonomy_id, $post_meta_select = !empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo' ? ", GROUP_CONCAT({$wpdb->prefix}postmeta.meta_key order by {$wpdb->prefix}postmeta.meta_id SEPARATOR ' #sm# ') AS prod_othermeta_key\n , GROUP_CONCAT({$wpdb->prefix}postmeta.meta_value order by {$wpdb->prefix}postmeta.meta_id SEPARATOR ' #sm# ') AS prod_othermeta_value" : ""; $select = "SELECT SQL_CALC_FOUND_ROWS {$wpdb->prefix}posts.id,\n {$wpdb->prefix}posts.post_title,\n {$wpdb->prefix}posts.post_title as post_title_search,\n {$wpdb->prefix}posts.post_content,\n {$wpdb->prefix}posts.post_excerpt,\n {$wpdb->prefix}posts.post_status,\n {$wpdb->prefix}posts.post_parent\n {$post_meta_select}\n {$parent_sort_id}"; //Used as an alternative to the SQL_CALC_FOUND_ROWS function of MYSQL Database $select_count = "SELECT COUNT(*) as count"; // To get the count of the number of rows generated from the above select query $search = ""; $search_condn = ""; //Code to clear the advanced search temp table if (empty($_POST['search_query']) || empty($_POST['search_query'][0]) || !empty($_POST['searchText'])) { $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp"); delete_option('sm_advanced_search_query'); } $sm_advanced_search_results_persistent = 0; //flag to handle persistent search results //Code fo handling advanced search functionality if (!empty($_POST['search_query']) && !empty($_POST['search_query'][0]) || !empty($_POST['searchText'])) { if (empty($_POST['searchText'])) { $search_query_diff = get_option('sm_advanced_search_query') != '' ? array_diff($_POST['search_query'], get_option('sm_advanced_search_query')) : $_POST['search_query']; } else { $search_query_diff = ''; } if (!empty($search_query_diff)) { $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp"); // query to reset advanced search temp table $advanced_search_query = array(); $i = 0; update_option('sm_advanced_search_query', $_POST['search_query']); foreach ($_POST['search_query'] as $search_string_array) { $search_string_array = json_decode(stripslashes($search_string_array), true); if (is_array($search_string_array)) { $advanced_search_query[$i] = array(); $advanced_search_query[$i]['cond_posts'] = ''; $advanced_search_query[$i]['cond_postmeta'] = ''; $advanced_search_query[$i]['cond_terms'] = ''; $advanced_search_query[$i]['cond_postmeta_col_name'] = ''; $advanced_search_query[$i]['cond_postmeta_col_value'] = ''; $advanced_search_query[$i]['cond_postmeta_operator'] = ''; $advanced_search_query[$i]['cond_terms_col_name'] = ''; $advanced_search_query[$i]['cond_terms_col_value'] = ''; $advanced_search_query[$i]['cond_terms_operator'] = ''; $search_value_is_array = 0; //flag for array of search_values foreach ($search_string_array as $search_string) { $search_col = !empty($search_string['col_name']) ? $search_string['col_name'] : ''; $search_operator = !empty($search_string['operator']) ? $search_string['operator'] : ''; $search_data_type = !empty($search_string['type']) ? $search_string['type'] : 'string'; $search_value = !empty($search_string['value']) ? $search_string['value'] : ($search_data_type == "number" ? '0' : ''); if (!empty($search_string['table_name']) && $search_string['table_name'] == $wpdb->prefix . 'posts') { if ($search_data_type == "number") { $advanced_search_query[$i]['cond_posts'] .= $search_string['table_name'] . "." . $search_col . " " . $search_operator . " " . $search_value; } else { if ($search_operator == 'is') { $advanced_search_query[$i]['cond_posts'] .= $search_string['table_name'] . "." . $search_col . " LIKE '" . $search_value . "'"; } else { if ($search_operator == 'is not') { $advanced_search_query[$i]['cond_posts'] .= $search_string['table_name'] . "." . $search_col . " NOT LIKE '" . $search_value . "'"; } else { $advanced_search_query[$i]['cond_posts'] .= $search_string['table_name'] . "." . $search_col . " " . $search_operator . "'%" . $search_value . "%'"; } } } $advanced_search_query[$i]['cond_posts'] .= " AND "; } else { if (!empty($search_string['table_name']) && $search_string['table_name'] == $wpdb->prefix . 'postmeta') { if ($search_col == '_tax_status' && $search_value == 'Shipping only') { $search_value = 'shipping'; } else { if ($search_col == '_visibility' && $search_value == 'Catalog & Search') { $search_value = 'visible'; } } $advanced_search_query[$i]['cond_postmeta_col_name'] .= $search_col; $advanced_search_query[$i]['cond_postmeta_col_value'] .= $search_value; if ($search_data_type == "number") { $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value " . $search_operator . " " . $search_value . " )"; $advanced_search_query[$i]['cond_postmeta_operator'] .= $search_operator; } else { if ($search_operator == 'is') { $advanced_search_query[$i]['cond_postmeta_operator'] .= 'LIKE'; if ($search_col == '_product_attributes') { $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value LIKE '%" . $search_value . "%'" . " )"; } else { $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value LIKE '" . $search_value . "'" . " )"; } } else { if ($search_operator == 'is not') { $advanced_search_query[$i]['cond_postmeta_operator'] .= 'NOT LIKE'; if ($search_col == '_product_attributes') { $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value NOT LIKE '%" . $search_value . "%'" . " )"; } else { $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value NOT LIKE '" . $search_value . "'" . " )"; } } else { $advanced_search_query[$i]['cond_postmeta_operator'] .= $search_operator; $advanced_search_query[$i]['cond_postmeta'] .= " ( " . $search_string['table_name'] . ".meta_key LIKE '" . $search_col . "' AND " . $search_string['table_name'] . ".meta_value " . $search_operator . "'%" . $search_value . "%'" . " )"; } } } $advanced_search_query[$i]['cond_postmeta'] .= " AND "; $advanced_search_query[$i]['cond_postmeta_col_name'] .= " AND "; $advanced_search_query[$i]['cond_postmeta_col_value'] .= " AND "; $advanced_search_query[$i]['cond_postmeta_operator'] .= " AND "; } else { if (!empty($search_string['table_name']) && $search_string['table_name'] == $wpdb->prefix . 'term_relationships') { $advanced_search_query[$i]['cond_terms_col_name'] .= $search_col; $advanced_search_query[$i]['cond_terms_col_value'] .= $search_value; if ($search_operator == 'is') { $advanced_search_query[$i]['cond_terms'] .= " ( " . $wpdb->prefix . "term_taxonomy.taxonomy LIKE '" . $search_col . "' AND " . $wpdb->prefix . "terms.slug LIKE '" . $search_value . "'" . " )"; $advanced_search_query[$i]['cond_terms_operator'] .= 'LIKE'; } else { if ($search_operator == 'is not') { $advanced_search_query[$i]['cond_terms'] .= " ( " . $wpdb->prefix . "term_taxonomy.taxonomy LIKE '" . $search_col . "' AND " . $wpdb->prefix . "terms.slug NOT LIKE '" . $search_value . "'" . " )"; $advanced_search_query[$i]['cond_terms_operator'] .= 'NOT LIKE'; } else { $advanced_search_query[$i]['cond_terms'] .= " ( " . $wpdb->prefix . "term_taxonomy.taxonomy LIKE '" . $search_col . "' AND " . $wpdb->prefix . "terms.slug " . $search_operator . "'%" . $search_value . "%'" . " )"; $advanced_search_query[$i]['cond_terms_operator'] .= $search_operator; } } $advanced_search_query[$i]['cond_terms'] .= " AND "; $advanced_search_query[$i]['cond_terms_col_name'] .= " AND "; $advanced_search_query[$i]['cond_terms_col_value'] .= " AND "; $advanced_search_query[$i]['cond_terms_operator'] .= " AND "; } } } } $advanced_search_query[$i]['cond_posts'] = !empty($advanced_search_query[$i]['cond_posts']) ? substr($advanced_search_query[$i]['cond_posts'], 0, -4) : ''; $advanced_search_query[$i]['cond_postmeta'] = !empty($advanced_search_query[$i]['cond_postmeta']) ? substr($advanced_search_query[$i]['cond_postmeta'], 0, -4) : ''; $advanced_search_query[$i]['cond_terms'] = !empty($advanced_search_query[$i]['cond_terms']) ? substr($advanced_search_query[$i]['cond_terms'], 0, -4) : ''; $advanced_search_query[$i]['cond_postmeta_col_name'] = !empty($advanced_search_query[$i]['cond_postmeta_col_name']) ? substr($advanced_search_query[$i]['cond_postmeta_col_name'], 0, -4) : ''; $advanced_search_query[$i]['cond_postmeta_col_value'] = !empty($advanced_search_query[$i]['cond_postmeta_col_value']) ? substr($advanced_search_query[$i]['cond_postmeta_col_value'], 0, -4) : ''; $advanced_search_query[$i]['cond_postmeta_operator'] = !empty($advanced_search_query[$i]['cond_postmeta_operator']) ? substr($advanced_search_query[$i]['cond_postmeta_operator'], 0, -4) : ''; $advanced_search_query[$i]['cond_terms_col_name'] = !empty($advanced_search_query[$i]['cond_terms_col_name']) ? substr($advanced_search_query[$i]['cond_terms_col_name'], 0, -4) : ''; $advanced_search_query[$i]['cond_terms_col_value'] = !empty($advanced_search_query[$i]['cond_terms_col_value']) ? substr($advanced_search_query[$i]['cond_terms_col_value'], 0, -4) : ''; $advanced_search_query[$i]['cond_terms_operator'] = !empty($advanced_search_query[$i]['cond_terms_operator']) ? substr($advanced_search_query[$i]['cond_terms_operator'], 0, -4) : ''; } $i++; } } else { if (!empty($_POST['searchText'])) { if (substr($_POST['searchText'], 0, 2) == '\\"' && substr($_POST['searchText'], -2) == '\\"') { $search_string = substr($_POST['searchText'], 2, strlen($_POST['searchText']) - 4); $advanced_search_query[0]['cond_posts'] = $wpdb->prefix . 'posts' . ".id = '" . $search_string . "'"; $advanced_search_query[1]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_title = '" . $search_string . "'"; $advanced_search_query[2]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_status = '" . $search_string . "'"; $advanced_search_query[3]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_content = '" . $search_string . "'"; $advanced_search_query[4]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_excerpt = '" . $search_string . "'"; $advanced_search_query[5]['cond_postmeta'] = $wpdb->prefix . 'postmeta' . ".meta_value = '" . $search_string . "'"; $advanced_search_query[6]['cond_terms'] = $wpdb->prefix . "term_taxonomy.taxonomy = '" . $search_string . "'"; $advanced_search_query[7]['cond_terms'] = $wpdb->prefix . "terms.slug = '" . $search_string . "'"; $advanced_search_query[8]['cond_terms'] = $wpdb->prefix . "terms.name = '" . $search_string . "'"; } else { $advanced_search_query[0]['cond_posts'] = $wpdb->prefix . 'posts' . ".id LIKE '" . $_POST['searchText'] . "'"; $advanced_search_query[1]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_title LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[2]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_status LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[3]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_content LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[4]['cond_posts'] = $wpdb->prefix . 'posts' . ".post_excerpt LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[5]['cond_postmeta'] = $wpdb->prefix . 'postmeta' . ".meta_value LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[6]['cond_terms'] = $wpdb->prefix . "term_taxonomy.taxonomy LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[7]['cond_terms'] = $wpdb->prefix . "terms.slug LIKE '%" . $_POST['searchText'] . "%'"; $advanced_search_query[8]['cond_terms'] = $wpdb->prefix . "terms.name LIKE '%" . $_POST['searchText'] . "%'"; } } else { $sm_advanced_search_results_persistent = 1; } } } // if (isset ( $_POST ['searchText'] ) && $_POST ['searchText'] != '') { // $search_on = trim ( $_POST ['searchText'] ); // $search = ""; // $product_type = wp_get_object_terms($records[$i]['id'], 'product_type', array('fields' => 'slugs')); // $count_all_double_quote = substr_count( $search_on, '"' ); // if ( $count_all_double_quote > 0 ) { // $search_ons = array_filter( array_map( 'trim', explode( $wpdb->_real_escape( '"' ), $search_on ) ) ); // $search_on = implode(",", $search_ons); // } else { // $search_on = $wpdb->_real_escape( $search_on ); // $search_ons = explode( ' ', $search_on ); // } // //Function to prepare the conditions for the query // function prepare_cond($search_ons,$column_nm) { // $cond = ""; // foreach ($search_ons as $search_on) { // $cond .= $column_nm . " LIKE '%" . $search_on . "%'"; // $cond .= " OR "; // } // return substr( $cond, 0, -3 ); // }; // //Query for getting the slug name for the term name typed in the search box of the products module // $query_terms = "SELECT slug FROM {$wpdb->prefix}terms WHERE (". prepare_cond($search_ons,"name") .") AND name IN ('" .implode("','",$attributes) . "');"; // $records_slug = $wpdb->get_col ( $query_terms ); // $rows = $wpdb->num_rows; // $search_text = $search_ons; // // if($rows > 0){ // if($rows > 0 && (!empty($records_slug))){ // $search_text = $records_slug; // } // //Query to get the term_taxonomy_id for the category name typed in the search text box of the products module // $query_category = "SELECT tr.object_id FROM {$wpdb->prefix}term_relationships AS tr // JOIN {$wpdb->prefix}term_taxonomy AS wt ON (wt.term_taxonomy_id = tr.term_taxonomy_id) // JOIN {$wpdb->prefix}terms AS terms ON (wt.term_id = terms.term_id) // WHERE wt.taxonomy like 'product_cat' // AND (". prepare_cond($search_ons,"terms.name") . ")"; // $results_category = $wpdb->get_col( $query_category ); // $rows_category = $wpdb->num_rows; // if ($rows_category > 0) { // $search_category = " OR products.ID IN (" .implode(",",$results_category). ") OR products.post_parent IN (" .implode(",",$results_category). ")"; // } // else { // $search_category = ""; // } // //Query to get the post id if title or status or content or excerpt matches // $query_title = "SELECT ID FROM {$wpdb->prefix}posts // WHERE post_type IN ('product') // AND (". prepare_cond($search_ons,"post_title"). // "OR ". prepare_cond($search_ons,"post_status"). // "OR ". prepare_cond($search_ons,"post_content"). // "OR ". prepare_cond($search_ons,"post_excerpt") .")"; // $results_title = $wpdb->get_col( $query_title ); // $rows_title = $wpdb->num_rows; // if ($rows_title > 0) { // $search_title = " OR products.ID IN (" .implode(",",$results_title). ") OR products.post_parent IN (" .implode(",",$results_title). ")"; // } // else { // $search_title = ""; // } // $visible = stristr("Catalog & Search", $search_on); // if ($visible === FALSE) { // $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta // WHERE meta_key IN ('_tax_status','_visibility') // AND meta_value LIKE '%$search_on%'"; // } // else { // if(count( $search_ons ) > 1) { // $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta // WHERE meta_key IN ('_visibility') // AND (meta_value LIKE '%visible%')"; // } // else { // $query_tax_visible = "SELECT post_id FROM {$wpdb->prefix}postmeta // WHERE meta_key IN ('_visibility') // AND (meta_value LIKE '%visible%' // OR meta_value LIKE '%$search_on%')"; // } // } // $results_tax_visible = $wpdb->get_col( $query_tax_visible ); // $rows_tax_visible = $wpdb->num_rows; // if ($rows_tax_visible > 0) { // $search_tax_visible = " OR products.ID IN (" .implode(",",$results_tax_visible). ") OR products.post_parent IN (" .implode(",",$results_tax_visible). ")"; // } // else { // $search_tax_visible = ""; // } // if ( is_array( $search_ons ) && count( $search_ons ) >= 1 ) { // $search_condn = " HAVING "; // foreach ( $search_ons as $search_on ) { // $search_condn .= " (concat(' ',REPLACE(REPLACE(post_title_search,'(',''),')','')) LIKE '%$search_on%' // OR post_content LIKE '%$search_on%' // OR post_excerpt LIKE '%$search_on%' // OR prod_othermeta_value LIKE '%$search_on%') // "; // $search_condn .= " OR"; // } // if( $rows == 1 ) { // $query_ids1 = "SELECT GROUP_CONCAT(post_id ORDER BY post_id SEPARATOR ',') as id FROM {$wpdb->prefix}postmeta WHERE meta_value IN ('". implode("','",$search_text) ."') AND meta_key like 'attribute_%'"; // $records_id1 = implode(",",$wpdb->get_col ( $query_ids1 )); // $search_condn .= " products.id IN ($records_id1)"; // $search_condn .= " OR"; // } // $search_condn_count = " AND(" . substr( $search_condn_count, 0, -2 ) . ")"; // $search_condn = substr( $search_condn, 0, -2 ); // $search_condn .= $search_title . $search_category .$search_tax_visible; // } // else { // $search_condn = " HAVING concat(' ',REPLACE(REPLACE(post_title_search,'(',''),')','')) LIKE '%$search_on%' // OR post_content LIKE '%$search_on%' // OR post_excerpt LIKE '%$search_on%' // OR prod_othermeta_value LIKE '%$search_on%' // $search_title // $search_category // $search_tax_visible // "; // if( $rows == 1 ) { // $query_ids1 = "SELECT GROUP_CONCAT(post_id ORDER BY post_id SEPARATOR ',') as id FROM {$wpdb->prefix}postmeta WHERE meta_value IN ('". implode("','",$search_text) ."') AND meta_key like 'attribute_%';"; // $records_id1 = implode(",",$wpdb->get_col ( $query_ids1 )); // if ( !empty( $records_id1 ) ) // $search_condn .= " OR products.id IN ($records_id1)"; // } // } // } //Code for handling advanced search conditions if (!empty($advanced_search_query)) { $index_search_string = 1; // index to keep a track of flags in the advanced search temp foreach ($advanced_search_query as &$advanced_search_query_string) { //Condn for terms if (!empty($advanced_search_query_string['cond_terms'])) { $cond_terms_array = explode(" AND ", $advanced_search_query_string['cond_terms']); $cond_terms_col_name = !empty($advanced_search_query_string['cond_terms_col_name']) ? explode(" AND ", $advanced_search_query_string['cond_terms_col_name']) : ''; $cond_terms_col_value = !empty($advanced_search_query_string['cond_terms_col_value']) ? explode(" AND ", $advanced_search_query_string['cond_terms_col_value']) : ''; $cond_terms_operator = !empty($advanced_search_query_string['cond_terms_operator']) ? explode(" AND ", $advanced_search_query_string['cond_terms_operator']) : ''; $cond_terms_post_ids = ''; $cond_cat_post_ids = array(); // array for storing the cat post ids $index = 0; $terms_cat_search_taxonomy_ids = array(); $terms_att_search_flag = 0; $query_terms_search_count_array = array(); $terms_advanced_search_from = ''; $terms_advanced_search_where = ''; $result_terms_search = ''; foreach ($cond_terms_array as $cond_terms) { $query_advanced_search_taxonomy_id = "SELECT {$wpdb->prefix}term_taxonomy.term_taxonomy_id\n FROM {$wpdb->prefix}term_taxonomy\n JOIN {$wpdb->prefix}terms\n ON ( {$wpdb->prefix}terms.term_id = {$wpdb->prefix}term_taxonomy.term_id)\n WHERE " . $cond_terms; $result_advanced_search_taxonomy_id = $wpdb->get_col($query_advanced_search_taxonomy_id); if (!empty($result_advanced_search_taxonomy_id)) { $terms_search_result_flag = $index == sizeof($cond_terms_array) - 1 ? ', ' . $index_search_string : ', 0'; $terms_advanced_search_select = "SELECT DISTINCT {$wpdb->prefix}posts.id " . $terms_search_result_flag; //code for getting the post ids for attributes if (!empty($cond_terms_col_name[$index]) && trim($cond_terms_col_name[$index]) != 'product_cat') { $terms_advanced_search_select .= " ,0"; $terms_advanced_search_from = " FROM {$wpdb->prefix}posts\n LEFT JOIN {$wpdb->prefix}term_relationships\n ON ({$wpdb->prefix}term_relationships.object_id = {$wpdb->prefix}posts.id)\n JOIN {$wpdb->prefix}postmeta\n ON ( {$wpdb->prefix}postmeta.post_id = {$wpdb->prefix}posts.id)"; $terms_advanced_search_where = "WHERE (({$wpdb->prefix}term_relationships.term_taxonomy_id IN (" . implode(",", $result_advanced_search_taxonomy_id) . ")\n AND {$wpdb->prefix}posts.id NOT IN (SELECT post_parent FROM {$wpdb->prefix}posts \n WHERE post_parent > 0 ) )\n OR ({$wpdb->prefix}postmeta.meta_key LIKE 'attribute_" . trim($cond_terms_col_name[$index]) . "' AND {$wpdb->prefix}postmeta.meta_value " . $cond_terms_operator[$index] . " '" . trim($cond_terms_col_value[$index]) . "'))"; //Flag to handle the child ids for cat advanced search $terms_att_search_flag = 1; } else { if (!empty($cond_terms_col_name[$index]) && trim($cond_terms_col_name[$index]) == 'product_cat') { $terms_advanced_search_select .= " ,1"; $terms_advanced_search_from = "FROM {$wpdb->prefix}posts\n JOIN {$wpdb->prefix}term_relationships\n ON ({$wpdb->prefix}term_relationships.object_id = {$wpdb->prefix}posts.id)"; $terms_advanced_search_where = "WHERE {$wpdb->prefix}term_relationships.term_taxonomy_id IN (" . implode(",", $result_advanced_search_taxonomy_id) . ")"; } } //Query to find if there are any previous conditions $count_temp_previous_cond = $wpdb->query("UPDATE {$wpdb->base_prefix}sm_advanced_search_temp \n SET flag = 0\n WHERE flag = " . $index_search_string); //Code to handle condition if the ids of previous cond are present in temp table if ($index == 0 && $count_temp_previous_cond > 0 || !empty($result_terms_search)) { $terms_advanced_search_from .= " JOIN {$wpdb->base_prefix}sm_advanced_search_temp\n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}posts.id)"; $terms_advanced_search_where .= "AND {$wpdb->base_prefix}sm_advanced_search_temp.flag = 0"; } $result_terms_search = array(); if (!empty($terms_advanced_search_select) && !empty($terms_advanced_search_from) && !empty($terms_advanced_search_where)) { $query_terms_search = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n (" . $terms_advanced_search_select . " " . $terms_advanced_search_from . " " . $terms_advanced_search_where . " " . ")"; $result_terms_search = $wpdb->query($query_terms_search); } //Code to handle child ids in case of category search if (!empty($result_terms_search) && trim($cond_terms_col_name[$index]) == 'product_cat') { //query when attr cond has been applied if ($terms_att_search_flag == 1) { $query_terms_search_cat_child = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n ( SELECT {$wpdb->prefix}posts.id " . $terms_search_result_flag . " ,1\n FROM {$wpdb->prefix}posts\n JOIN {$wpdb->base_prefix}sm_advanced_search_temp AS temp1\n ON (temp1.product_id = {$wpdb->prefix}posts.id)\n JOIN {$wpdb->base_prefix}sm_advanced_search_temp AS temp2\n ON (temp2.product_id = {$wpdb->prefix}posts.post_parent)\n WHERE temp2.cat_flag = 1 )"; } else { //query when no attr cond has been applied $query_terms_search_cat_child = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n ( SELECT {$wpdb->prefix}posts.id " . $terms_search_result_flag . " ,1\n FROM {$wpdb->prefix}posts \n JOIN {$wpdb->base_prefix}sm_advanced_search_temp\n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}posts.post_parent)\n WHERE {$wpdb->base_prefix}sm_advanced_search_temp.cat_flag = 1 )"; } $result_terms_search_cat_child = $wpdb->query($query_terms_search_cat_child); } } $index++; } //Query to reset the cat_flag $wpdb->query("UPDATE {$wpdb->base_prefix}sm_advanced_search_temp SET cat_flag = 0"); //Code to delete the unwanted post_ids $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp WHERE flag = 0"); } //Cond for postmeta if (!empty($advanced_search_query_string['cond_postmeta'])) { $cond_postmeta_array = explode(" AND ", $advanced_search_query_string['cond_postmeta']); $cond_postmeta_col_name = !empty($advanced_search_query_string['cond_postmeta_col_name']) ? explode(" AND ", $advanced_search_query_string['cond_postmeta_col_name']) : ''; $cond_postmeta_col_value = !empty($advanced_search_query_string['cond_postmeta_col_value']) ? explode(" AND ", $advanced_search_query_string['cond_postmeta_col_value']) : ''; $cond_postmeta_operator = !empty($advanced_search_query_string['cond_postmeta_operator']) ? explode(" AND ", $advanced_search_query_string['cond_postmeta_operator']) : ''; $index = 0; $cond_postmeta_post_ids = ''; $result_postmeta_search = ''; foreach ($cond_postmeta_array as $cond_postmeta) { $postmeta_advanced_search_from = ''; $postmeta_advanced_search_where = ''; $cond_postmeta_col_name1 = !empty($cond_postmeta_col_name[$index]) ? trim($cond_postmeta_col_name[$index]) : ''; $cond_postmeta_col_value1 = !empty($cond_postmeta_col_value[$index]) ? trim($cond_postmeta_col_value[$index]) : ''; $cond_postmeta_operator1 = !empty($cond_postmeta_operator[$index]) ? trim($cond_postmeta_operator[$index]) : ''; $cond_postmeta_custom_att = $cond_postmeta_col_name1 == '_product_attributes' ? " OR (wp_postmeta.meta_key LIKE 'attribute%' AND wp_postmeta.meta_value " . $cond_postmeta_operator1 . " '%" . $cond_postmeta_col_value1 . "%')" : ''; $postmeta_search_result_flag = $index == sizeof($cond_postmeta_array) - 1 ? ', ' . $index_search_string : ', 0'; //Query to find if there are any previous conditions $count_temp_previous_cond = $wpdb->query("UPDATE {$wpdb->base_prefix}sm_advanced_search_temp \n SET flag = 0\n WHERE flag = " . $index_search_string); //Code to handle condition if the ids of previous cond are present in temp table if ($index == 0 && $count_temp_previous_cond > 0 || !empty($result_postmeta_search)) { $postmeta_advanced_search_from = " JOIN {$wpdb->base_prefix}sm_advanced_search_temp\n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}postmeta.post_id)"; $postmeta_advanced_search_where = " AND {$wpdb->base_prefix}sm_advanced_search_temp.flag = 0"; } $query_postmeta_search = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n (SELECT DISTINCT {$wpdb->prefix}postmeta.post_id " . $postmeta_search_result_flag . " ,0\n FROM {$wpdb->prefix}postmeta " . $postmeta_advanced_search_from . "\n WHERE " . $cond_postmeta . " " . $cond_postmeta_custom_att . " " . $postmeta_advanced_search_where . ")"; $result_postmeta_search = $wpdb->query($query_postmeta_search); $index++; } //Query to delete the unwanted post_ids $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp WHERE flag = 0"); } //Cond for posts if (!empty($advanced_search_query_string['cond_posts'])) { $cond_posts_array = explode(" AND ", $advanced_search_query_string['cond_posts']); $index = 0; $cond_posts_post_ids = ''; $result_posts_search = ''; foreach ($cond_posts_array as $cond_posts) { $posts_advanced_search_from = ''; $posts_advanced_search_where = ''; $posts_search_result_flag = $index == sizeof($cond_posts_array) - 1 ? ', ' . $index_search_string : ', 0'; $posts_search_result_cat_flag = $index == sizeof($cond_posts_array) - 1 ? ", 999" : ', 0'; //Query to find if there are any previous conditions $count_temp_previous_cond = $wpdb->query("UPDATE {$wpdb->base_prefix}sm_advanced_search_temp \n SET flag = 0\n WHERE flag = " . $index_search_string); //Code to handle condition if the ids of previous cond are present in temp table if ($index == 0 && $count_temp_previous_cond > 0 || !empty($result_posts_search)) { $posts_advanced_search_from = " JOIN {$wpdb->base_prefix}sm_advanced_search_temp\n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}posts.id)"; $posts_advanced_search_where = " AND {$wpdb->base_prefix}sm_advanced_search_temp.flag = 0"; } $query_posts_search = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n (SELECT DISTINCT {$wpdb->prefix}posts.id " . $posts_search_result_flag . " " . $posts_search_result_cat_flag . "\n FROM {$wpdb->prefix}posts " . $posts_advanced_search_from . "\n WHERE " . $cond_posts . " " . $posts_advanced_search_where . ")"; $result_posts_search = $wpdb->query($query_posts_search); $index++; } //Query to get the variations of the parent product in result set $query_posts_search = "REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp\n (SELECT DISTINCT {$wpdb->prefix}posts.id ," . $index_search_string . ", 0\n FROM {$wpdb->prefix}posts \n JOIN {$wpdb->base_prefix}sm_advanced_search_temp \n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}posts.post_parent)\n WHERE {$wpdb->base_prefix}sm_advanced_search_temp.cat_flag = 999\n AND {$wpdb->base_prefix}sm_advanced_search_temp.flag = " . $index_search_string . ")"; $result_posts_search = $wpdb->query($query_posts_search); //Query to delete the unwanted post_ids $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp WHERE flag = 0"); } $index_search_string++; } } //for combined $advanced_search_from = ''; $advanced_search_where = ''; if (!empty($advanced_search_query) || $sm_advanced_search_results_persistent == 1) { $advanced_search_from = " JOIN {$wpdb->base_prefix}sm_advanced_search_temp\n ON ({$wpdb->base_prefix}sm_advanced_search_temp.product_id = {$wpdb->prefix}posts.id)"; $advanced_search_where = " AND {$wpdb->base_prefix}sm_advanced_search_temp.flag > 0"; } $from = "FROM {$wpdb->prefix}posts"; // and // {$wpdb->prefix}postmeta.meta_key IN ('_regular_price','_sale_price','_sale_price_dates_from','_sale_price_dates_to','_sku','_stock','_weight','_height','_length','_width','_price','_thumbnail_id','_tax_status','_min_variation_regular_price','_min_variation_sale_price','_min_variation_price','_visibility','_product_attributes','" . implode( "','", $variation ) . "') $from_export = "FROM {$wpdb->prefix}posts\n JOIN {$wpdb->prefix}postmeta ON ({$wpdb->prefix}postmeta.post_id = {$wpdb->prefix}posts.id)"; $where = " WHERE {$wpdb->prefix}posts.post_status IN {$post_status}\n AND {$wpdb->prefix}posts.post_type IN {$post_type}\n {$trash_id}\n {$search}"; $group_by = " GROUP BY {$wpdb->prefix}posts.id "; //Query for getting the actual data loaded into the smartManager $query = !empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo' ? "{$select} {$from_export} {$advanced_search_from} {$where} {$advanced_search_where} {$group_by} {$search_condn} {$order_by} {$limit_string};" : "{$select} {$from} {$advanced_search_from} {$where} {$advanced_search_where} {$group_by} {$search_condn} {$order_by} {$limit_string};"; // $query = "$select $from_export $where $group_by $search_condn $order_by"; $records = $wpdb->get_results($query, 'ARRAY_A'); $num_rows = $wpdb->num_rows; //Query for getting the count of the number of products loaded into the smartManager $recordcount_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $recordcount_result[0]['count']; if ($num_rows <= 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', $sm_text_domain); } else { if (empty($_POST['func_nm'])) { $post_ids = array(); foreach ($records as $record) { $post_ids[] = $record['id']; } // and // prod_othermeta.meta_key IN ('_regular_price','_sale_price','_sale_price_dates_from','_sale_price_dates_to','_sku','_stock','_weight','_height','_length','_width','_price','_thumbnail_id','_tax_status','_min_variation_regular_price','_min_variation_sale_price','_min_variation_price','_visibility','_product_attributes','" . implode( "','", $variation ) . "') $query_postmeta = "SELECT prod_othermeta.post_id as post_id,\n\n GROUP_CONCAT(prod_othermeta.meta_key order by prod_othermeta.meta_id SEPARATOR ' #sm# ') AS prod_othermeta_key,\n GROUP_CONCAT(prod_othermeta.meta_value order by prod_othermeta.meta_id SEPARATOR ' #sm# ') AS prod_othermeta_value\n FROM {$wpdb->prefix}postmeta as prod_othermeta \n WHERE post_id IN (" . implode(",", $post_ids) . ") \n GROUP BY post_id"; $records_postmeta = $wpdb->get_results($query_postmeta, 'ARRAY_A'); $products_meta_data = array(); foreach ($records_postmeta as $record_postmeta) { $products_meta_data[$record_postmeta['post_id']] = array(); $products_meta_data[$record_postmeta['post_id']]['prod_othermeta_key'] = $record_postmeta['prod_othermeta_key']; $products_meta_data[$record_postmeta['post_id']]['prod_othermeta_value'] = $record_postmeta['prod_othermeta_value']; } foreach ($records as &$record) { $record['prod_othermeta_key'] = $products_meta_data[$record['id']]['prod_othermeta_key']; $record['prod_othermeta_value'] = $products_meta_data[$record['id']]['prod_othermeta_value']; } } $export_column_header = array(); for ($i = 0; $i < $num_rows; $i++) { // $records[$i]['post_content'] = str_replace('"','\'',$records[$i]['post_content']); // $records[$i]['post_excerpt'] = str_replace('"','\'',$records[$i]['post_excerpt']); // $records[$i]['post_excerpt'] = json_encode(addslashes($records[$i]['post_excerpt'])); //$records[$i]['post_content'] = json_encode(addslashes($records[$i]['post_content'])); //$records[$i]['post_excerpt'] = htmlspecialchars($records[$i]['post_excerpt']); //$records[$i]['post_content'] = htmlspecialchars($records[$i]['post_content']); $records[$i]['post_excerpt'] = ''; $records[$i]['post_content'] = ''; $prod_meta_values = explode(' #sm# ', $records[$i]['prod_othermeta_value']); $prod_meta_key = explode(' #sm# ', $records[$i]['prod_othermeta_key']); if (count($prod_meta_values) != count($prod_meta_key)) { continue; } if (!empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo' && empty($export_column_header)) { $export_column_header = $prod_meta_key; } unset($records[$i]['prod_othermeta_value']); unset($records[$i]['prod_othermeta_key']); $temp = array_combine($prod_meta_key, $prod_meta_values); $product_type = wp_get_object_terms($records[$i]['id'], 'product_type', array('fields' => 'slugs')); //for formatting meta keys of custom keys $prod_meta_key_values = array(); if (empty($_POST['func_nm'])) { foreach ($temp as $key => $val) { $key = preg_replace('/[^A-Za-z0-9\\-_]/', '', $key); $prod_meta_key_values[$key] = $val; // Removes special chars. } } else { $prod_meta_key_values = $temp; } // Code to get the Category Name from the term_taxonomy_id if (isset($category_ids_all[$records[$i]['id']])) { //$category_id = explode('###', $records[$i]['term_taxonomy_id']); $category_names = ""; // unset($records[$i]['term_taxonomy_id']); $category_id = explode(' #sm# ', $category_ids_all[$records[$i]['id']]); for ($j = 0; $j < sizeof($category_id); $j++) { if (isset($term_taxonomy[$category_id[$j]])) { $category_names .= $term_taxonomy[$category_id[$j]] . ', '; } } if ($category_names != "") { $category_names = substr($category_names, 0, -2); $records[$i]['category'] = $category_names; } } else { $records[$i]['category'] = ""; } $product_type = !empty($product_type[0]) ? $product_type[0] : ''; $records[$i]['category'] = $records[$i]['post_parent'] > 0 && $product_type == 'simple' || $records[$i]['post_parent'] == 0 ? !empty($records[$i]['category']) ? $records[$i]['category'] : '' : ''; // To hide category name from Product's variations //Attributes Column if (isset($prod_meta_key_values['_product_attributes']) && $prod_meta_key_values['_product_attributes'] != "") { $prod_attr = unserialize($prod_meta_key_values['_product_attributes']); $attributes_list = ""; //cond added for handling blank data if (is_array($prod_attr) && !empty($prod_attr)) { foreach ($prod_attr as $prod_attr1) { $attribute_terms = ""; if (isset($attributes_label[$prod_attr1['name']]) && isset($product_attributes[$prod_attr1['name']])) { if (!empty($category_id)) { foreach ($category_id as $category_id1) { if (isset($product_attributes[$prod_attr1['name']][$category_id1])) { $attribute_terms .= $product_attributes[$prod_attr1['name']][$category_id1] . ', '; } } } if ($attribute_terms != "") { $attribute_terms = substr($attribute_terms, 0, -2); $attributes_list .= $attributes_label[$prod_attr1['name']] . ": [" . $attribute_terms . "]"; $attributes_list .= "<br>"; } } elseif ($prod_attr1['is_taxonomy'] == 0) { $attributes_list .= $prod_attr1['name'] . ": [" . str_replace(" |", ",", $prod_attr1['value']) . "]"; $attributes_list .= "<br>"; } } } // $records[$i]['product_attributes'] = substr( $attributes_list, 0, -3); $records[$i]['product_attributes'] = $attributes_list; } else { $records[$i]['product_attributes'] = ""; } if (isset($prod_meta_key_values['_sale_price_dates_from']) && !empty($prod_meta_key_values['_sale_price_dates_from'])) { $prod_meta_key_values['_sale_price_dates_from'] = date('Y-m-d', (int) $prod_meta_key_values['_sale_price_dates_from']); } if (isset($prod_meta_key_values['_sale_price_dates_to']) && !empty($prod_meta_key_values['_sale_price_dates_to'])) { $prod_meta_key_values['_sale_price_dates_to'] = date('Y-m-d', (int) $prod_meta_key_values['_sale_price_dates_to']); } $records[$i] = array_merge((array) $records[$i], $prod_meta_key_values); $thumbnail = isset($records[$i]['_thumbnail_id']) ? wp_get_attachment_image_src($records[$i]['_thumbnail_id'], $image_size) : ''; $records[$i]['thumbnail'] = !empty($thumbnail[0]) ? $thumbnail[0] : false; $records[$i]['_tax_status'] = !empty($prod_meta_key_values['_tax_status']) ? $prod_meta_key_values['_tax_status'] : ''; // Setting product type for grouped products if ($records[$i]['post_parent'] != 0) { $product_type_parent = wp_get_object_terms($records[$i]['post_parent'], 'product_type', array('fields' => 'slugs')); if ($product_type_parent[0] == "grouped") { $records[$i]['product_type'] = $product_type_parent[0]; } } else { // $records[$i]['product_type'] = $product_type[0]; $records[$i]['product_type'] = $product_type; } $records[$i]['total_sales'] = !empty($records[$i]['total_sales']) ? $records[$i]['total_sales'] : '0'; //added in woo23 if ($show_variation === true) { if ($records[$i]['post_parent'] != 0 && $product_type_parent[0] != "grouped") { $records[$i]['post_status'] = get_post_status($records[$i]['post_parent']); // Code for assigning the parent sku if sku is blank if (empty($records[$i]['_sku'])) { $records[$i]['_sku'] = get_post_meta($records[$i]['post_parent'], '_sku'); } if ($_POST['SM_IS_WOO16'] == "true") { $records[$i]['_regular_price'] = $records[$i]['_price']; } $variation_names = ''; foreach ($variation as $slug) { $prod_meta_key_values_slug = !empty($prod_meta_key_values[$slug]) ? $prod_meta_key_values[$slug] : ''; $variation_names .= isset($attributes[$prod_meta_key_values_slug]) && !empty($attributes[$prod_meta_key_values_slug]) ? $attributes[$prod_meta_key_values_slug] . ', ' : ucfirst($prod_meta_key_values_slug) . ', '; } $records[$i]['post_title'] = get_the_title($records[$i]['post_parent']) . " - " . trim($variation_names, ", "); $records[$i]['product_attributes'] = ''; //for clearing the attributes field for variations if exists $records[$i]['total_sales'] = ''; //added in woo23 // } else if ($records[$i]['post_parent'] == 0 && $product_type[0] == 'variable') { } else { if ($records[$i]['post_parent'] == 0 && $product_type == 'variable') { $records[$i]['_regular_price'] = ""; $records[$i]['_sale_price'] = ""; } else { $records[$i]['_regular_price'] = trim($records[$i]['_regular_price']); if (empty($records[$i]['_regular_price'])) { $records[$i]['_regular_price'] = $records[$i]['_price']; } } } $products[$records[$i]['id']]['post_title'] = $records[$i]['post_title']; $products[$records[$i]['id']]['variation'] = !empty($variation_names) ? $variation_names : ''; } elseif ($show_variation === false && SMPRO) { // if ($product_type[0] == 'variable') { if ($product_type == 'variable') { // WOO 2.1 compatibility if ($_POST['SM_IS_WOO21'] == "true" || $_POST['SM_IS_WOO22'] == "true") { $records[$i]['_regular_price'] = isset($variation_reg_price[$records[$i]['id']]) ? get_post_meta($variation_reg_price[$records[$i]['id']], '_regular_price', true) : 0; $records[$i]['_sale_price'] = !empty($records[$i]['_min_variation_price']) && !empty($variation_reg_price[$records[$i]['id']]) && get_post_meta($variation_reg_price[$records[$i]['id']], '_sale_price', true) ? $records[$i]['_min_variation_price'] : ''; } else { $records[$i]['_regular_price'] = $records[$i]['_min_variation_regular_price']; $records[$i]['_sale_price'] = $records[$i]['_min_variation_sale_price']; } } else { $records[$i]['_regular_price'] = trim($records[$i]['_regular_price']); // if ( empty( $records[$i]['_regular_price'] ) ) { // $records[$i]['_regular_price'] = $records[$i]['_price']; // } $records[$i]['_sale_price'] = trim($records[$i]['_sale_price']); } } else { $records[$i]['_regular_price'] = $records[$i]['_regular_price']; $records[$i]['_sale_price'] = $records[$i]['_sale_price']; } unset($records[$i]['prod_othermeta_value']); unset($records[$i]['prod_othermeta_key']); } } } elseif ($active_module == 'Customers') { //BOF Customer's module $search_condn = customers_query($_POST['searchText']); $terms_post_cond = ''; $terms_post_cond_join = ''; // WOO 2.2 compatibility if ($_POST['SM_IS_WOO22'] == "true") { $terms_post_cond_join = 'JOIN ' . $wpdb->prefix . 'posts AS posts ON (posts.ID = postmeta.post_id)'; $terms_post_cond = "AND posts.post_status IN ('wc-completed','wc-processing','wc-on-hold','wc-pending')"; } else { $query_terms = "SELECT id FROM {$wpdb->prefix}posts AS posts\n JOIN {$wpdb->prefix}term_relationships AS term_relationships \n ON term_relationships.object_id = posts.ID \n JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n JOIN {$wpdb->prefix}terms AS terms \n ON term_taxonomy.term_id = terms.term_id\n WHERE terms.name IN ('completed','processing','on-hold','pending')\n AND posts.post_status IN ('publish')"; $terms_post = implode(",", $wpdb->get_col($query_terms)); $terms_post_cond = "AND postmeta.post_id IN ({$terms_post})"; } //Query for getting the max of post id for all the Guest Customers $query_post_guest = "SELECT postmeta.post_ID FROM {$wpdb->prefix}postmeta AS postmeta\n {$terms_post_cond_join}\n WHERE postmeta.meta_key ='_customer_user' AND postmeta.meta_value=0\n {$terms_post_cond}"; $post_id_guest = $wpdb->get_col($query_post_guest); $num_guest = $wpdb->num_rows; $result_max_id = ''; if ($num_guest > 0) { $query_max_id = "SELECT GROUP_CONCAT(distinct postmeta1.post_ID \n ORDER BY posts.post_date DESC SEPARATOR ',' ) AS all_id,\n GROUP_CONCAT(postmeta2.meta_value \n ORDER BY posts.post_date DESC SEPARATOR ',' ) AS order_total, \n date_format(max(posts.post_date),'%b %e %Y, %r') AS date,\n count(postmeta1.post_id) as count,\n sum(postmeta2.meta_value) as total\n \n FROM {$wpdb->prefix}postmeta AS postmeta1\n JOIN {$wpdb->prefix}posts AS posts ON (posts.ID = postmeta1.post_id)\n INNER JOIN {$wpdb->prefix}postmeta AS postmeta2\n ON (postmeta2.post_ID = postmeta1.post_ID AND postmeta2.meta_key IN ('_order_total'))\n\n WHERE postmeta1.meta_key IN ('_billing_email')\n AND postmeta1.post_ID IN (" . implode(",", $post_id_guest) . ") \n GROUP BY postmeta1.meta_value\n ORDER BY date desc"; $result_max_id = $wpdb->get_results($query_max_id, 'ARRAY_A'); } //Query for getting the max of post id for all the Registered Customers $query_post_user = "******"; $post_id_user = $wpdb->get_col($query_post_user); $num_user = $wpdb->num_rows; if ($num_user > 0) { $query_max_user = "******" . implode(",", $post_id_user) . ") \n GROUP BY postmeta1.meta_value\n ORDER BY date"; $result_max_user = $wpdb->get_results($query_max_user, 'ARRAY_A'); } //Code for generating the total orders, count of orders , max ids and last order total arrays for ($i = 0; $i < sizeof($result_max_id); $i++) { $temp = !empty($result_max_id[$i]['all_id']) ? explode(",", $result_max_id[$i]['all_id']) : array(); $max_ids[$i] = !empty($temp) ? $temp[0] : 0; $order_count[$max_ids[$i]] = !empty($result_max_id[$i]['count']) ? $result_max_id[$i]['count'] : ''; $order_total[$max_ids[$i]] = !empty($result_max_id[$i]['total']) ? $result_max_id[$i]['total'] : ''; //Code for getting the last Order Total $temp = !empty($result_max_id[$i]['order_total']) ? explode(",", $result_max_id[$i]['order_total']) : ''; $last_order_total[$max_ids[$i]] = !empty($temp) ? $temp[0] : ''; } $j = $k = $l = $m = 0; //initiliazing variables if (!empty($result_max_id)) { $j = sizeof($max_ids); $k = sizeof($order_count); $l = sizeof($order_total); $m = sizeof($last_order_total); } for ($i = 0; $i < sizeof($result_max_user); $i++, $j++, $k++, $l++, $m++) { $temp = !empty($result_max_user[$i]['all_id']) ? explode(",", $result_max_user[$i]['all_id']) : ''; $max_ids[$j] = !empty($temp[0]) ? $temp[0] : 0; $order_count[$max_ids[$j]] = !empty($result_max_user[$i]['count']) ? $result_max_user[$i]['count'] : ''; $order_total[$max_ids[$j]] = !empty($result_max_user[$i]['total']) ? $result_max_user[$i]['total'] : ''; $temp = !empty($result_max_user[$i]['order_total']) ? explode(",", $result_max_user[$i]['order_total']) : ''; $last_order_total[$max_ids[$j]] = !empty($temp[0]) ? $temp[0] : ''; } $max_id = !empty($max_ids) ? implode(",", $max_ids) : ''; $max_id_join = ''; $orderby_cond = ''; if (!empty($max_ids)) { $wpdb->query("DELETE FROM {$wpdb->base_prefix}sm_advanced_search_temp"); $max_ids_inserted = !empty($max_ids) ? '(' . implode("),(", $max_ids) . ')' : ''; $wpdb->query("REPLACE INTO {$wpdb->base_prefix}sm_advanced_search_temp (product_id) VALUES " . $max_ids_inserted); $max_id_join = " JOIN {$wpdb->base_prefix}sm_advanced_search_temp as temp ON (temp.product_id = posts.id)"; //$orderby_cond = "ORDER BY temp.product_id"; $orderby_cond = "ORDER BY posts.ID"; } // $postid_cond = (!empty($max_ids)) ? "AND posts.ID IN ($max_id)" : ''; // $orderby_cond = (!empty($max_ids)) ? "ORDER BY FIND_IN_SET(posts.ID,'$max_id')" : "ORDER BY posts.ID"; $customers_query = "SELECT SQL_CALC_FOUND_ROWS\n DISTINCT(GROUP_CONCAT( postmeta.meta_value\n ORDER BY postmeta.meta_id SEPARATOR '###' ) )AS meta_value,\n GROUP_CONCAT(distinct postmeta.meta_key\n ORDER BY postmeta.meta_id SEPARATOR '###' ) AS meta_key,\n date_format(max(posts.post_date),'%b %e %Y, %r') AS date,\n posts.ID AS id\n\n FROM {$wpdb->prefix}posts AS posts\n RIGHT JOIN {$wpdb->prefix}postmeta AS postmeta\n ON (posts.ID = postmeta.post_id AND postmeta.meta_key IN\n ('_billing_first_name' , '_billing_last_name' , '_billing_email',\n '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_state',\n '_billing_country','_billing_postcode', '_billing_phone','_customer_user'))"; // WOO 2.2 compatibility $post_status_cond = "AND posts.post_status IN ('publish')"; if ($_POST['SM_IS_WOO22'] == "true") { $post_status_cond = "AND posts.post_status NOT IN ('trash')"; } $where = " WHERE posts.post_type LIKE 'shop_order' \n {$post_status_cond}\n {$postid_cond}"; $group_by = " GROUP BY posts.ID"; $limit_query = " {$orderby_cond} {$limit_string}"; $query = "{$customers_query} {$max_id_join} {$where} {$group_by} {$search_condn} {$limit_query};"; $result = $wpdb->get_results($query, 'ARRAY_A'); $num_rows = $wpdb->num_rows; //To get Total count $customers_count_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $customers_count_result[0]['count']; if ($num_records == 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', $sm_text_domain); } else { $postmeta = array(); $user = array(); $user_order_ids = array(); $j = 0; $k = 0; for ($i = 0; $i < sizeof($result); $i++) { $meta_value = explode('###', $result[$i]['meta_value']); $meta_key = explode('###', $result[$i]['meta_key']); //note: while merging the array, $data as to be the second arg if (count($meta_key) == count($meta_value)) { $temp[$i] = array_combine($meta_key, $meta_value); } if ($temp[$i]['_customer_user'] == 0) { $postmeta[$j] = $temp[$i]; $postmeta[$j]['order_id'] = $result[$i]['id']; $j++; } elseif ($temp[$i]['_customer_user'] > 0) { $user[$k] = $temp[$i]['_customer_user']; $user_order_ids[$temp[$i]['_customer_user']] = $result[$i]['id']; $k++; } unset($meta_value); unset($meta_key); } //Query for getting the Registered Users data from wp_usermeta and wp_users table if (!empty($user)) { $user_ids = implode(",", $user); $query_users = "SELECT users.ID,users.user_email,\n GROUP_CONCAT( usermeta.meta_value ORDER BY usermeta.umeta_id SEPARATOR '###' ) AS meta_value,\n GROUP_CONCAT(distinct usermeta.meta_key\n ORDER BY usermeta.umeta_id SEPARATOR '###_' ) AS meta_key\n FROM {$wpdb->users} AS users\n JOIN {$wpdb->usermeta} AS usermeta\n ON (users.ID = usermeta.user_id AND usermeta.meta_key IN\n ('billing_first_name' , 'billing_last_name' , 'billing_email',\n 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state',\n 'billing_country','billing_postcode', 'billing_phone'))\n WHERE users.ID IN ({$user_ids})\n GROUP BY users.ID\n ORDER BY FIND_IN_SET(users.ID,'{$user_ids}');"; $result_users = $wpdb->get_results($query_users, 'ARRAY_A'); $num_rows_users = $wpdb->num_rows; for ($i = 0, $k = sizeof($postmeta); $i < sizeof($result_users); $i++, $k++) { $meta_value = explode('###', $result_users[$i]['meta_value']); $result_users[$i]['meta_key'] = "_" . $result_users[$i]['meta_key']; $meta_key = explode('###', $result_users[$i]['meta_key']); //note: while merging the array, $data as to be the second arg if (count($meta_key) == count($meta_value)) { $postmeta[$k] = array_combine($meta_key, $meta_value); $postmeta[$k]['_customer_user'] = $result_users[$i]['ID']; $postmeta[$k]['order_id'] = $user_order_ids[$result_users[$i]['ID']]; $postmeta[$k]['_billing_email'] = $result_users[$i]['user_email']; } unset($meta_value); unset($meta_key); } } $user_id = array(); for ($i = 0; $i < sizeof($postmeta); $i++) { if ($postmeta[$i]['_customer_user'] == 0) { $user_email[$i] = "'" . $postmeta[$i]['_billing_email'] . "'"; } elseif ($postmeta[$i]['_customer_user'] > 0) { $user_id[$i] = $postmeta[$i]['_customer_user']; } } for ($i = 0; $i < sizeof($postmeta); $i++) { $postmeta[$i]['id'] = $max_ids[$i]; if (SMPRO == true) { $result[$i]['_order_total'] = $last_order_total[$postmeta[$i]['order_id']]; $postmeta[$i]['count_orders'] = $order_count[$postmeta[$i]['order_id']]; $postmeta[$i]['total_orders'] = $order_total[$postmeta[$i]['order_id']]; $result[$i]['last_order'] = $result[$i]['date']; } else { $postmeta[$i]['count_orders'] = 'Pro only'; $postmeta[$i]['total_orders'] = 'Pro only'; $result[$i]['_order_total'] = 'Pro only'; $result[$i]['last_order'] = 'Pro only'; } $billing_address_2 = !empty($postmeta[$i]['_billing_address_2']) ? $postmeta[$i]['_billing_address_2'] : ''; $result[$i]['_billing_address'] = isset($postmeta[$i]['_billing_address_1']) ? $postmeta[$i]['_billing_address_1'] . ', ' . $billing_address_2 : $billing_address_2; $postmeta[$i]['_billing_state'] = isset($woocommerce->countries->states[$postmeta[$i]['_billing_country']][$postmeta[$i]['_billing_state']]) ? $woocommerce->countries->states[$postmeta[$i]['_billing_country']][$postmeta[$i]['_billing_state']] : $postmeta[$i]['_billing_state']; $postmeta[$i]['_billing_country'] = isset($woocommerce->countries->countries[$postmeta[$i]['_billing_country']]) ? $woocommerce->countries->countries[$postmeta[$i]['_billing_country']] : $postmeta[$i]['_billing_country']; unset($result[$i]['date']); unset($result[$i]['meta_key']); unset($result[$i]['meta_value']); unset($postmeta[$i]['_billing_address_1']); unset($postmeta[$i]['_billing_address_2']); //NOTE: storing old email id in an extra column in record so useful to indentify record with emailid during updates. if ($postmeta[$i]['_billing_email'] != '' || $postmeta[$i]['_billing_email'] != null) { $records[] = array_merge($postmeta[$i], $result[$i]); } } } unset($result); unset($postmeta); } elseif ($active_module == 'Orders') { if (SMPRO == true && function_exists('sm_woo_get_packing_slip') && (!empty($_POST['label']) && $_POST['label'] == 'getPurchaseLogs')) { $log_ids_arr = json_decode(stripslashes($_POST['log_ids'])); if (is_array($log_ids_arr)) { $log_ids = implode(', ', $log_ids_arr); } sm_woo_get_packing_slip($log_ids, $log_ids_arr); } if ($_POST['SM_IS_WOO22'] == "true") { $orders_select_col = ",posts.post_status as order_status"; $orders_join_cond = ""; $orders_where_cond = " AND posts.post_status NOT IN('trash')"; } else { //Code to get all the term_names along with the term_taxonomy_id in an array $query_terms = "SELECT terms.name,term_taxonomy.term_taxonomy_id \n FROM {$wpdb->prefix}term_taxonomy AS term_taxonomy\n JOIN {$wpdb->prefix}terms AS terms ON terms.term_id = term_taxonomy.term_id\n WHERE taxonomy LIKE 'shop_order_status'"; $terms = $wpdb->get_results($query_terms, 'ARRAY_A'); for ($i = 0; $i < sizeof($terms); $i++) { $terms_name[$terms[$i]['term_taxonomy_id']] = $terms[$i]['name']; $terms_id[$i] = $terms[$i]['term_taxonomy_id']; } $terms_post = implode(",", $terms_id); $orders_select_col = ",term_relationships.term_taxonomy_id AS term_taxonomy_id"; $orders_join_cond = "JOIN {$wpdb->prefix}term_relationships AS term_relationships \n ON (term_relationships.object_id = posts.ID )"; $orders_where_cond = "AND posts.post_status IN ('publish','draft','auto-draft')\n AND term_relationships.term_taxonomy_id IN ({$terms_post})"; } //Code for Sequential Orders compatibility if (is_plugin_active('woocommerce-sequential-order-numbers-pro/woocommerce-sequential-order-numbers.php')) { $order_formatted = ", '_order_number_formatted'"; } else { $order_formatted = ""; } $select_query = "SELECT SQL_CALC_FOUND_ROWS posts.ID as id,\n posts.post_excerpt as order_note,\n date_format(posts.post_date,'%b %e %Y, %r') AS date,\n GROUP_CONCAT( postmeta.meta_value \n ORDER BY postmeta.meta_id\n SEPARATOR '###' ) AS meta_value,\n GROUP_CONCAT(distinct postmeta.meta_key\n ORDER BY postmeta.meta_id \n SEPARATOR '###' ) AS meta_key\n {$orders_select_col}\n \n FROM {$wpdb->prefix}posts AS posts \n {$orders_join_cond}\n RIGHT JOIN {$wpdb->prefix}postmeta AS postmeta \n ON (posts.ID = postmeta.post_id AND postmeta.meta_key IN \n ('_billing_first_name' , '_billing_last_name' , '_billing_email',\n '_shipping_first_name', '_shipping_last_name', '_shipping_address_1', '_shipping_address_2',\n '_shipping_city', '_shipping_state', '_shipping_country','_shipping_postcode',\n '_shipping_method', '_payment_method', '_order_items', '_order_total',\n '_shipping_method_title', '_payment_method_title','_customer_user','_billing_phone',\n '_order_shipping', '_order_discount', '_cart_discount', '_order_tax', '_order_shipping_tax', '_order_currency', 'coupons'" . $order_formatted . "))"; $group_by = " GROUP BY posts.ID"; $limit_query = " ORDER BY posts.ID DESC {$limit_string} ;"; $where = " WHERE posts.post_type LIKE 'shop_order' \n {$orders_where_cond}"; if (isset($_POST['fromDate'])) { $from_date = date('Y-m-d H:i:s', (int) strtotime($_POST['fromDate'])); $date_start = date('Y-m-d', (int) strtotime($_POST['fromDate'])); $date = date('Y-m-d', (int) strtotime($_POST['toDate'])); if ($date_start == $date && $date == date('Y-m-d')) { $curr_time_gmt = date('H:i:s', time() - date("Z")); $new_date = $date . " " . $curr_time_gmt; $to_date = date('Y-m-d H:i:s', (int) strtotime($new_date) + get_option('gmt_offset') * HOUR_IN_SECONDS); } else { $to_date = $date . " 23:59:59"; } if (SMPRO == true) { $where .= " AND posts.post_date BETWEEN '{$from_date}' AND '{$to_date}'"; } } $search_condn = ''; if (isset($_POST['searchText']) && $_POST['searchText'] != '') { $multiple_search_terms = explode('\\"', trim($_POST['searchText'])); $search_on = $wpdb->_real_escape(trim($_POST['searchText'])); //Query for getting the user_id based on the email enetered in the Search Box $query_user_email = "SELECT id FROM {$wpdb->prefix}users \n WHERE user_email like '%{$search_on}%'"; $result_user_email = $wpdb->get_col($query_user_email); $num_rows_email = $wpdb->num_rows; if ($num_rows_email == 0) { $query_user_email = "SELECT DISTINCT p2.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value > 0\n AND p1.meta_value like '%{$search_on}%'"; $result_user_email = $wpdb->get_col($query_user_email); $num_rows_email1 = $wpdb->num_rows; } //Query for getting the user_id based on the Customer phone number enetered in the Search Box $query_user_phone = "SELECT user_id FROM {$wpdb->prefix}usermeta \n WHERE meta_key='billing_phone' \n AND meta_value like '%{$search_on}%'"; $result_user_phone = $wpdb->get_col($query_user_phone); $num_rows_phone = $wpdb->num_rows; if ($num_rows_phone == 0) { $query_user_phone = "SELECT DISTINCT p2.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_phone'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value > 0\n AND p1.meta_value like '%{$search_on}%'"; $result_user_phone = $wpdb->get_col($query_user_phone); $num_rows_phone1 = $wpdb->num_rows; } $query_terms = "SELECT term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy\n WHERE term_id IN (SELECT term_id FROM {$wpdb->prefix}terms"; // name like '%$search_on%') // $multiple_search_terms = explode( '\"', $search_on ); if (!empty($multiple_search_terms)) { $query_terms .= " WHERE"; foreach ($multiple_search_terms as $search_status) { $search_status = trim($search_status); if (!empty($search_status)) { $query_terms .= " name like '%{$search_status}%' OR"; } } $query_terms = trim($query_terms, ' OR'); } $query_terms .= ")"; $result_terms = implode(",", $wpdb->get_col($query_terms)); $num_terms = $wpdb->num_rows; // Start: Query for searching product names in order if ($_POST['SM_IS_WOO16'] == "false") { $query_product_names = "SELECT order_id\n FROM {$wpdb->prefix}woocommerce_order_items"; if (!empty($multiple_search_terms)) { $query_product_names .= " WHERE"; foreach ($multiple_search_terms as $product_name) { $product_name = trim($product_name); if (!empty($product_name)) { $query_product_names .= " order_item_name LIKE '%{$product_name}%' OR"; } } $query_product_names = trim($query_product_names, ' OR'); } } else { $query_product_names = "SELECT post_id\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key LIKE '%_order_items%'"; if (!empty($multiple_search_terms)) { $query_product_names .= " AND ("; foreach ($multiple_search_terms as $product_name) { $product_name = trim($product_name); if (!empty($product_name)) { $query_product_names .= " meta_value LIKE '%{$product_name}%' OR"; } } $query_product_names = trim($query_product_names, ' OR'); $query_product_names .= ")"; } } $result_product_ids = $wpdb->get_col($query_product_names); $num_product_ids = $wpdb->num_rows; // End: Query for searching product names in order //Query to get the post_id of the products whose sku code matches with the one type in the search text box of the Orders Module $query_sku = "SELECT post_id FROM {$wpdb->prefix}postmeta\n WHERE meta_key = '_sku'\n AND meta_value like '%{$search_on}%'"; $result_sku = $wpdb->get_col($query_sku); $rows_sku = $wpdb->num_rows; //Code for handling the Search functionality of the Orders Module using the SKU code of the product if ($rows_sku > 0) { if ($_POST['SM_IS_WOO16'] == "false") { $query_order_by_sku = "SELECT order_id\n FROM {$wpdb->prefix}woocommerce_order_items AS woocommerce_order_items\n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woocommerce_order_itemmeta USING ( order_item_id )\n WHERE woocommerce_order_itemmeta.meta_key IN ( '_product_id', '_variation_id' )\n AND woocommerce_order_itemmeta.meta_value IN ( " . implode(',', $result_sku) . " )"; $results_order_by_sku = $wpdb->get_col($query_order_by_sku); $num_order_by_sku = $wpdb->num_rows; if ($num_order_by_sku > 0) { $search_condn = " HAVING id IN ( " . implode(',', $results_order_by_sku) . " )"; } } else { //Query for getting all the distinct attribute meta key names $query_variation = "SELECT DISTINCT meta_key as variation\n FROM {$wpdb->prefix}postmeta\n WHERE meta_key like 'attribute_%'"; $variation = $wpdb->get_col($query_variation); //Query to get all the product title's as displayed in the products module along wih the post_id and SKU code in an array $query_product = "SELECT posts.id, posts.post_title, posts.post_parent, \n GROUP_CONCAT( postmeta.meta_value \n ORDER BY postmeta.meta_id\n SEPARATOR ',' ) AS meta_value\n FROM {$wpdb->prefix}posts AS posts\n JOIN {$wpdb->prefix}postmeta AS postmeta\n ON (posts.ID = postmeta.post_id\n AND postmeta.meta_key IN ('_sku','" . implode("','", $variation) . "'))\n GROUP BY posts.id"; $result_product = $wpdb->get_results($query_product, 'ARRAY_A'); //Code to store all the products title in an array with the post_id as the array index for ($i = 0; $i < sizeof($result_product); $i++) { $product_title[$result_product[$i]['id']]['post_title'] = $result_product[$i]['post_title']; $product_title[$result_product[$i]['id']]['variation_title'] = $result_product[$i]['meta_value']; $product_title[$result_product[$i]['id']]['post_parent'] = $result_product[$i]['post_parent']; } $post_title = array(); $variation_title = array(); $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_sku); $i++) { $product_type = wp_get_object_terms($result_sku[$i], 'product_type', array('fields' => 'slugs')); // Getting the type of the product //Code to prepare the search condition for the search using SKU Code if ($product_title[$result_sku[$i]]['post_parent'] == 0) { $post_title[$i] = $product_title[$result_sku[$i]]['post_title']; $search_condn .= " meta_value like '%s:4:\"name\"%\"{$post_title[$i]}\"%' "; $search_condn .= "OR"; } elseif ($product_title[$result_sku[$i]]['post_parent'] > 0) { $temp = explode(",", $product_title[$result_sku[$i]]['variation_title']); $post_title[$i] = $product_title[$product_title[$result_sku[$i]]['post_parent']]['post_title']; $search_condn .= " meta_value like '%s:4:\"name\"%\"{$post_title[$i]}\"%' "; $search_condn .= "AND ("; for ($j = 1; $j < sizeof($temp); $j++) { $search_condn .= " meta_value like '%s:10:\"meta_value\"%\"{$temp[$j]}\"%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2) . ")"; $search_condn .= "OR"; } } $variation_title = array_unique($variation_title); $search_condn = substr($search_condn, 0, -2); } } elseif ($num_product_ids > 0) { $search_condn = " HAVING id IN ( " . implode(',', $result_product_ids) . " )"; } elseif ($num_rows_email > 0) { // Query to bring the matching email of the Guest uers $query = "SELECT DISTINCT p1.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value = 0\n AND p1.meta_value like '%{$search_on}%'"; $result_email_guest = $wpdb->get_col($query); $rows_email_guest = $wpdb->num_rows; $query_email = "SELECT DISTINCT(p1.meta_value)\n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value IN (" . implode(",", $result_user_email) . ")"; $result_email = $wpdb->get_col($query_email); if ($rows_email_guest > 0) { for ($i = 0, $j = sizeof($result_email); $i < sizeof($result_email_guest); $i++, $j++) { $result_email[$j] = $result_email_guest[$i]; } } $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_email); $i++) { $search_condn .= " meta_value like '%{$result_email[$i]}%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2); } elseif ($num_rows_phone > 0) { // Query to bring the matching Phone No. of the Guest uers $query = "SELECT DISTINCT p1.meta_value \n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_phone'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value = 0\n AND p1.meta_value like '%{$search_on}%'"; $result_phone_guest = $wpdb->get_col($query); $rows_phone_guest = $wpdb->num_rows; $query_phone = "SELECT DISTINCT(p1.meta_value)\n FROM {$wpdb->prefix}postmeta AS p1, {$wpdb->prefix}postmeta AS p2 \n WHERE p1.post_id = p2.post_id \n AND p1.meta_key = '_billing_email'\n AND p2.meta_key = '_customer_user'\n AND p2.meta_value IN (" . implode(",", $result_user_phone) . ")"; $result_phone = $wpdb->get_col($query_phone); if ($rows_phone_guest > 0) { for ($i = 0, $j = sizeof($result_phone); $i < sizeof($result_phone_guest); $i++, $j++) { $result_phone[$j] = $result_phone_guest[$i]; } } $search_condn = "HAVING"; for ($i = 0; $i < sizeof($result_phone); $i++) { $search_condn .= " meta_value like '%{$result_phone[$i]}%' "; $search_condn .= "OR"; } $search_condn = substr($search_condn, 0, -2); } elseif ($num_rows_email1 > 0 || $num_rows_phone1 > 0) { $search_condn = " HAVING id = 0"; } elseif ($num_terms > 0) { $search_condn = " HAVING term_taxonomy_id IN ({$result_terms})"; } else { $search_condn = " HAVING id like '{$search_on}%'\n OR date like '%{$search_on}%'\n OR meta_value like '%{$search_on}%'"; } if ($_POST['SM_IS_WOO22'] == "true") { $search_on = 'wc-' . str_replace(" ", "-", $search_on); if (!empty($search_condn)) { $search_condn .= " OR order_status LIKE '%{$search_on}%'"; } else { $search_condn = " HAVING order_status LIKE '%{$search_on}%'"; } } } //get the state id if the shipping state is numeric or blank $query = "{$select_query} {$where} {$group_by} {$search_condn} {$limit_query}"; $results = $wpdb->get_results($query, 'ARRAY_A'); //To get the total count $orders_count_result = $wpdb->get_results('SELECT FOUND_ROWS() as count;', 'ARRAY_A'); $num_records = $orders_count_result[0]['count']; if ($num_records == 0) { $encoded['totalCount'] = ''; $encoded['items'] = ''; $encoded['msg'] = __('No Records Found', $sm_text_domain); } else { foreach ($results as $data) { $order_ids[] = $data['id']; } if ($_POST['SM_IS_WOO16'] == "false") { $order_id = implode(",", $order_ids); // Code for handling export functionality if (!empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo') { $order_id_cond = ""; $order_id_order_by = ""; } else { $order_id_cond = " AND order_items.order_id IN ({$order_id})"; $order_id_order_by = "ORDER BY FIND_IN_SET(order_items.order_id,'{$order_id}')"; } $query_order_items = "SELECT order_items.order_item_id,\n order_items.order_id ,\n order_items.order_item_name AS order_prod,\n order_items.order_item_type,\n GROUP_CONCAT(order_itemmeta.meta_key\n ORDER BY order_itemmeta.meta_id \n SEPARATOR '###' ) AS meta_key,\n GROUP_CONCAT(order_itemmeta.meta_value\n ORDER BY order_itemmeta.meta_id \n SEPARATOR '###' ) AS meta_value\n FROM {$wpdb->prefix}woocommerce_order_items AS order_items \n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta \n ON (order_items.order_item_id = order_itemmeta.order_item_id)\n -- WHERE order_items.order_item_type LIKE 'line_item'\n WHERE order_items.order_item_type IN ('line_item', 'shipping') \n {$order_id_cond}\n GROUP BY order_items.order_item_id\n {$order_id_order_by}"; $results_order_items = $wpdb->get_results($query_order_items, 'ARRAY_A'); $num_rows_order_items = $wpdb->num_rows; //code for formatting order items array if ($num_rows_order_items > 0) { $order_items = array(); $order_shipping_method = array(); foreach ($results_order_items as $results_order_item) { if (!isset($order_items[$results_order_item['order_id']])) { $order_items[$results_order_item['order_id']] = array(); } if ($results_order_item['order_item_type'] == 'shipping') { $order_shipping_method[$results_order_item['order_id']] = $results_order_item['order_prod']; } else { $order_items[$results_order_item['order_id']][] = $results_order_item; } } } //Code for export functionality if (!empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo') { $coupons_order_id_cond = ""; $coupons_order_id_order_by = ""; } else { $coupons_order_id_cond = " AND order_id IN ({$order_id})"; $coupons_order_id_order_by = "ORDER BY FIND_IN_SET(order_id,'{$order_id}')"; } $query_order_coupons = "SELECT order_id,\n GROUP_CONCAT(order_item_name\n ORDER BY order_item_id \n SEPARATOR ', ' ) AS coupon_used\n FROM {$wpdb->prefix}woocommerce_order_items\n WHERE order_item_type LIKE 'coupon'\n {$coupons_order_id_cond}\n GROUP BY order_id\n {$coupons_order_id_order_by}"; $results_order_coupons = $wpdb->get_results($query_order_coupons, 'ARRAY_A'); $num_rows_coupons = $wpdb->num_rows; if ($num_rows_coupons > 0) { $order_coupons = array(); foreach ($results_order_coupons as $results_order_coupon) { $order_coupons[$results_order_coupon['order_id']] = $results_order_coupon['coupon_used']; } } $query_variation_ids = "SELECT order_itemmeta.meta_value \n FROM {$wpdb->prefix}woocommerce_order_items AS order_items \n LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta \n ON (order_items.order_item_id = order_itemmeta.order_item_id)\n WHERE order_itemmeta.meta_key LIKE '_variation_id'\n AND order_itemmeta.meta_value > 0\n AND order_items.order_id IN ({$order_id})"; $result_variation_ids = $wpdb->get_col($query_variation_ids); if (count($result_variation_ids) > 0) { $query_variation_att = "SELECT postmeta.post_id AS post_id,\n GROUP_CONCAT(postmeta.meta_value\n ORDER BY postmeta.meta_id \n SEPARATOR ',' ) AS meta_value\n FROM {$wpdb->prefix}postmeta AS postmeta\n WHERE postmeta.meta_key LIKE 'attribute_%'\n AND postmeta.post_id IN (" . implode(",", $result_variation_ids) . ")\n GROUP BY postmeta.post_id"; // $results_variation_att = $wpdb->get_results($query_variation_att, 'ARRAY_A'); } $query_terms = "SELECT terms.slug as slug, terms.name as term_name\n FROM {$wpdb->prefix}terms AS terms\n JOIN {$wpdb->prefix}postmeta AS postmeta \n ON ( postmeta.meta_value = terms.slug \n AND postmeta.meta_key LIKE 'attribute_%' ) \n GROUP BY terms.slug"; $attributes_terms = $wpdb->get_results($query_terms, 'ARRAY_A'); $attributes = array(); foreach ($attributes_terms as $attributes_term) { $attributes[$attributes_term['slug']] = $attributes_term['term_name']; } $variation_att_all = array(); if (!empty($results_variation_att) && is_array($results_variation_att) && count($results_variation_att) > 0) { for ($i = 0; $i < sizeof($results_variation_att); $i++) { $variation_attributes = explode(", ", $results_variation_att[$i]['meta_value']); $attributes_final = array(); foreach ($variation_attributes as $variation_attribute) { $attributes_final[] = isset($attributes[$variation_attribute]) ? $attributes[$variation_attribute] : ucfirst($variation_attribute); } $results_variation_att[$i]['meta_value'] = implode(", ", $attributes_final); $variation_att_all[$results_variation_att[$i]['post_id']] = $results_variation_att[$i]['meta_value']; } } } $customer_user_ids = $reg_users = array(); foreach ($results as $data) { $meta_key = explode('###', $data['meta_key']); $meta_value = explode('###', $data['meta_value']); if (count($meta_key) == count($meta_value)) { continue; } $postmeta = array_combine($meta_key, $meta_value); if ($postmeta['_customer_user'] == 0) { continue; } $customer_user_ids[] = $postmeta['_customer_user']; } if (!empty($customer_user_ids)) { //Query to get the email id from the wp_users table for the Registered Customers $query_users = "SELECT users.ID,users.user_email,usermeta.meta_value\n FROM {$wpdb->prefix}users AS users, {$wpdb->prefix}usermeta AS usermeta\n WHERE usermeta.user_id = users.id\n AND usermeta.meta_key = 'billing_phone'\n AND users.ID IN (" . implode(',', $customer_user_ids) . ")\n GROUP BY users.ID"; $result_users = $wpdb->get_results($query_users, 'ARRAY_A'); $result_users_count = $wpdb->num_rows; if ($result_users_count > 0) { foreach ($result_users as $result_user) { $reg_users[$result_user['ID']] = array('billing_email' => $result_user['user_email'], 'billing_phone' => $result_user['meta_value']); } } } foreach ($results as $data) { $meta_key = explode('###', $data['meta_key']); $meta_value = explode('###', $data['meta_value']); if (count($meta_key) == count($meta_value)) { $postmeta = array_combine($meta_key, $meta_value); //Code to replace the email of the Registered Customers with the one from the wp_users if ($postmeta['_customer_user'] > 0 && !empty($reg_users[$postmeta['_customer_user']])) { $postmeta['_billing_email'] = $reg_users[$postmeta['_customer_user']]['billing_email']; $postmeta['_billing_phone'] = $reg_users[$postmeta['_customer_user']]['billing_phone']; // for ( $index=0;$index<sizeof($result_users);$index++ ) { // if ( $postmeta['_customer_user'] == $result_users[$index]['ID'] ){ // $postmeta['_billing_email'] = $result_users[$index]['user_email']; // $postmeta['_billing_phone'] = $result_users[$index]['meta_value']; // break; // } // } } if ($_POST['SM_IS_WOO16'] == "true") { if (is_serialized($postmeta['_order_items'])) { $order_items = unserialize(trim($postmeta['_order_items'])); foreach ((array) $order_items as $order_item) { if (isset($order_item['item_meta']) && count($order_item['item_meta']) > 0) { $variation_data = array(); foreach ($order_item['item_meta'] as $meta) { $variation_data['attribute_' . $meta['meta_name']] = $meta['meta_value']; } $variation_details = woocommerce_get_formatted_variation($variation_data, true); } $data['details'] += $order_item['qty']; $data['order_total_ex_tax'] += $order_item['line_total']; $product_id = $order_item['variation_id'] > 0 ? $order_item['variation_id'] : $order_item['id']; $sm_sku = get_post_meta($product_id, '_sku', true); if (!empty($sm_sku)) { $sku_detail = '[SKU: ' . $sm_sku . ']'; } else { $sku_detail = ''; } $product_full_name = !empty($variation_details) ? $order_item['name'] . ' (' . $variation_details . ')' : $order_item['name']; $data['products_name'] .= $product_full_name . ' ' . $sku_detail . '[' . __('Qty', $sm_text_domain) . ': ' . $order_item['qty'] . '][' . __('Price', $sm_text_domain) . ': ' . $order_item['line_total'] / $order_item['qty'] . '], '; } isset($data['details']) ? $data['details'] .= ' items' : ($data['details'] = ''); $data['products_name'] = substr($data['products_name'], 0, -2); //To remove extra comma ', ' from returned string } else { $data['details'] = 'Details'; } } else { if (!empty($order_items[$data['id']])) { foreach ($order_items[$data['id']] as $order_item) { $prod_meta_values = explode('###', $order_item['meta_value']); $prod_meta_key = explode('###', $order_item['meta_key']); if (count($prod_meta_values) != count($prod_meta_key)) { continue; } unset($order_item['meta_value']); unset($order_item['meta_key']); $sku_detail = !empty($sku_detail) ? $sku_detail : ''; $index = !empty($index) ? $index : ''; update_post_meta($index, $sku_detail, $meta_value); $prod_meta_key_values = array_combine($prod_meta_key, $prod_meta_values); // if ($data['id'] == $order_item['order_id']) { $data['details'] = !empty($data['details']) ? $data['details'] : ''; $data['order_total_ex_tax'] = !empty($data['order_total_ex_tax']) ? $data['order_total_ex_tax'] : ''; $data['details'] += $prod_meta_key_values['_qty']; $data['order_total_ex_tax'] += $prod_meta_key_values['_line_total']; $product_id = $prod_meta_key_values['_variation_id'] > 0 ? $prod_meta_key_values['_variation_id'] : $prod_meta_key_values['_product_id']; $sm_sku = get_post_meta($product_id, '_sku', true); if (!empty($sm_sku)) { $sku_detail = '[SKU: ' . $sm_sku . ']'; } else { $sku_detail = ''; } $variation_att = isset($variation_att_all[$prod_meta_key_values['_variation_id']]) && !empty($variation_att_all[$prod_meta_key_values['_variation_id']]) ? $variation_att_all[$prod_meta_key_values['_variation_id']] : ''; $product_full_name = !empty($variation_att) ? $order_item['order_prod'] . ' (' . $variation_att . ')' : $order_item['order_prod']; $data['products_name'] = !empty($data['products_name']) ? $data['products_name'] : ''; $data['products_name'] .= $product_full_name . ' ' . $sku_detail . '[' . __('Qty', $sm_text_domain) . ': ' . $prod_meta_key_values['_qty'] . '][' . __('Price', $sm_text_domain) . ': ' . $prod_meta_key_values['_line_total'] / $prod_meta_key_values['_qty'] . '], '; $data['coupons'] = isset($order_coupons[$order_item['order_id']]) ? $order_coupons[$order_item['order_id']] : ""; // } } isset($data['details']) ? $data['details'] .= ' items' : ($data['details'] = ''); $data['products_name'] = substr($data['products_name'], 0, -2); //To remove extra comma ', ' from returned string } } //Code to get the Order_Status using the $terms_name array if ($_POST['SM_IS_WOO22'] == "true") { $data['order_status'] = 'wc-' === substr($data['order_status'], 0, 3) ? substr($data['order_status'], 3) : $data['order_status']; } else { $data['order_status'] = $terms_name[$data['term_taxonomy_id']]; } $name_emailid[0] = "<font class=blue>" . $postmeta['_billing_first_name'] . "</font>"; $name_emailid[1] = "<font class=blue>" . $postmeta['_billing_last_name'] . "</font>"; $name_emailid[2] = "(" . $postmeta['_billing_email'] . ")"; //email comes at 7th position. $data['name'] = implode(' ', $name_emailid); //in front end,splitting is done with this space. $data['_shipping_address'] = $postmeta['_shipping_address_1'] . ', ' . $postmeta['_shipping_address_2']; unset($data['meta_value']); $postmeta['_shipping_method'] = isset($postmeta['_shipping_method_title']) ? $postmeta['_shipping_method_title'] : (!empty($postmeta['_shipping_method']) ? $postmeta['_shipping_method'] : ''); $postmeta['_shipping_method'] = !empty($order_shipping_method[$data['id']]) ? $order_shipping_method[$data['id']] : $postmeta['_shipping_method']; $payment_method = !empty($postmeta['_payment_method']) ? $postmeta['_payment_method'] : ''; $postmeta['_payment_method'] = isset($postmeta['_payment_method_title']) ? $postmeta['_payment_method_title'] : $payment_method; $postmeta['_shipping_state'] = isset($woocommerce->countries->states[$postmeta['_shipping_country']][$postmeta['_shipping_state']]) ? $woocommerce->countries->states[$postmeta['_shipping_country']][$postmeta['_shipping_state']] : $postmeta['_shipping_state']; $postmeta['_shipping_country'] = isset($woocommerce->countries->countries[$postmeta['_shipping_country']]) ? $woocommerce->countries->countries[$postmeta['_shipping_country']] : $postmeta['_shipping_country']; $data['display_id'] = $data['id']; //Code for Sequential Orders compatibility if ($order_formatted != "" && isset($postmeta['_order_number_formatted'])) { $data['display_id'] = $postmeta['_order_number_formatted']; } $records[] = array_merge($postmeta, $data); } } unset($meta_value); unset($meta_key); unset($postmeta); unset($results); } } if (!isset($_POST['label']) || !empty($_POST['label']) && $_POST['label'] != 'getPurchaseLogs') { $encoded['items'] = $records; $encoded['totalCount'] = $num_records; // Code for passing the column headers for export for handling custom columns if (!empty($_POST['func_nm']) && $_POST['func_nm'] == 'exportCsvWoo' && $active_module == 'Products') { $encoded['column_header'] = $export_column_header; } unset($records); return $encoded; } }
function fp_rac_get_formatted_variation($variation) { if (function_exists('woocommerce_get_formatted_variation')) { return woocommerce_get_formatted_variation($variation, true); } else { if (function_exists('wc_get_formatted_variation')) { return wc_get_formatted_variation($variation, true); } } }