根据这里的代码https://www.businessbloomer.com/woocommerce-stock-quantity-suffix-e-g-sq-in-kg-boxes-etc/,我有下面的代码,允许我为WooCommerce的产品数量添加后缀.一切都可以通过简单的产品完美运行.对于可变产品,即使我设置了另一个后缀,它也只显示默认后缀.例如,不显示"Bax",而是显示"m2".如何使其与可变产品一起工作?
提前感谢!
add_filter( 'woocommerce_format_stock_quantity', 'wc_stock_quantity_suffix', 9999, 2 );
function wc_stock_quantity_suffix( $stock_quantity, $product ) {
$categories = get_the_terms( $product->get_id(), 'product_cat' );
if ( $categories && ! is_wp_error( $categories ) ) {
$category_slug = $categories[0]->slug;
switch ( $category_slug ) {
case 'phone':
$stock_quantity .= ' buc.';
break;
case 'accessories':
$stock_quantity .= ' bax';
break;
case 'milk':
$stock_quantity .= ' l';
break;
default:
$stock_quantity .= ' m<sup>2</sup>';
break;
}
} else {
// If there are no categories, we use the default suffix
$stock_quantity .= ' m<sup>2</sup>';
}
return $stock_quantity;
}