Woocommerce 中特定产品的动态销售价格显示
Dynamic sale price display for specific products in Woocommerce
我需要在功能文件中开发一个脚本,为注册用户显示特定类别的促销价。此代码工作正常。
我需要添加促销价设计
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 );
但它适用于所有产品,我如何才能仅在特定类别中调用此过滤器?
以下代码将显示您的预期输出(对于简单产品):
// Discount prices
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
function specific_discounted_product_prices( $price, $product ) {
// For logged in customers
if ( is_user_logged_in() ) {
if( has_term( 'daniel-wellington', 'product_cat' ,$product->get_id() ) ){
$price *= 0.8; // 20% discount
} elseif( has_term( 'giardino-segreto', 'product_cat' ,$product->get_id() ) ){
$price *= 0.85; // 15% discount
}
}
return $price;
}
// Display the discount
add_filter( 'woocommerce_get_price_html', 'specific_discounted_product_prices_display', 10, 2 );
function specific_discounted_product_prices_display( $price, $product ) {
// For simple products and logged in customers
if( $product->is_type('simple') && is_user_logged_in() ){
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$active_price = wc_get_price_to_display( $product );
if( $regular_price != $active_price ) {
if( $product->is_on_sale() )
$price = sprintf( 'Was: %s – Now: %s', wc_price($sale_price), wc_price($active_price) );
else
$price = sprintf( 'Was: %s – Now: %s', wc_price($regular_price), wc_price($active_price) );
}
}
return $price;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
如果你来这里是为了让它与变体一起工作,你需要以下所有过滤器:
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'specific_discounted_product_prices', 10, 2 );
并且您需要确保已更改 woocommerce_get_variation_prices_hash 因为存储的瞬变。
如果您也使用 woocommerce_product_get_sale_price 过滤器,则可以跳过 woocommerce_get_price_html。
您可能会发现我为客户创建的要点很有用
我需要在功能文件中开发一个脚本,为注册用户显示特定类别的促销价。此代码工作正常。
我需要添加促销价设计
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 );
但它适用于所有产品,我如何才能仅在特定类别中调用此过滤器?
以下代码将显示您的预期输出(对于简单产品):
// Discount prices
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
function specific_discounted_product_prices( $price, $product ) {
// For logged in customers
if ( is_user_logged_in() ) {
if( has_term( 'daniel-wellington', 'product_cat' ,$product->get_id() ) ){
$price *= 0.8; // 20% discount
} elseif( has_term( 'giardino-segreto', 'product_cat' ,$product->get_id() ) ){
$price *= 0.85; // 15% discount
}
}
return $price;
}
// Display the discount
add_filter( 'woocommerce_get_price_html', 'specific_discounted_product_prices_display', 10, 2 );
function specific_discounted_product_prices_display( $price, $product ) {
// For simple products and logged in customers
if( $product->is_type('simple') && is_user_logged_in() ){
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$active_price = wc_get_price_to_display( $product );
if( $regular_price != $active_price ) {
if( $product->is_on_sale() )
$price = sprintf( 'Was: %s – Now: %s', wc_price($sale_price), wc_price($active_price) );
else
$price = sprintf( 'Was: %s – Now: %s', wc_price($regular_price), wc_price($active_price) );
}
}
return $price;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
如果你来这里是为了让它与变体一起工作,你需要以下所有过滤器:
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'specific_discounted_product_prices', 10, 2 );
并且您需要确保已更改 woocommerce_get_variation_prices_hash 因为存储的瞬变。
如果您也使用 woocommerce_product_get_sale_price 过滤器,则可以跳过 woocommerce_get_price_html。
您可能会发现我为客户创建的要点很有用