隐藏 Woocommerce 商店页面上的价格

问题描述 投票:0回答:1

我正在使用带有 WooCommerce 插件的 WordPress,我想隐藏商店页面的价格(例如 20 - 50 美元)。我尝试过研究它,但没有发现与这个问题相关的太多内容。

我只想隐藏商店页面上的价格,而不是单个产品页面上的价格。

我们将非常感谢您提供的任何帮助。

php wordpress woocommerce hook-woocommerce product-price
1个回答
5
投票

您可以使用这个简单的挂钩函数,从 Woocommerce 存档页面(如商店、产品类别存档和产品标签存档页面)中删除所有产品价格:

add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}

代码位于活动子主题(或主题)的 function.php 文件中。已测试且有效。

enter image description here

如果您只想定位商店页面,则必须这样做:

add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
    if( ! is_shop() ) return; // only on shop pages
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}

更新:您可能还想将添加到购物车按钮替换为商店和档案页面中产品的链接按钮

// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Not needed for variable products
    if( $product->is_type( 'variable' ) ) return $button;

    // Button text here
    $button_text = __( "View product", "woocommerce" );

    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
© www.soinside.com 2019 - 2024. All rights reserved.