2 回答

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
以下代碼將顯示您的預(yù)期輸出(對于簡單產(chǎn)品):
// 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;
}
代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的function.php文件中。經(jīng)過測試和工作。
- 2 回答
- 0 關(guān)注
- 181 瀏覽
添加回答
舉報(bào)