编辑Woocommerce get_price_html()格式


Editing Woocommerce get_price_html() format

这是一个price.php文件,用于显示Woocommerce中类别级别的产品价格。它产生以下输出:

₹1,504节省66%每包

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

我想编辑格式,但我不能这样做,我想使用₹1,504来操纵它。怎么得到这个?

有关在apply_filters调用中添加过滤器的更多信息,请参阅Codex中的add_filter。

From get_price_html in class-wc-product:

return apply_filters('woocommerce_get_price_html', $price, $this);

所以要添加自己的过滤器:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    return 'Was:' . str_replace( '<ins>', ' Now:<ins>', $price );
}

更多信息请点击此链接。

请将以上代码添加到您的functions.php

您也可以通过使用过滤器钩子进行更改,参见下面的示例代码。

function cs_custom_simple_product_price_html( $price ) {
  global $product;
  $parcels = 10; // Edit the number of parcels here!
  $parcel_price = $product->get_price() / 10;
  $html = '<span class="parcels">' . $parcels . 'x </span>';
  $html .= woocommerce_price( $parcel_price ) . '<br />';
  $html .= '<span class="without-interest">' . __( 'sem juros' ) . '</span><br />';
  $html .= woocommerce_price( $product->get_price() );
  return $html;
  }
  add_filter( 'woocommerce_price_html', 'cs_custom_simple_product_price_html' );