woocommerce multiple custom fields code

I used a guide to add multiple custom fields to the general tab in product post setting.

When I enter data into the fields, it saves and shows on product page without problem;
The problem is that when I erase them, data cleaned from fields but created divs and other html elements remain on the product page.

This is the code I used in functions.php in my theme folder:

// Custom field Type

?> <p class=”form-field custom_field_type”>
<label for=”custom_field_type”><?php echo __( ‘ویژگی های کلیدی محصول’, ‘woocommerce’ ); ?></label>
<span class=”wrap”>
<?php $custom_field_type = get_post_meta( $post->ID, ‘_custom_field_type’, true ); ?>
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_one” value=”<?php echo $custom_field_type[0]; ?>” step=”any” min=”0″ />
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_two” value=”<?php echo $custom_field_type[1]; ?>” step=”any” min=”0″ />
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_three” value=”<?php echo $custom_field_type[2]; ?>” step=”any” min=”0″ />
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_four” value=”<?php echo $custom_field_type[3]; ?>” step=”any” min=”0″ />
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_five” value=”<?php echo $custom_field_type[4]; ?>” step=”any” min=”0″ />
<input placeholder=”<?php _e( ”, ‘woocommerce’ ); ?>” type=”text” name=”_field_six” value=”<?php echo $custom_field_type[5]; ?>” step=”any” min=”0″ />
</span>
<span class=”description”><?php _e( ‘شش ویژگی کلیدی محصول را بنویسید’, ‘woocommerce’ ); ?></span> </p> <?php

and for saving also in functions.php:

// Custom Field $custom_field_type = array(
esc_attr( $_POST[‘_field_one’] ), esc_attr( $_POST[‘_field_two’] ),
esc_attr( $_POST[‘_field_three’] ), esc_attr( $_POST[‘_field_four’] ),
esc_attr( $_POST[‘_field_five’] ), esc_attr( $_POST[‘_field_six’] ) );
update_post_meta( $post_id, ‘_custom_field_type’, $custom_field_type );

in the single-product.php for shownig:

<?php
if (!empty( $custom_field_type = get_post_meta($post->ID, ‘_custom_field_type’, true))) {
echo ‘<div id=”vijegiha”>’;
echo ‘<h3 id=”vijegih3″>ویژگی های کلیدی</h3>’;
echo ‘<ul>’;
foreach($custom_field_type as $vijegi)
if (!empty( $vijegi )) {
echo ‘<li>’.$vijegi.'</li>’;}
echo ‘</ul>’;
echo ‘</div>’;}else{} ?>

Read more here:: woocommerce multiple custom fields code

Leave a Reply

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