Не только предыдущие ответы на код не сохраняют статус флажка, но также обновляют все цены, а не только отдельные продукты. Попробуйте этот код ниже:
//add meta box to each product which will take status to add text or not
function add_events_metaboxes() {
add_meta_box('wpt_events_product', 'Add \'From only\' text to product', 'wpt_events_products', 'product', 'advanced', 'default');
}
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
function wpt_events_products($post) {
$text_status = get_post_meta( $post->ID, 'enable_text', true) ;
//checkbox to select add text or not
echo '<input id="wp_add_text_to_woo_product" name="text_enable" type="checkbox" '. checked('on', $text_status, false) .'></input>';
}
//function will update post meta on save post button
function my_project_updated_post_meta( $post_id ) {
update_post_meta($post_id, 'enable_text', $_POST['text_enable']);
}
add_action( 'save_post', 'my_project_updated_post_meta' );
//function will check for perticular product for which check box is set and assign text for them.
function custom_price_message( $price, $product ) {
$text_status = get_post_meta( $product->get_id(), 'enable_text', true) ;
if($text_status == "on") {
$addedtext = 'From only ';
return '<span class="price-prefix-text">' . $addedtext . '</span>' . $price;
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message', 10, 2 );