예제 #1
0
/**
 * dhwc_product_dropdown_brands function
 * @return string
 */
function dhwc_product_dropdown_brands($show_counts = 1, $hierarchical = 1, $show_uncategorized = 1, $orderby = '')
{
    global $wp_query, $woocommerce;
    if (!class_exists('WC_Product_Cat_Dropdown_Walker')) {
        include_once $woocommerce->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php';
    }
    $r = array();
    $r['pad_counts'] = 1;
    $r['hierarchical'] = $hierarchical;
    $r['hide_empty'] = 1;
    $r['show_count'] = $show_counts;
    $r['selected'] = isset($wp_query->query['product_brand']) ? $wp_query->query['product_brand'] : '';
    $r['menu_order'] = false;
    if ($orderby == 'order') {
        $r['menu_order'] = 'asc';
    } elseif ($orderby) {
        $r['orderby'] = $orderby;
    }
    $terms = get_terms('product_brand', $r);
    if (!$terms) {
        return;
    }
    $output = "<select name='product_brand' id='dropdown_product_brand'>";
    $output .= '<option value="" ' . selected(isset($_GET['product_brand']) ? $_GET['product_brand'] : '', '', false) . '>' . __('Select a brand', DHVC_WOO) . '</option>';
    $output .= woocommerce_walk_category_dropdown_tree($terms, 0, $r);
    if ($show_uncategorized) {
        $output .= '<option value="0" ' . selected(isset($_GET['product_brand']) ? $_GET['product_brand'] : '', '0', false) . '>' . __('Uncategorized', DHVC_WOO) . '</option>';
    }
    $output .= "</select>";
    echo $output;
}
/**
 * WooCommerce Dropdown categories
 * 
 * Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
 * We use a custom walker, just like WordPress does
 */
function woocommerce_product_dropdown_categories($show_counts = 1, $hierarchal = 1, $show_uncategorized = 1)
{
    global $wp_query, $woocommerce;
    include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-dropdown-walker.php';
    $r = array();
    $r['pad_counts'] = 1;
    $r['hierarchal'] = $hierarchal;
    $r['hide_empty'] = 1;
    $r['show_count'] = 1;
    $r['selected'] = isset($wp_query->query['product_cat']) ? $wp_query->query['product_cat'] : '';
    $terms = get_terms('product_cat', $r);
    if (!$terms) {
        return;
    }
    $output = "<select name='product_cat' id='dropdown_product_cat'>";
    $output .= '<option value="" ' . selected(isset($_GET['product_cat']) ? $_GET['product_cat'] : '', '', false) . '>' . __('Select a category', 'woocommerce') . '</option>';
    $output .= woocommerce_walk_category_dropdown_tree($terms, 0, $r);
    if ($show_uncategorized) {
        $output .= '<option value="0" ' . selected(isset($_GET['product_cat']) ? $_GET['product_cat'] : '', '0', false) . '>' . __('Uncategorized', 'woocommerce') . '</option>';
    }
    $output .= "</select>";
    echo $output;
}
/**
 * woocommerce_category_sales function.
 *
 * @access public
 * @return void
 */
function woocommerce_category_sales()
{
    global $start_date, $end_date, $woocommerce, $wpdb, $wp_locale;
    $first_year = $wpdb->get_var("SELECT post_date FROM {$wpdb->posts} WHERE post_date != 0 ORDER BY post_date ASC LIMIT 1;");
    $first_year = $first_year ? date('Y', strtotime($first_year)) : date('Y');
    $current_year = isset($_POST['show_year']) ? $_POST['show_year'] : date('Y', current_time('timestamp'));
    $categories = get_terms('product_cat', array('orderby' => 'name'));
    ?>
	<form method="post" action="" class="report_filters">
		<p>
			<label for="show_year"><?php 
    _e('Show:', 'woocommerce');
    ?>
</label>
			<select name="show_year" id="show_year">
				<?php 
    for ($i = $first_year; $i <= date('Y'); $i++) {
        printf('<option value="%s" %s>%s</option>', $i, selected($current_year, $i, false), $i);
    }
    ?>
			</select>

			<select multiple="multiple" class="chosen_select" id="show_categories" name="show_categories[]" style="width: 300px;">
				<?php 
    $r = array();
    $r['pad_counts'] = 1;
    $r['hierarchal'] = 1;
    $r['hide_empty'] = 1;
    $r['value'] = 'id';
    $r['selected'] = isset($_POST['show_categories']) ? $_POST['show_categories'] : '';
    include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-dropdown-walker.php';
    echo woocommerce_walk_category_dropdown_tree($categories, 0, $r);
    ?>
			</select>

			<input type="submit" class="button" value="<?php 
    _e('Show', 'woocommerce');
    ?>
" />
		</p>
	</form>
	<?php 
    $item_sales = array();
    // Get order items
    $order_items = apply_filters('woocommerce_reports_category_sales_order_items', $wpdb->get_results($wpdb->prepare("\n\t\tSELECT order_item_meta_2.meta_value as product_id, posts.post_date, SUM( order_item_meta.meta_value ) as line_total\n\t\tFROM {$wpdb->prefix}woocommerce_order_items as order_items\n\n\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id\n\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id\n\t\tLEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID\n\t\tLEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID\n\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )\n\t\tLEFT JOIN {$wpdb->terms} AS term USING( term_id )\n\n\t\tWHERE \tposts.post_type \t= 'shop_order'\n\t\tAND \tposts.post_status \t= 'publish'\n\t\tAND \ttax.taxonomy\t\t= 'shop_order_status'\n\t\tAND\t\tterm.slug\t\t\tIN ('" . implode("','", apply_filters('woocommerce_reports_order_statuses', array('completed', 'processing', 'on-hold'))) . "')\n\t\tAND\t\tdate_format(posts.post_date,'%%Y') = %s\n\t\tAND \torder_items.order_item_type = 'line_item'\n\t\tAND \torder_item_meta.meta_key = '_line_total'\n\t\tAND \torder_item_meta_2.meta_key = '_product_id'\n\t\tGROUP BY order_items.order_item_id\n\t\tORDER BY posts.post_date ASC\n\t", $current_year)));
    if ($order_items) {
        foreach ($order_items as $order_item) {
            $month = date('m', strtotime($order_item->post_date)) - 1;
            $item_sales[$month][$order_item->product_id] = isset($item_sales[$month][$order_item->product_id]) ? $item_sales[$month][$order_item->product_id] + $order_item->line_total : $order_item->line_total;
        }
    }
    if (!empty($_POST['show_categories']) && sizeof($_POST['show_categories']) > 0) {
        $show_categories = $include_categories = array_map('absint', $_POST['show_categories']);
        foreach ($show_categories as $cat) {
            $include_categories = array_merge($include_categories, get_term_children($cat, 'product_cat'));
        }
        $categories = get_terms('product_cat', array('include' => array_unique($include_categories)));
        ?>
		<div class="woocommerce-wide-reports-wrap">
			<table class="widefat">
				<thead>
					<tr>
						<th><?php 
        _e('Category', 'woocommerce');
        ?>
</th>
						<?php 
        $column_count = 0;
        for ($count = 0; $count < 12; $count++) {
            if ($count >= date('m') && $current_year == date('Y')) {
                continue;
            }
            $column_count++;
            ?>
								<th><?php 
            echo date('F', strtotime('2012-' . ($count + 1) . '-01'));
            ?>
</th>
						<?php 
        }
        ?>
						<th><strong><?php 
        _e('Total', 'woocommerce');
        ?>
</strong></th>
					</tr>
				</thead>
				<tbody><?php 
        // While outputting, lets store them for the chart
        $chart_data = $month_totals = $category_totals = array();
        $top_cat = $bottom_cat = $top_cat_name = $bottom_cat_name = '';
        for ($count = 0; $count < 12; $count++) {
            if ($count >= date('m') && $current_year == date('Y')) {
                break;
            } else {
                $month_totals[$count] = 0;
            }
        }
        foreach ($categories as $category) {
            $cat_total = 0;
            $category_chart_data = $term_ids = array();
            $term_ids = get_term_children($category->term_id, 'product_cat');
            $term_ids[] = $category->term_id;
            $product_ids = get_objects_in_term($term_ids, 'product_cat');
            if ($category->parent > 0) {
                $prepend = '&mdash; ';
            } else {
                $prepend = '';
            }
            $category_sales_html = '<tr><th>' . $prepend . $category->name . '</th>';
            for ($count = 0; $count < 12; $count++) {
                if ($count >= date('m') && $current_year == date('Y')) {
                    continue;
                }
                if (!empty($item_sales[$count])) {
                    $matches = array_intersect_key($item_sales[$count], array_flip($product_ids));
                    $total = array_sum($matches);
                    $cat_total += $total;
                } else {
                    $total = 0;
                }
                if (sizeof(array_intersect($include_categories, get_ancestors($category->term_id, 'product_cat'))) == 0) {
                    $month_totals[$count] += $total;
                }
                $category_sales_html .= '<td>' . woocommerce_price($total) . '</td>';
                $category_chart_data[] = array(strtotime(date('Ymd', strtotime('2012-' . ($count + 1) . '-01'))) . '000', $total);
            }
            if ($cat_total == 0) {
                continue;
            }
            $category_totals[] = $cat_total;
            $category_sales_html .= '<td><strong>' . woocommerce_price($cat_total) . '</strong></td>';
            $category_sales_html .= '</tr>';
            echo $category_sales_html;
            $chart_data[$category->name] = $category_chart_data;
            if ($cat_total > $top_cat) {
                $top_cat = $cat_total;
                $top_cat_name = $category->name;
            }
            if ($cat_total < $bottom_cat || $bottom_cat === '') {
                $bottom_cat = $cat_total;
                $bottom_cat_name = $category->name;
            }
        }
        sort($category_totals);
        echo '<tr><th><strong>' . __('Total', 'woocommerce') . '</strong></th>';
        for ($count = 0; $count < 12; $count++) {
            if ($count >= date('m') && $current_year == date('Y')) {
                break;
            } else {
                echo '<td><strong>' . woocommerce_price($month_totals[$count]) . '</strong></td>';
            }
        }
        echo '<td><strong>' . woocommerce_price(array_sum($month_totals)) . '</strong></td></tr>';
        ?>
</tbody>
			</table>
		</div>

		<div id="poststuff" class="woocommerce-reports-wrap">
			<div class="woocommerce-reports-sidebar">
				<div class="postbox">
					<h3><span><?php 
        _e('Top category', 'woocommerce');
        ?>
</span></h3>
					<div class="inside">
						<p class="stat"><?php 
        echo $top_cat_name . ' (' . woocommerce_price($top_cat) . ')';
        ?>
</p>
					</div>
				</div>
				<?php 
        if (sizeof($category_totals) > 1) {
            ?>
				<div class="postbox">
					<h3><span><?php 
            _e('Worst category', 'woocommerce');
            ?>
</span></h3>
					<div class="inside">
						<p class="stat"><?php 
            echo $bottom_cat_name . ' (' . woocommerce_price($bottom_cat) . ')';
            ?>
</p>
					</div>
				</div>
				<div class="postbox">
					<h3><span><?php 
            _e('Category sales average', 'woocommerce');
            ?>
</span></h3>
					<div class="inside">
						<p class="stat"><?php 
            if (sizeof($category_totals) > 0) {
                echo woocommerce_price(array_sum($category_totals) / sizeof($category_totals));
            } else {
                echo __('N/A', 'woocommerce');
            }
            ?>
</p>
					</div>
				</div>
				<div class="postbox">
					<h3><span><?php 
            _e('Category sales median', 'woocommerce');
            ?>
</span></h3>
					<div class="inside">
						<p class="stat"><?php 
            if (sizeof($category_totals) == 0) {
                echo __('N/A', 'woocommerce');
            } elseif (sizeof($category_totals) % 2) {
                echo woocommerce_price(($category_totals[floor(sizeof($category_totals) / 2)] + $category_totals[ceil(sizeof($category_totals) / 2)]) / 2);
            } else {
                echo woocommerce_price($category_totals[sizeof($category_totals) / 2]);
            }
            ?>
</p>
					</div>
				</div>
				<?php 
        }
        ?>
			</div>
			<div class="woocommerce-reports-main">
				<div class="postbox">
					<h3><span><?php 
        _e('Monthly sales by category', 'woocommerce');
        ?>
</span></h3>
					<div class="inside chart">
						<div id="placeholder" style="width:100%; overflow:hidden; height:568px; position:relative;"></div>
						<div id="cart_legend"></div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			jQuery(function(){

				<?php 
        // Variables
        foreach ($chart_data as $name => $data) {
            $varname = 'cat_' . str_replace('-', '_', sanitize_title($name)) . '_data';
            echo 'var ' . $varname . ' = jQuery.parseJSON( \'' . json_encode($data) . '\' );';
        }
        ?>

				var placeholder = jQuery("#placeholder");

				var plot = jQuery.plot(placeholder, [
					<?php 
        $labels = array();
        foreach ($chart_data as $name => $data) {
            $labels[] = '{ label: "' . esc_js($name) . '", data: ' . 'cat_' . str_replace('-', '_', sanitize_title($name)) . '_data }';
        }
        echo implode(',', $labels);
        ?>
				], {
					legend: {
						container: jQuery('#cart_legend'),
						noColumns: 2
					},
					series: {
						lines: { show: true, fill: true },
						points: { show: true, align: "left" }
					},
					grid: {
						show: true,
						aboveData: false,
						color: '#aaa',
						backgroundColor: '#fff',
						borderWidth: 2,
						borderColor: '#aaa',
						clickable: false,
						hoverable: true
					},
					xaxis: {
						mode: "time",
						timeformat: "%b",
						monthNames: <?php 
        echo json_encode(array_values($wp_locale->month_abbrev));
        ?>
,
						tickLength: 1,
						minTickSize: [1, "month"]
					},
					yaxes: [ { min: 0, tickDecimals: 2 } ]
			 	});

			 	placeholder.resize();

				<?php 
        woocommerce_tooltip_js();
        ?>
			});
		</script>
		<?php 
    }
    ?>
	<script type="text/javascript">
		jQuery(function(){
			jQuery("select.chosen_select").chosen();
		});
	</script>
	<?php 
}
/**
 * WooCommerce Dropdown categories
 * 
 * Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
 * We use a custom walker, just like WordPress does it
 */
function woocommerce_product_dropdown_categories($show_counts = 1, $hierarchal = 1)
{
    global $wp_query;
    $r = array();
    $r['pad_counts'] = 1;
    $r['hierarchal'] = $hierarchal;
    $r['hide_empty'] = 1;
    $r['show_count'] = 1;
    $r['selected'] = isset($wp_query->query['product_cat']) ? $wp_query->query['product_cat'] : '';
    $terms = get_terms('product_cat', $r);
    if (!$terms) {
        return;
    }
    $output = "<select name='product_cat' id='dropdown_product_cat'>";
    $output .= '<option value="">' . __('Select a category', 'woocommerce') . '</option>';
    $output .= woocommerce_walk_category_dropdown_tree($terms, 0, $r);
    $output .= "</select>";
    echo $output;
}
    function _show_cat_filter()
    {
        global $wp_query, $woocommerce;
        include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-dropdown-walker.php';
        $r = array();
        $r['pad_counts'] = 1;
        $r['hierarchal'] = false;
        $r['hide_empty'] = 1;
        $r['show_count'] = 0;
        $r['selected'] = isset($wp_query->query['product_cat']) ? $wp_query->query['product_cat'] : '';
        $terms = get_terms('product_cat', $r);
        if (!$terms) {
            return;
        }
        $output = '<div id="custom_select_cat">';
        $output .= "<select name='product_cat' id='wip_dropdown_product_cat'>";
        $output .= '<option value="">' . __('All Products', 'wip') . '</option>';
        $output .= woocommerce_walk_category_dropdown_tree($terms, 0, $r);
        $output .= "</select>";
        $output .= "</div>";
        echo $output;
        $shop_page = (int) woocommerce_get_page_id('shop');
        $shop_page_url = get_permalink($shop_page);
        ?>

			<script type='text/javascript'>
			/* <![CDATA[ */
				var dropdown = document.getElementById("wip_dropdown_product_cat");
				function onCustomCatChange() {
					if ( dropdown.options[dropdown.selectedIndex].value !=='' ) {
						location.href = "<?php 
        echo home_url();
        ?>
/?product_cat="+dropdown.options[dropdown.selectedIndex].value;
					} else {
						location.href = "<?php 
        echo $shop_page_url;
        ?>
";
					}
				}
				dropdown.onchange = onCustomCatChange;
			/* ]]> */
			</script>

<?php 
    }
    /**
     * Generate the table navigation above or below the table
     *
     * @since 3.1.0
     * @access protected
     */
    function display_tablenav($which)
    {
        global $woocommerce;
        include_once $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-dropdown-walker.php';
        if ('top' == $which) {
            wp_nonce_field('bulk-' . $this->_args['plural']);
            ?>

			<ul class="subsubsub">
				<li class="all"><a href="<?php 
            echo admin_url('edit.php?post_type=product&page=wc_stock_management');
            ?>
" class="<?php 
            if (empty($_REQUEST['filter_product_type'])) {
                echo 'current';
            }
            ?>
"><?php 
            _e('All', 'wc_stock_management');
            ?>
</a> |</li>
				<li class="product"><a href="<?php 
            echo admin_url('edit.php?post_type=product&page=wc_stock_management&filter_product_type=product');
            ?>
" class="<?php 
            if (!empty($_REQUEST['filter_product_type']) && $_REQUEST['filter_product_type'] == 'product') {
                echo 'current';
            }
            ?>
"><?php 
            _e('Products', 'wc_stock_management');
            ?>
</a> |</li>
				<li class="variation"><a href="<?php 
            echo admin_url('edit.php?post_type=product&page=wc_stock_management&filter_product_type=product_variation');
            ?>
" class="<?php 
            if (!empty($_REQUEST['filter_product_type']) && $_REQUEST['filter_product_type'] == 'product_variation') {
                echo 'current';
            }
            ?>
"><?php 
            _e('Variations', 'wc_stock_management');
            ?>
</a></li>
			</ul>

			<?php 
            $this->search_box(__('Search'), 'search-products');
        }
        ?>

		<div class="tablenav <?php 
        echo esc_attr($which);
        ?>
">

			<div class="alignright" style="padding: 3px 0 0 8px;">
				<input type="submit" name="save_stock" value="<?php 
        _e('Save stock quantities', 'wc_stock_management');
        ?>
" class="button button-primary" />
			</div>

			<div class="alignleft actions">
				<?php 
        $this->bulk_actions($which);
        ?>
			</div>

			<?php 
        if ('top' == $which) {
            ?>
			<div class="alignleft actions">
				<input type="hidden" name="filter_product_type" value="<?php 
            if (!empty($_REQUEST['filter_product_type'])) {
                echo $_REQUEST['filter_product_type'];
            }
            ?>
" />
				<select name="filter_manage_stock">
					<option value=""><?php 
            _e('Stock Management on or off', 'wc_stock_management');
            ?>
</option>
					<option value="yes" <?php 
            if (!empty($_REQUEST['filter_manage_stock']) && $_REQUEST['filter_manage_stock'] == 'yes') {
                selected(1);
            }
            ?>
><?php 
            _e('Managing stock', 'wc_stock_management');
            ?>
</option>
					<option value="no" <?php 
            if (!empty($_REQUEST['filter_manage_stock']) && $_REQUEST['filter_manage_stock'] == 'no') {
                selected(1);
            }
            ?>
><?php 
            _e('Not managing stock', 'wc_stock_management');
            ?>
</option>
				</select>
				<select name="filter_stock_status">
					<option value=""><?php 
            _e('Any stock status', 'wc_stock_management');
            ?>
</option>
					<option value="instock" <?php 
            if (!empty($_REQUEST['filter_stock_status']) && $_REQUEST['filter_stock_status'] == 'instock') {
                selected(1);
            }
            ?>
><?php 
            _e('In stock', 'wc_stock_management');
            ?>
</option>
					<option value="outofstock" <?php 
            if (!empty($_REQUEST['filter_stock_status']) && $_REQUEST['filter_stock_status'] == 'outofstock') {
                selected(1);
            }
            ?>
><?php 
            _e('Out of stock', 'wc_stock_management');
            ?>
</option>
				</select>
				<?php 
            global $wp_query;
            $r = array();
            $r['pad_counts'] = 0;
            $r['hierarchal'] = 1;
            $r['hide_empty'] = 1;
            $r['show_count'] = 0;
            $r['selected'] = isset($_REQUEST['filter_product_cat']) ? $_REQUEST['filter_product_cat'] : '';
            $terms = get_terms('product_cat', $r);
            if ($terms) {
                ?>
						<select name='filter_product_cat' id='dropdown_product_cat'>
							<option value=""><?php 
                _e('Any category', 'wc_stock_management');
                ?>
</option>
							<?php 
                echo woocommerce_walk_category_dropdown_tree($terms, 0, $r);
                echo '<option value="0" ' . selected(isset($_REQUEST['filter_product_cat']) ? $_REQUEST['filter_product_cat'] : '', '0', false) . '>' . __('Uncategorized', 'woocommerce') . '</option>';
                ?>
						</select>
						<?php 
            }
            ?>
				<select name="products_per_page">
					<option value=""><?php 
            _e('50 per page', 'wc_stock_management');
            ?>
</option>
					<option value="100" <?php 
            if (!empty($_REQUEST['products_per_page']) && $_REQUEST['products_per_page'] == '100') {
                selected(1);
            }
            ?>
><?php 
            _e('100 per page', 'wc_stock_management');
            ?>
</option>
					<option value="200" <?php 
            if (!empty($_REQUEST['products_per_page']) && $_REQUEST['products_per_page'] == '200') {
                selected(1);
            }
            ?>
><?php 
            _e('200 per page', 'wc_stock_management');
            ?>
</option>
					<option value="-1" <?php 
            if (!empty($_REQUEST['products_per_page']) && $_REQUEST['products_per_page'] == '-1') {
                selected(1);
            }
            ?>
><?php 
            _e('View All', 'wc_stock_management');
            ?>
</option>
				</select>
				<input type="submit" name="save_stock" value="<?php 
            _e('Filter', 'wc_stock_management');
            ?>
" class="button" />
			</div>
			<?php 
        }
        ?>
			<?php 
        $this->extra_tablenav($which);
        $this->pagination($which);
        ?>

			<br class="clear" />
		</div><?php 
    }