//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 text to product', 'wpt_events_products', 'product', 'side', 'default');
}
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
function wpt_events_products() {
//checkbox to select add text or not
echo '<input id="wp_add_text_to_woo_product" name="text_enable" type="checkbox"></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 ) {
$args = array('post_type' => 'product', 'nopaging' => true);
$product_query = new WP_Query($args);
$posts = $product_query->get_posts();
foreach ($posts as $post) {
$text_status = get_post_meta($post->ID,'enable_text',true);
if($text_status == "on") {
$addedtext = '(Upfront Paid in Full Price)';
return $price . '<br /><span class="price-disclaimer">' . $addedtext . '</span>';
}
else {
return $price;
}
}
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
задан linzbuck 31 December 2018 в 05:05
поделиться