I'm developing woocommerce on WordPress and I want to make some sales badges but ignore the sales price (only the regular price).
I've tried this a few times but the "Promotion Badge" only appears when I put the number on the product's sale price
I use the following code
add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3); function woocommerce_custom_sale_text($text, $post, $_product) { global $post,$product; if ( ! $product->is_in_stock() ) return; $sale_price = get_post_meta( $product->id, '_price', true); $regular_price = get_post_meta( $product->id, '_regular_price', true); if (has_term('one', 'product_cat', $product->ID)) { return '<span class="onsale">one</span>'; } elseif (has_term('two', 'product_cat', $product->ID)) { return '<span class="onsale">two</span>'; } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) { return '<span class="onsale">three</span>'; } return '<span class="onsale">Sale</span>'; }
P粉7138664252024-01-08 13:15:16
The filter itself is only applied when the product is promoted.
You need to override the flash sale action that occurs before checking if the product is on sale.
First, remove the core flash sale hook.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 ); remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
Then, add your custom sales functionality.
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_custom_sale_text', 10 ); add_action( 'woocommerce_before_single_product_summary', 'woocommerce_custom_sale_text', 10 );
Then use echo
instead of return
function woocommerce_custom_sale_text() { global $post,$product; if ( ! $product->is_in_stock() ) return; $sale_price = get_post_meta( $product->id, '_price', true); $regular_price = get_post_meta( $product->id, '_regular_price', true); if (has_term('one', 'product_cat', $product->ID)) { echo 'one'; } elseif (has_term('two', 'product_cat', $product->ID)) { echo 'two'; } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) { echo 'three'; } echo 'Sale'; }