ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-12-10 22:31:07  阅读:332  来源: 互联网

标签:woocommerce price taxonomy-terms wordpress php


我正在寻找正确的代码来隐藏Woocommerce中某些特定类别的价格.

我已经有代码隐藏单个产品页面上的价格:

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' );
    }
}

如何为WooCommerce存档页面执行此操作?

解决方法:

您现有的代码很复杂,未完成,并且不太方便.请尝试以下操作,该方法也将适用于单个产品页面和存档页面(作为商店页面).

它处理任何种类的产品,包括可变产品及其变体.

对于定义的产品类别,它将替换价格并禁用相关产品上的“添加到购物车”按钮.

编码:

// 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;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试和工作.

标签:woocommerce,price,taxonomy-terms,wordpress,php
来源: https://codeday.me/bug/20191210/2104530.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有