在 WooCommerce 中以空价格在外部产品上显示 "Sold Out" 文本

Display "Sold Out" text on External products with empty price in WooCommerce

部分亚马逊进口商品无价或已售罄但未下架,及其违约。

我正在尝试自定义 woocommerce/templates/loop/price.php 模板文件。

由此

<?php if ( $price_html = $product->get_price_html() ) : ?>
    <span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>

像这样

<?php if ( $price_html = $product->get_price_html() ) : 
    if (empty($price)) 
    { 
        echo '<a href="link">Sold Out</a>';} 
    else 
    { ?>
        <span class="price"><?php echo $price_html; ?></span>
    <?php }; ?>
<?php endif; ?>

但是它不能正常工作,因为现在所有产品都卖光了。

我做错了什么?

您可以尝试以下代码而不是覆盖价格模板。该代码将确保产品是外部产品并且价格为零或为空。

function op_change_price_html( $price_html, $product ) {
   if ( 'external' === $product->get_type() && empty( $product->get_price() ) ) {
       return 'Sold Out';
   }
   return $price_html;
}
add_filter( 'woocommerce_empty_price_html', 'op_change_price_html', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'op_change_price_html', 10, 2 );