【问题标题】:wordpress/woocommerce: Remove price if zerowordpress/woocommerce:如果为零,则删除价格
【发布时间】:2015-04-10 19:11:24
【问题描述】:

我正忙于一个 wordpress/woocommerce 网站,我想在产品类别/存档页面上的商品价格为零时隐藏该商品的价格。

我试过在网上和 php 文件中查找,但找不到应该添加 if 语句的位置。

我的 php 知识非常有限。我想知道其他人是否有这方面的经验或可以以正确的方式帮助我

谢谢!

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:

此代码适用于 Woocommerce 3.6.3。复制到你的functions.php中

//Hide Price when Price is Zero
add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if($product->get_price()>0){
          return $price_html;
     }
     return '';
 } 
// End of above code

【讨论】:

  • 这适用于我在 WooCommerce 4.5.2 中。我检查了服务器日志,也没有抛出任何警告,所以这个实现是有效的。我还检查了is_admin(),因此后端产品列表中的价格不受影响。
【解决方案2】:

这是另一种方法:

add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if($product->get_price()>0){
          return $price_html;
     }
     return '';
 }

【讨论】:

  • 上面的代码很优雅,但在与 Woocommerce 3.6.3 一起使用时出现错误Notice: "price" was called incorrectly. Product properties should not be accessed directly. 查看我的新答案,该代码再次起作用
【解决方案3】:

我还没有测试过,但我希望这样的东西可以完成这项工作。将此添加到您的 functions.php 文件中。

add_action('woocommerce_before_shop_loop_item','custom_remove_loop_price');
function custom_remove_loop_price(){
    global $product;
    if(!$product->price){
        remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
    }
}

【讨论】:

  • 它从类别页面中删除所有价格。不是 100% 我想要的,但它可能是我网站的更好解决方案。非常感谢!
  • 这就是这段代码的问题,如果 $product->price 在循环中返回 false 一次,它无论如何都会隐藏所有价格。这不是答案。
【解决方案4】:

这个终于为我做了(使用 woocommerce 3.0.8):

function cw_change_product_price_display( $price ) {
    $clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($price))))));
    if($clear == "0 00"){
      return '';
    }
    return $price;
  }
  add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
  add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

【讨论】:

  • 例如?到目前为止,我没有看到任何问题。但是,如果您给我提示,我可以检查一下;)
【解决方案5】:

!is_admin()

add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if( $product->get_price() == 0 && !is_admin() ){
          return ''; //or return 'Any text'
     }
     return $price_html;
 } 

【讨论】:

    【解决方案6】:

    尝试了 Jared 的答案,但正如有人所说,一旦满足条件,它就会删除所有价格。最终,我采用了这个解决方案,如果价格为 0,它会删除操作,如果不是,则将其添加回来。似乎有效。

    public function woocommerce_after_shop_loop_item_title_remove_product_price(){
        global $product;
        $price = floatval( $product->get_price() );
        if( $price <= 0 ){
            remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
        }else{
            add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
        }
    }
    
    add_action( 'woocommerce_after_shop_loop_item_title', 'category_page_changes', 0 );
    

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2013-04-28
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多