简体   繁体   English

隐藏产品价格并禁用 Woocommerce 中特定产品类别的添加到购物车

[英]Hide product price and disable add to cart for specific product categories in Woocommerce

I'm looking for the right code that hides the prices for some specific categories in Woocommerce.我正在寻找正确的代码来隐藏 Woocommerce 中某些特定类别的价格。

I already have the code to hide the prices on de single product page:我已经有了在单个产品页面上隐藏价格的代码:

add_action( 'wp', 'remove_prices_based_on_category' );
function remove_prices_based_on_category() {
    // On product single pages 
    if ( is_product() ) {
        remove_product_price( get_the_ID() );
    }
}

function return_custom_price( $price, $instance ) {
    $price = '<span style="color:red; font-size:12px;">Call our office <strong>516.695.3110</strong> for prices.</span>';
    return $price; 
}

add_action( 'woocommerce_before_shop_loop_item', 'remove_product_price', 5, 1 ); // for each product on product listing page/shop page.
function remove_product_price( $product_id ) {
    $product_id  = get_the_ID();
    $hidden_price_category_ids = array( '27419','27421' ); // Add Product Category IDs for which the product price should be hidden.
    $product_cat_ids  = get_the_terms( $product_id, 'product_cat' ); // Getting all categories for this product.
    $cat_ids = wp_list_pluck( $product_cat_ids, 'term_id' ); // Getting all category ids for this product.   
    $result = array_intersect( $hidden_price_category_ids, $cat_ids ); // Will match hidden price categories with product categories and the cat id in the array.

    // If a hidden price category is found
    if( !empty($result) ) {
        add_filter( 'woocommerce_get_price_html', 'return_custom_price', 10, 2 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    } else {
        remove_filter( 'woocommerce_get_price_html', 'return_custom_price', 10, 2 );
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    }
}

How can I do it for WooCommerce archive pages?如何为 WooCommerce 存档页面执行此操作?

Your existing code is complicated, uncompleted and not really convenient.您现有的代码很复杂、未完成且不是很方便。 Try the following instead that will work for single product pages and archives pages too (as shop page) .请尝试以下操作,这也适用于单个产品页面和档案页面(作为商店页面)

It handles any kind of product, including variable products and their variations.它处理任何类型的产品,包括可变产品及其变体。

For defined product categories, it replace the price and disable add to cart button on related products.对于定义的产品类别,它会替换相关产品的价格并禁用添加到购物车按钮。

The code:编码:

// Custom conditional function that check for specific product categories
function check_for_defined_product_categories( $product_id ) {
    // HERE your Product Categories where the product price need to be hidden.
    $targeted_terms = array( '27419','27421' ); // Can be term names, slugs or Ids

    return has_term( $targeted_terms, 'product_cat', $product_id );
}

// Custom function that replace the price by a text
function product_price_replacement(){
    return '<span style="color:red; font-size:12px;">' . sprintf( __( "Call our office %s for prices."), '<strong>516.695.3110</strong>' ) . '</span>';
}

// Replace price by a text (conditionally)
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ){
    if( check_for_defined_product_categories( $product->get_id() ) ) {
        $price = product_price_replacement();
    }
    return $price;

}

// Hide prices and availiability on product variations (conditionally)
add_filter( 'woocommerce_available_variation', 'filter_available_variation_callback', 10, 3 ); // for Variations
function filter_available_variation_callback( $args, $product, $variation ) {
    if( check_for_defined_product_categories( $product->get_id() ) ) {
        $args['price_html'] = '';
        $args['availability_html'] = '';
    }
    return $args;
}

// Disable add to cart button (conditionally)
add_filter( 'woocommerce_variation_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
    $product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();

    if( check_for_defined_product_categories( $product_id ) ) {
        $purchasable = false;
    }
    return $purchasable;
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and work.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 避免 Woocommerce 中特定产品类别的购物车项目价格更新 - Avoid cart items price update for specific product categories in Woocommerce 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce 如何从购物车图标下拉菜单中隐藏特定产品类别的价格 - How to hide specific product categories price from cart icon dropdown 在 Woocommerce 电子邮件通知中隐藏特定产品类别的价格项目 - Hide price items for specific product categories in Woocommerce email notification 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车 - Avoid add to cart for specific product categories if user is unlogged in Woocommerce 在特定的WooCommerce产品类别上自定义“添加到购物车”按钮 - Customize Add to Cart button on specific WooCommerce product categories WooCommerce中特定产品类别的最低购物车数量 - Minimum cart amount for specific product categories in WooCommerce WooCommerce:将产品添加到购物车并覆盖价格? - WooCommerce: Add product to cart with price override? 如何隐藏添加到购物车按钮特定角色和产品类别 Woocommerce - How to hide add to cart button specific role and product category Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM