英文:
How to show "Shipping Class" column in WooCommerce admin product list
问题
我正在使用 WooCommerce。
我知道您可以在“所有产品”页面上显示:图片、SKU、库存、价格、类别、标签、特色和日期。我已经四处寻找,但找不到在页面上添加“运输类别”列的方法。
上面的代码显示了商品是否可见。有人能指导我如何显示产品的运输类别吗?
谢谢。
英文:
I am using woocommerce.
I know that you can show: Image, SKU, Stock, Price, Categories, Tags, Featured, and Date on the "All Products" page. I have had a look around I cannot find a way to add a "Shipping Class" column to the page.
* @snippet New Products Table Column @ WooCommerce Admin
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( 'manage_edit-product_columns', 'bbloomer_admin_products_visibility_column', 9999 );
function bbloomer_admin_products_visibility_column( $columns ){
$columns['visibility'] = 'Visibility';
return $columns;
}
add_action( 'manage_product_posts_custom_column', 'bbloomer_admin_products_visibility_column_content', 10, 2 );
function bbloomer_admin_products_visibility_column_content( $column, $product_id ){
if ( $column == 'visibility' ) {
$product = wc_get_product( $product_id );
echo $product->get_catalog_visibility();
}
}
The code above shows if an item is visible or not. Can anyone point me in the right direct to show the product's shipping class?
Thanks
答案1
得分: 2
为了在管理产品列表中显示运输类别,请使用以下修改后的代码:
add_filter('manage_edit-product_columns', 'add_admin_products_shipping_class_column', 999);
function add_admin_products_shipping_class_column($columns){
$columns['shipping_class'] = __('运输类别', 'woocommerce');
return $columns;
}
add_action('manage_product_posts_custom_column', 'get_admin_products_shipping_class_column_content', 10, 2);
function get_admin_products_shipping_class_column_content($column, $product_id){
if ($column === 'shipping_class') {
global $post, $product;
if (is_a($product, 'WC_Product')) {
$shipping_class_id = (int) $product->get_shipping_class_id();
$shipping_class = get_term($shipping_class_id, 'product_shipping_class');
echo !is_wp_error($shipping_class) ? $shipping_class->name : '<em>n/a</em>';
}
}
}
此代码应添加到活动子主题(或活动主题)的functions.php文件中。已测试并可正常运行。
英文:
To show the shipping class in Admin product list, use the following revisited code:
add_filter( 'manage_edit-product_columns', 'add_admin_products_shipping_class_column', 999 );
function add_admin_products_shipping_class_column( $columns ){
$columns['shipping_class'] = __('Shipping Class', 'woocommerce');
return $columns;
}
add_action( 'manage_product_posts_custom_column', 'get_admin_products_shipping_class_column_content', 10, 2 );
function get_admin_products_shipping_class_column_content( $column, $product_id ){
if ( $column === 'shipping_class' ) {
global $post, $product;
if( is_a( $product, 'WC_Product' ) ) {
$shipping_class_id = (int) $product->get_shipping_class_id();
$shipping_class = get_term($shipping_class_id, 'product_shipping_class');
echo ! is_wp_error($shipping_class) ? $shipping_class->name : '<em>n/a</em>';
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论