Support » Plugin: WooCommerce » Variable product and standard product quantity totals in admin

  • Resolved JapeNZ

    (@japenz)


    Hi there,

    I’m trying to create a column in the admin products page that will show the total product amounts for variable and simple products.
    I’d also like it to be sortable if possible.

    At present the product count lists stock as ‘In stock × 7’ for simple products and ‘In stock’ for variable products without an in stock amount.

    Is there a way I can have the variable stock amount display a total amount for all variants?

    For example if a product has colour variations such as Red x3, Blue x4 the stock amount in the admin products page would read ‘In stock × 7’.

    I found Francesco Di Candia’s solution for showing the total stock quantity of a variable product “Woocommerce – Trying to get variable product quantity sum in admin”

    function add_qty_admin( $column ) {
        if (!isset($columns['total_qty']))
        $columns['total_qty'] = "Total Stock";
        return $columns;
    }
    add_filter( 'manage_posts_columns', 'add_qty_admin' );
    
    function admin_post_data_row($column_name, $post_id)
    {
        global $wpdb;
        switch($column_name)
        {
            case 'total_qty':
                $query = "SELECT sum(meta_value)
                          FROM $wpdb->posts AS p, $wpdb->postmeta AS s
                          WHERE p.post_parent = %d
                          AND p.post_type = 'product_variation'
                          AND p.post_status = 'publish'
                          AND p.id = s.post_id
                          AND s.meta_key = '_stock'";
    
            $product_qty = $wpdb->get_var($wpdb->prepare($query,$post_id));
            if ($product_qty) echo $product_qty;
            break;
    
        default:
            break;
    }
    }
    add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);

    However if the product isn’t a variable product the area is left blank, also this seems to cause some conflicts.

    And finally if this can be done would there be a way to make the column sortable?

    Thanks for your help!

    Kind regards, JP

    https://wordpress.org/plugins/woocommerce/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Partially solved!

    I’ve managed to work out how to get the variables total in the ‘In stock’ column and it was frustrating easy considering how long I’ve been trying to solve this haha!

    So, in the variable product got to Product Data / Inventory – Manage Stock and tick the box but leave the Stock Qty at 0.
    Then apply the stock amounts under the variations section.

    Now in the admin products page the total number of variant products will be displayed! Woohoo!

    So does anyone have any idea how I can make the ‘In stock’ column sortable?

    Thanks for your help!

    Kind regards,
    JP

    Okay so I can make the simple products sortable using this code:

    add_filter( 'manage_edit-product_sortable_columns', 'my_make_stock_sortable' );
    function my_make_stock_sortable( $sortable_columns ) {
    
       $sortable_columns[ 'is_in_stock' ] = '_stock';
       return $sortable_columns;
    
    }
    
    add_action( 'pre_get_posts', 'my_is_in_stock_orderby' );
    function my_is_in_stock_orderby( $query ) {
        if( ! is_admin() )
            return;
    
        $orderby = $query->get( 'orderby');
    
        if( '_stock' == $orderby ) {
            $query->set('meta_key','_stock');
            $query->set('orderby','meta_value_num');
        }
    }

    However it isn’t sorting the variable product amounts as well.
    Am I able to order by whatever the In stock column is displaying?
    I thought it was ‘_stock’ but clearly not.

    Any help would be very much appreciated 🙂

    Kind regards,
    JP

    Is there some way I can / should be using this?

    /**
    	 * Get total stock.
    	 *
    	 * This is the stock of parent and children combined.
    	 *
    	 * @return int
    	 */
    	public function get_total_stock() {
    		if ( empty( $this->total_stock ) ) {
    			if ( sizeof( $this->get_children() ) > 0 ) {
    				$this->total_stock = max( 0, $this->get_stock_quantity() );
    				foreach ( $this->get_children() as $child_id ) {
    					if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) {
    						$stock = get_post_meta( $child_id, '_stock', true );
    						$this->total_stock += max( 0, wc_stock_amount( $stock ) );
    					}
    				}
    			} else {
    				$this->total_stock = $this->get_stock_quantity();
    			}
    		}
    		return wc_stock_amount( $this->total_stock );
    	}
    Plugin Author Mike Jolley

    (@mikejolley)

    Not easy – you can only sort on meta data, not functions. You’re sorting products, so the _stock of variations (these are children posts) won’t be available to sort on. Thats the main reason we don’t allow sorting on this column in core.

    Hi Mike,
    Thanks for getting back to me!

    Fair enough mate, being able to view the total stock will prove handy anyway.

    I’m assuming it would be difficult (impossible?) to use the ‘get total stock’ code to define a custom meta key that I can then call for sorting?

    Anyway thanks again, if there’s some reference material you think I might find useful please let me know 🙂

    Kind regards,
    JP

    Plugin Author Mike Jolley

    (@mikejolley)

    No, wouldn’t be possible since its a function, and you’re adjusting the MySQL query. Only way would be to sync the field to some meta somewhere for all products.

    Fair enough Mike, thank you for your time 🙂

    Yes @JapenNZ! It works! 😉

    Thanks!

    Oh wait!

    It hides the Projects List from the WordPress Projects Menu…

    What’s wrong?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Variable product and standard product quantity totals in admin’ is closed to new replies.