Forked from damiencarbery/woocommerce-stock-info.php
Created
February 22, 2020 06:32
-
-
Save KoolPal/a4492148b16eb4631b299edf2f6da31f to your computer and use it in GitHub Desktop.
List stock levels in WooCommerce - List the stock level for each product and variation. https://www.damiencarbery.com/2019/10/list-stock-levels-in-woocommerce/
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 Info | |
Plugin URI: https://www.damiencarbery.com/2019/10/list-stock-levels-in-woocommerce/ | |
Description: List the stock level for each product and variation. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.1 | |
*/ | |
add_action( 'admin_enqueue_scripts', 'wsi_include_wc_admin_css', 20 ); | |
function wsi_include_wc_admin_css() { | |
wp_enqueue_style( 'woocommerce_admin_styles' ); | |
} | |
add_action('admin_menu', 'wsi_add_menu_page', 100 ); | |
function wsi_add_menu_page() { | |
add_submenu_page( 'woocommerce', 'WooCommerce Stock Info', 'Stock Info', 'manage_options', 'dcwd-woocommerce-stock-info', 'wsi_stock_info_page' ); | |
} | |
function wsi_stock_info_page() { | |
?> | |
<div class="wrap"> | |
<h1>Stock Information</h1> | |
<?php | |
$args = array( 'limit' => -1, 'orderby' => 'name', 'order' => 'ASC', 'status' => 'publish' ); | |
$all_products = wc_get_products( $args ); | |
echo '<table class="wp-list-table widefat fixed striped posts">'; | |
echo '<thead><tr><td class="manage-column">Product name</td><td class="manage-column">Variation name</td><td class="manage-column">Stock level</td><td class="manage-column">In stock?</td></tr></thead>'; | |
foreach ( $all_products as $product ) { | |
if ( 'variable' == $product->get_type() ) { | |
echo '<tr><td colspan="4"><strong>', $product->get_name(), ' (Variable product)</strong></td></tr>'; | |
$variations = $product->get_available_variations(); | |
foreach ( $variations as $variation_obj ) { | |
$variation = wc_get_product( $variation_obj['variation_id'] ); | |
//echo '<pre>', $variation, '</pre>'; | |
$stock_level = wsi_stock_level_message( $variation ); | |
echo wsi_stock_level_table_row( $variation->get_attribute_summary(), $stock_level, $variation->get_stock_status() ); | |
} | |
} | |
elseif ( 'simple' == $product->get_type() ) { | |
echo '<tr><td colspan="4"><strong>', $product->get_name(), ' (Simple product)</strong></td></tr>'; | |
$stock_level = wsi_stock_level_message( $product); | |
echo wsi_stock_level_table_row( $product->get_name(), $stock_level, $product->get_stock_status() ); | |
} | |
else { | |
// Skip over products that are not variable or simple. | |
} | |
} | |
echo '</table>'; | |
?> | |
</div> | |
<?php | |
} | |
// Generate the stock level message. | |
function wsi_stock_level_message( $product ) { | |
$stock_level = '<span style="color: red">Stock not managed</span>'; // Warning that stock not managed. | |
if ( $product->get_manage_stock() ) { | |
$stock_level = wc_stock_amount( $product->get_stock_quantity() ); | |
$low_stock_amount = wc_get_low_stock_amount( $product ); | |
if ( $stock_level <= $low_stock_amount ) { | |
$stock_level = '<mark class="outofstock">' . $stock_level . ' (low stock)</mark>'; | |
} | |
else { | |
$stock_level = '<mark class="instock">' . $stock_level . '</mark>'; | |
} | |
} | |
return $stock_level; | |
} | |
// Generate the table row with product name, stock level and stock status. | |
function wsi_stock_level_table_row( $product_name, $stock_level, $stock_status ) { | |
$stock_status_text = array( 'instock' => 'In stock', 'outofstock' => 'Out of stock', 'onbackorder' => 'On backorder' ); | |
return sprintf( '<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>', $product_name, $stock_level, $stock_status_text[ $stock_status ] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment