php发布产品隐藏显示,PHP-隐藏产品价格,并禁用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 '' . sprintf( __( "Call our office %s for prices."), '516.695.3110' ) . '';

}

// 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文件中.经过测试和工作.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值