Woocommerce显示价格不变的产品

时间:2017-10-24 16:22:33

标签: php wordpress woocommerce

我商店的价格显示含税,我也使用{price_excluding_tax}后缀显示不含税的价格。但是变量产品存在问题......商店只显示包含税的价格(以及促销时的旧价格)但不显示不含税的价格。两种可变产品的价格相同。我试着使用这段代码:

https://tomjesch.com/display-woocommerce-products-with-and-without-tax/

但它无法正常工作 - 它显示价格,但不会对woocommerce管理面板中的任何价格变化做出反应。因此,只有在添加新产品时才能正常工作。

提前致谢...

我的代码:

function edit_price_display() {
  $product         = WC_Product( get_the_ID() );
  $regular_price   = $product ->regular_price;
  $price           = $product->price;

  $price_incl_tax  = $price + round( $price * ( 23 / 100 ), 2 ); 
  $price_incl_tax  = number_format( $price_incl_tax, 2, ",", "" ); 
  $price           = number_format( $price, 2, ",", "" ); 

  $display_price   = '<ins><span class="woocommerce-Price-amount amount">';
  $display_price  .= ' '.$price_incl_tax .'<span class="woocommerce-Price-currencySymbol">&nbsp;zł</span>';
  $display_price  .= '</span></ins>&nbsp;';
  $display_price  .= '<small class="woocommerce-price-suffix">(netto: <span class="woocommerce-Price-amount amount">'.$price .'&nbsp;<span class="woocommerce-Price-currencySymbol">zł</span></span>)</small>';

  $display_price .= '';

  $display_regular_price ='<del><span class="woocommerce-Price-amount amount">'.$regular_price .'<span class="woocommerce-Price-currencySymbol">&nbsp;zł</span></span></del>';


  if($product->is_on_sale()) {
    echo $display_regular_price;    
  }
  echo $display_price;
}
add_filter('woocommerce_variable_price_html', 'edit_price_display');

1 个答案:

答案 0 :(得分:2)

请记住Woocommerce开发人员对该功能的评论: https://github.com/woocommerce/woocommerce/issues/14839

但如果你需要这样的结果,你可以这样做:

add_filter( 'woocommerce_get_price_html', 'my_price_prefix_suffix', 100, 2 );

function my_price_prefix_suffix( $price, $product ){
    // To add suffix, go to /wp-admin/admin.php?page=wc-settings&tab=tax
    if($product->is_type( 'variable' ) && $product->is_in_stock()){
        $price =  $price . '('.wc_price($product->get_variation_regular_price()* 1.23).' z Vat)';
    }
    return apply_filters( 'woocommerce_get_price', $price );
}

代码经过测试,按预期工作。

相关问题