![](/img/trans.png)
[英]Displaying the 'out of stock' string on shop page but not the stock quantity.
[英]Change the Label on the shop page to display stock quantity
實際上,您的問題有兩個部分:當滿足產品庫存級別的某些標准時,如何更改庫存可用性文本以及如何刪除銷售徽章。
通過掛鈎woocommerce_get_availability
過濾器可以輕松更改庫存可用性文本。 獲取產品庫存數量並相應地修改文本。 例如:
add_filter( 'woocommerce_get_availability', 'custom_override_get_availability', 10, 2 );
function custom_override_get_availability( $availability, $_product ) {
$stock_quantity = $_product->get_stock_quantity();
if ( 0 === $stock_quantity ) {
$availability['availability'] = 'Available on backorder';
} elseif ( $stock_quantity < 10 ) {
$availability['availability'] = sprintf( 'Only %s available', $stock_quantity );
} else {
$availability['availability'] = sprintf( '%s left in stock', $stock_quantity );
}
return $availability;
}
至於刪除銷售徽章,我認為您使用的插件提供了可以覆蓋的模板。 Views
文件夾,您應該找到sale-flash.php
模板。 然后,您可以在您的主題中覆蓋這些模板並應用類似的邏輯,因為您可以訪問product
。 例如,僅當產品在售且庫存數量大於或等於 10 時才顯示銷售徽章:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $product;
?>
<?php if ( $product->is_on_sale() && $product->get_stock_quantity() >= 10 ) : ?>
<?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale wdr-onsale">' . esc_html__( 'Sale!', 'woocommerce' ) . '</span>', $post, $product ); ?>
<?php endif;
雖然我不確定您是否要在產品庫存數量少於 10 件時移除銷售徽章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.