- Joined
- Jun 14, 2016
- Messages
- 1,501
- Points
- 63
I am using woocommerce with WordPress and I want to have a check box for each product which I want to hide or show price depending on my decision. How can I do this with a custom code?
add_action( 'woocommerce_product_options_pricing', 'add_hide_price_checkbox' );
function add_hide_price_checkbox() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => 'hide_price',
'wrapper_class' => 'show_if_simple',
'label' => __('Hide Price', 'woocommerce' ),
'description' => __('Check this box to hide the price of the product on the front-end.', 'woocommerce' )
)
);
echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'save_hide_price_checkbox' );
function save_hide_price_checkbox( $post_id ) {
$hide_price = isset( $_POST['hide_price'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'hide_price', $hide_price );
}
add_filter( 'woocommerce_get_price_html', 'hide_price_based_on_checkbox', 100, 2 );
function hide_price_based_on_checkbox( $price, $product ) {
if ( 'yes' === get_post_meta( $product->get_id(), 'hide_price', true ) ) {
$price = '';
}
return $price;
}
ForumWeb.Hosting is a web hosting forum where you’ll find in-depth discussions and resources to help you find the best hosting providers for your websites or how to manage your hosting whether you are new or experienced. You’ll find it all here. With topics ranging from web hosting, internet marketing, search engine optimization, social networking, make money online, affiliate marketing as well as hands-on technical support for web design, programming and more. We are a growing community of like-minded people that is keen to help and support each other with ambitions and online endeavors. Learn and grow, make friends and contacts for life.
The world's smartest hosting providers come here to discuss & share what's trending in the web hosting world!