• Resolved yknivag

    (@yknivag)


    In the main WordPress admin dashboard WooCommerce places a small widget which contains the number of items “out-of-stock” and the number “low-in-stock”.

    These values come from the function status_widget_order_rows() in this file.

    However this is a private function. Is there a way of getting at these values using functions.php or in a plugin? Without running DB queries directly?

    I’m trying to build a “status page” and this would be very useful.

    If not is there an endpoint in the API that just returns the count? Or do I have to return the full list of out-of-stock products and count them?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @yknivag!

    As it is private, you won’t be able to use that function. You would need to use the wp_count_posts function to return the necessary values. Wouldn’t it be easier to just replace the status widget with your own?

    You can use

    function your_custom__dashboard_widgets_init() {
    	wp_add_dashboard_widget( 'woocommerce_dashboard_new', __( 'Status', 'woocommerce' ), 'woocommerce_dashboard_widget_right_now' );
    }
    add_action( 'wp_dashboard_setup', 'woocommerce_dashboard_new' );

    cheers!

    Thread Starter yknivag

    (@yknivag)

    Hi @rynald0s thank you for the quick reply.

    It is the two counts that I am interested in accessing rather than how to display them.

    You say this is possible with wp_count_posts(). Are products given a different post type when they are “low in stock” or “out of stock”? I see no such post types in my test store. Only product and product_variation.

    How would it be possible to return the number of products currently out of stock with the wp_count_posts() function?

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @yknivag I think what Rynaldo was referring to was to copy the code from the widget in WordPress and then use it in your own code. The code you’d need to copy is here:
    https://github.com/woocommerce/woocommerce/blob/4.3.1/includes/admin/class-wc-admin-dashboard.php#L167-L174

    Thread Starter yknivag

    (@yknivag)

    Hi @jessepearson,

    Thank you for your reply. I’m afraid I’m still thoroughly confused as to how that code would help me.

    I am looking to find the count of products or variations 'out of stock' and the count of products or variations 'low in stock'.

    Ideally I was looking for a way to get these two numbers without resorting to querying the database directly as that is very much discouraged of WordPress functions.

    I am beginning to think, from the answers to this thread, that it isn’t going to be possible.

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @yknivag I was going off of what you said here:

    These values come from the function status_widget_order_rows() in this file.

    The function mentioned is where my focus went, but I see now it’s completely unrelated and you probably meant the status_widget_stock_rows() method. The same applies, though, you would need all of the code highlighted here:
    https://github.com/woocommerce/woocommerce/blob/4.3.1/includes/admin/class-wc-admin-dashboard.php#L212-L274

    You could just try to query the transients like on lines 216 and 248, but if the transients have expired you will not get the results needed.

    Thread Starter yknivag

    (@yknivag)

    @jessepearson what I really want is to be able to get at the information that the widget in the linked picture shows programmatically.

    https://ibb.co/XCXK2ck

    The order state statistics are trivial to access with public functions but I am really struggling to find any way either in functions or in the API to get at the count of products or of stock.

    The widget appears to be working fine and to refresh every time I load the dashboard but I can never access those transients even immediately after refreshing the dashboard.

    As it appears there is no function that exposes these values and no API call either, are there flags I can check for with database queries? Or do I have to count them manually against individual low stock thresholds for each product in turn?

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @yknivag As far as I can tell, the transients work. I wrote this function to log the data retrieved from them:

    
    function get_product_counts_20200805() {
    	$transient_name   = 'wc_outofstock_count';
    	$outofstock_count = get_transient( $transient_name );
    
    	$transient_name   = 'wc_low_stock_count';
    	$lowinstock_count = get_transient( $transient_name );
    
    	error_log( '$outofstock_count: '. $outofstock_count );
    	error_log( '$lowinstock_count: '. $lowinstock_count );
    }
    add_action( 'init', 'get_product_counts_20200805' );
    

    When I went to the dashboard, this is what was logged:

    
    [05-Aug-2020 15:42:24 UTC] $outofstock_count: 
    [05-Aug-2020 15:42:24 UTC] $lowinstock_count: 
    [05-Aug-2020 15:42:30 UTC] $outofstock_count: 21
    [05-Aug-2020 15:42:30 UTC] $lowinstock_count: 9
    

    Initially nothing was logged because the transients were not set, but after they were, they returned values.

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved. If you have any further questions, a new thread can be opened.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘General Stock Statistics’ is closed to new replies.