【问题标题】:Add a custom text before the price display in WooCommerce在 WooCommerce 中的价格显示之前添加自定义文本
【发布时间】:2018-04-11 12:02:55
【问题描述】:

在 WooCommerce 中,我使用此代码在价格显示中添加文本:

function cw_change_product_price_display( $price ) {
    $price .= ' TEXT';
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

页面显示为"$99,99 TEXT"

我想让它显示如下:"TEXT $99,99"

感谢您的帮助。

【问题讨论】:

  • $price = 'TEXT '.$price;
  • 工作得很好!感谢您的快速答复!

标签: php wordpress woocommerce product price


【解决方案1】:

您只需颠倒价格和文字:

add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
    // Your additional text in a translatable string
    $text = __('TEXT');

    // returning the text before the price
    return $text . ' ' . $price;
}

这应该可以按您的预期工作......

【讨论】:

    【解决方案2】:

    使用“woocommerce_currency_symbol”这样的钩子:

    add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
    function change_existing_currency_symbol( $currency_symbol, $currency ) {
      switch( $currency ) {
        case 'AUD': $currency_symbol = 'AUD$'; break;
      }
      return $currency_symbol;
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      使用此代码,如果您没有为所有产品定价,那么价格之前的文字将不会显示!

      add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
      add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
      function cw_change_product_price_display( $price ) {
      
      $text = __('text-before-price-here:');
      
      if ($price  == true) {
      return '<span class="pre-price">'. $text . '</span> ' . $price;
      }
      else {
      
      }
      }
      

      祝你好运;))

      【讨论】:

        【解决方案4】:

        你可以用这个:

        if( !function_exists("add_custom_text_prices") ) {
            function add_custom_text_prices( $price, $product ) {
                // Text
                $text_regular_price = __("Regular Price: ");
                $text_final_price = __("FinalPrice: ");
        
                if ( $product->is_on_sale() ) {
                    $has_sale_text = array(
                      '<del>' => '<del>' . $text_regular_price,
                      '<ins>' => '<br>'.$text_final_price.'<ins>'
                    );
                    $return_string = str_replace(
                        array_keys( $has_sale_text ), 
                        array_values( $has_sale_text ), 
                        $price
                    );
        
                    return $return_string;
                }
                return $text_regular_price . $price;
            }
            add_filter( 'woocommerce_get_price_html', 'add_custom_text_prices', 100, 2 );
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-04
          • 1970-01-01
          • 1970-01-01
          • 2017-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          相关资源
          最近更新 更多