在WooCommerce中以空价显示外部产品上的“售完”文本

时间:2018-01-28 11:21:45

标签: php wordpress woocommerce product price

某些亚马逊进口产品没有价格或已售罄,但未从列表中删除,以及其破坏的对象。

我正在尝试自定义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; ?>

但由于现在所有产品都已售罄,因此无法正常工作。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

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 );