【发布时间】:2019-09-16 14:42:16
【问题描述】:
我需要在函数文件中开发一个脚本,为注册用户显示特定类别的销售价格。此代码工作正常。
我需要为其添加销售价格设计
function thenga_customer_specific_pricing( $price, $product ) {
if ( ! is_user_logged_in() ) {
return $price;
}
$id = $product->get_id();
if( has_term( 'daniel-wellington', 'product_cat' ,$id ) ){
// Give these customers a 20% discount.
return $price * 0.8;
} elseif( has_term( 'giardino-segreto', 'product_cat' ,$id ) ){
return $price * 0.85;
} else {
return $price;
}
}
add_filter( 'woocommerce_product_get_price', 'thenga_customer_specific_pricing', 10, 2 );
我希望得到这样的输出:
之前:100 欧元 现在:80 欧元
我试过这个过滤器:
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
但它适用于所有产品,我如何才能仅在特定类别中调用此过滤器?
【问题讨论】:
标签: php wordpress woocommerce price discount