Add a custom meta box to the woocommerce product.

Do you want to add a custom meta box to add additional information to a single product page? Then, this post will provide a custom snippet created specifically to solve this problem, so please stick to it. Of course, this means that implementing the solution requires some coding experience. Woocommerce is created for all types of e-commerce websites. But only meet the basic requirements of online stores. In other words, if the product is specific, other product information may need to be added to help customers make information-based decisions.
One way to do this is to create a custom meta box on the edit product page. The information is then saved and displayed on a single product page. However, a quick search can find many plug-ins to solve this problem. However, if there are many plug-ins, the site will expand. Therefore, the loading speed of the site will be negatively affected. That’s why we decided to make this tutorial for you. Adding custom meta boxes to woocommerce products today’s simple tutorial will add custom meta boxes to the woocommerce store. Using custom snippets is the recommended way to make changes in word press.
You must install or create a subtopic before continuing. This ensures that changes are not lost during updates. Don’t waste too much time. Go straight in The steps to add a custom meta box to the woocommerce product are as follows: Log in to the WordPress site and access the dashboard as an administrator. In the panel menu, click shape Menu > theme editor menu. After opening the theme editor page, browse the theme function file to add the function of custom meta box to woocommerce products. Add the following code to the PHP file:#— 1. Backend —- ##
\/\/ Adding a custom Meta container to admin products pages
add_action( ‘add_meta_boxes’, ‘create_custom_meta_box’ );
if ( ! function_exists( ‘create_custom_meta_box’ ) )
{
function create_custom_meta_box()
{
add_meta_box(
‘custom_product_meta_box’,
__( ‘Additional Product Information (optional)‘, ‘cmb’ ),
‘add_custom_content_meta_box’,
‘product’,
‘normal’,
‘default’
);
}
}
\/\/ Custom metabox content in admin product pages
if ( ! function_exists( ‘add_custom_content_meta_box’ ) ){
function add_custom_content_meta_box( $post ){
$prefix = ‘_bhww_’; \/\/ global $prefix;
$ingredients = get_post_meta($post->ID, $prefix.’ingredients_wysiwyg’, true) ? get_post_meta($post->ID, $prefix.’ingredients_wysiwyg’, true) : ”;
$benefits = get_post_meta($post->ID, $prefix.’benefits_wysiwyg’, t
rue) ? get_post_meta($post->ID, $prefix.’benefits_wysiwyg’, true) : ”;
$args[‘textarea_rows’] = 6;
echo ‘
‘.__ ( ‘Ingredients’, ‘cmb’ ).’

‘;
wp_editor( $ingredients, ‘ingredients_wysiwyg’, $args );
echo ‘

‘.__ ( ‘Benefits’, ‘cmb’ ).’

‘;
wp_editor( $benefits, ‘benefits_wysiwyg’, $args );
echo ‘

‘;
}
}
\/\/Save the data of the Meta field
add_action( ‘save_post’, ‘save_custom_content_meta_box’, 10, 1 );
if ( ! function_exists( ‘save_custom_content_meta_box’ ) )
{
function save_custom_content_meta_box( $post_id ) {
$prefix = ‘_bhww_’; \/\/ global $prefix;
\/\/ We need to verify this with the proper authorization (security stuff).
\/\/ Check if our nonce is set.
if ( ! isset( $_POST[ ‘custom_product_field_nonce’ ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ ‘custom_product_field_nonce’ ];
\/\/Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
\/\/ If this is an autosave, our form has not been submitted, so we don’t want to do anything.
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return $post_id;
}
\/\/ Check the user’s permissions.
if ( ‘product’ == $_POST[ ‘post_type’ ] ){
if ( ! current_user_can( ‘edit_product’, $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( ‘edit_post’, $post_id ) )
return $post_id;
}
\/\/ Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, $prefix.’ingredients_wysiwyg’, wp_kses_post($_POST[ ‘ingredients_wysiwyg’ ]) );
update_post_meta( $post_id, $prefix.’benefits_wysiwyg’, wp_kses_post($_POST[ ‘benefits_wysiwyg’ ]) );
}
}
## —- 2. Front-end —- ##
\/\/ Create custom tabs in product single pages
add_filter( ‘woocommerce_product_tabs’, ‘custom_product_tabs’ );
functi
Regain the method of woocommerce woocommerce change the email template method add the custom background of woocommerce woocommerce set the method of online store woocommerce product page upload image method woocommerce product add star method woocommerce custom order system How to use product generation page elementor pro to customize woocommerce product page, how to add information to woocommerce shop page, how to add shopping cart View button, how to hide product library, how to add custom classification to products, and how to add new columns to woocommerce order page How to move woocommerce products to a new site

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *