正如标题所说,我有一个多产品“测试”网站。并且每个产品在商店页面都有“X有库存”。 我的网站按类别过滤,例如“地板”和“百叶窗”。
我的问题是如何更改和发短信“X 库存”来想象“X 库存盒子”。我想要一个简单的解决方案,不影响其他产品类别。
喜欢Floor的分类展示:“库存X盒” 而Blind的品类展示:“库存X米”
类似这样的事情。谢谢你~!
function ak_woocommerce_get_availability( $availability, $product ) {
// Specific categories
$specific_categories = array( 'test', 'test-1' );
if ( $product->is_in_stock() && has_term( $specific_categories, 'product_cat', $product->get_id() ) ) {
$availability['availability'] = __('My custom text', 'woocommerce' );
}
return $availability;
}
add_filter( 'woocommerce_get_availability', 'ak_woocommerce_get_availability', 10, 2 );
add_action( 'woocommerce_after_shop_loop_item_title', 'xstore_stock_catalog', 10 );
function xstore_stock_catalog() {
global $product;
if ( $product->is_in_stock() && has_term( 'rodapes', 'product_cat', $product->get_id() ) ) {
echo '<div class="et-stock" >' . $availability['availability'] = $product->get_stock_quantity() . __( ' Rodapés', 'woocommerce' ) . '</div>'; }
}
这对我有用:)
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {
// Check if the product is a variation and get the parent product ID if so
if ( $product->is_type( 'variation' ) ) {
$product_id = $product->get_parent_id();
} else {
$product_id = $product->get_id();
}
// Get the product categories
$categories = get_the_terms( $product_id, 'product_cat' ); // Using 'product_cat' as the taxonomy slug
// Check if the product categories were retrieved successfully
if ( $categories && ! is_wp_error( $categories ) ) {
foreach ( $categories as $category ) {
// Check if the product is in category 336
if ( $category->term_id == 336 ) {
$availability_text = str_replace( 'in stock', __( 'places available', 'woocommerce' ), $availability_text );
}
// Check if the product is in category 342
if ( $category->term_id == 342 ) {
$availability_text = str_replace( 'en stock', __( 'places disponibles', 'woocommerce' ), $availability_text );
}
}
}
return $availability_text;
}