简体   繁体   中英

show woocommerce product variations and stock qty on product admin page

I've been trying to add a variations qty to the following code with no sucess. In addition to the SKU in the code, to try and show the in stock qty remaining for each product variation on the edit.php?>post_type=product page rather than having to go into each product to see this data.

add_filter( 'woocommerce_product_get_sku', 'bbloomer_variable_product_skus_admin', 9999, 2 );


function bbloomer_variable_product_skus_admin( $sku, $product ) {
   if ( ! is_admin() ) return $sku;
   global $post_type, $pagenow;
   if ( 'edit.php' === $pagenow && 'product' === $post_type ) {
      if ( $product->is_type('variable') ) {
         $sku = '';
         foreach ( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); 
            if ( $variation && $variation->exists() ) $sku .= '(' . $variation->get_sku() . ') ';
         }
      }
   }
   return $sku;
}

The code comes from a post on Business Bloomer by Rodolfo Melogli .

The code you are using is to filter sku column only. It would have been clearly mentioned in the article where you have taken this code from.

To filter the quantity column, there is another filter that you should be using: woocommerce_admin_stock_html

Try this code below:

function custom_woocommerce_admin_stock_html( $stock_html, $product ) {
    if ( $product->is_type('variable') ) {
    foreach ( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); 
            if ( $variation && $variation->exists() ) {
                $var_qty .= '(' .$variation->get_sku() .': ' . $variation->get_stock_quantity() . ') ';
            }
         }
            $stock_html = '<p>' .$var_qty . '</p>';      

     }

    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'custom_woocommerce_admin_stock_html', 10, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM