Forked from greenhornet79/Endo-Stock-Report-Exporter.php
Last active
April 19, 2017 17:41
-
-
Save CapWebSolutions/adcc5225afe061c804a1c7fc10758e78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Stock Report Exporter | |
* Plugin URI: http://www.capwebsolutions.com | |
* Description: A custom stock report exporter plugin for WooCommerce | |
* Version: 2.0.0 | |
* Author: Cap Web Solutions | Matt Ryan | |
* Author URI: http://www.capwebsolutions.com | |
* License: GPL2 | |
* Forked from https://gist.github.com/greenhornet79/74107d429dbf229a2435 | |
*/ | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
// create Stock Report admin menu item as sub to WooCommerce | |
add_action('admin_menu', 'capweb_stock_report_admin_menu'); | |
function capweb_stock_report_admin_menu() { | |
add_submenu_page('woocommerce', 'Stock Report Export', 'Stock Report Export', 'manage_options', 'capweb_stock_report', 'capweb_admin_stock_report_page'); | |
} | |
function capweb_admin_stock_report_page() { | |
?> | |
<div class="wrap"> | |
<h2>Stock Report Export to CSV</h2> | |
<p>Click export below to generate a stock report of the products on this site.</p> | |
<form method="post" id="export-form" action=""> | |
<?php submit_button('Export Stock Report', 'primary', 'download_csv' ); ?> | |
</form> | |
</div> | |
<?php | |
} | |
add_action('admin_init', 'capweb_stock_report_admin_init'); | |
function capweb_stock_report_admin_init() { | |
global $plugin_page; | |
if ( isset($_POST['download_csv']) && $plugin_page == 'woo_stock_report' ) { | |
generate_stock_report_csv(); | |
die(); | |
} | |
} | |
function generate_stock_report_csv() { | |
// output headers so that the file is downloaded rather than displayed | |
header('Content-Type: text/csv; charset=utf-8'); | |
// set file name with current date and time | |
header('Content-Disposition: attachment; filename=endo-stock-report-' . date('Y-m-d') . '-T' . date('H-i') . '.csv'); | |
// create a file pointer connected to the output stream | |
$output = fopen('php://output', 'w'); | |
// set the column headers for the csv | |
$headings = array( 'Product', 'Stock' ); | |
// output the column headings | |
fputcsv($output, $headings ); | |
// get all simple products where stock is managed | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'meta_query' => array( | |
array( | |
'key' => '_manage_stock', | |
'value' => 'yes' | |
) | |
), | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'product_type', | |
'field' => 'slug', | |
'terms' => array('simple'), | |
'operator' => 'IN' | |
) | |
) | |
); | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
global $product; | |
$row = array( $product->get_title(), $product->stock ); | |
fputcsv($output, $row); | |
endwhile; | |
// get all product variations where stock is managed | |
$args = array( | |
'post_type' => 'product_variation', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'meta_query' => array( | |
array( | |
'key' => '_stock', | |
'value' => array('', false, null), | |
'compare' => 'NOT IN' | |
) | |
) | |
); | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
$product = new WC_Product_Variation( $loop->post->ID ); | |
/* Use next line if don't want all variations of product' */ | |
// $row = array( $product->get_title() . ', ' . get_the_title( $product->variation_id ), $product->stock ); | |
/* This section for all product variations */ | |
$variation = wc_get_product( $product->variation_id ); | |
$var_attr = $variation->get_variation_attributes(); | |
foreach( $var_attr as $key => $value ) { | |
$row = array( $product->get_title() . ', ' . $var_attr[$key], $product->stock ); | |
} | |
/* End of all variation option */ | |
fputcsv($output, $row); | |
endwhile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment