Save custom fields data
-
I added custom code on the new-product-single.php on Dokan, to add new fields – a text area, a select and a checkbox. Using the functions.php I also added the custom fields on the backend. I made it so the introduced data in the edit product page is sent to the edit product page in the backend (in the admin). The data gets sent and stores in the custom fields on the backend. However, when a vendor wants to edit their product on the front-end, and they get in to edit their product, the data on those custom fields does not appear.
How can I make it for the vendor to update their product and the data to get stored inside that edit product page for when they want to review the data they put?This is the code in the new-product-single.php:
<!-- Nuevo campo URL--> <div class="dokan-form-group"> <div class="dokan-input-group"> <label for="post_url" class="form-label"><?php esc_html_e( 'URL del blog', 'dokan-lite' ); ?></label> <input class="dokan-form-control" name="_new_field" id="_new_field" type="text" placeholder="https://" value="<?php echo dokan_posted_input( '_new_field' ); ?>"> </div> </div> <br/> <!-- Nuevo campo tipo de enlaces --> <div class="dokan-form-group"> <div class="dokan-input-group"> <label for="post_enlaces" class="form-label"><?php esc_html_e( 'Tipo de enlaces que permites', 'dokan-lite' ); ?></label> <input name="_follow" id="_follow" type="checkbox" value="<?php echo dokan_posted_input( '_follow' ); ?>" /> Follow </div> </div> <div class="dokan-form-group"> <div class="dokan-input-group"> <input name="_nofollow" id="_nofollow" type="checkbox" value="<?php echo dokan_posted_input( '_nofollow' ); ?>" /> No Follow </div> </div> <div class="dokan-form-group"> <div class="dokan-input-group"> <label for="post_numenlaces" class="form-label"><?php esc_html_e( 'Numero de enlaces que permites', 'dokan-lite' ); ?></label> <div class="dokan-w4"> <select name="_numenlaces" id="_numenlaces" type="select" value="<?php echo dokan_posted_input( '_numenlaces' ); ?>"> <option value="uno">1</option> <option value="dos">2</option> <option value="tres">3</option> <option value="cuatro">4</option> </select> </div> </div> </div>
And this is what I have in my functions.php:
function x_add_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Campo URL woocommerce_wp_text_input( array( 'id' => '_new_field', 'label' => __( 'URL del blog', 'woocommerce' ), 'placeholder' => '', 'description' => __( 'Inserta la URL de la página web.', 'woocommerce' ), 'type' => 'string', /*'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) */ ) ); echo '</div>'; echo '<div class="options_group">'; // Campo tipo enlaces woocommerce_wp_checkbox( array( 'id' => '_follow', 'wrapper_class' => 'show_if_simple', 'label' => __( 'Tipo de enlaces', 'woocommerce' ), 'description' => __( 'Follow', 'woocommerce' ) ) ); echo '</div>'; woocommerce_wp_checkbox( array( 'id' => '_nofollow', 'wrapper_class' => 'show_if_simple', 'label' => __( '', 'woocommerce' ), 'description' => __( 'No Follow', 'woocommerce') ) ); echo '</div>'; echo '<div class="options_group">'; woocommerce_wp_select( array( 'id' => '_numenlaces', 'label' => __( 'Numero de enlaces', 'woocommerce' ), 'options' => array( 'uno' => __( '1', 'woocommerce' ), 'dos' => __( '2', 'woocommerce' ), 'tres' => __( '3', 'woocommerce' ), 'cuatro' => __( '4', 'woocommerce') ) ) ); echo '</div>'; echo '<div class="options_group">'; } // Save Fields - los de dokan tienen que ir primero para que se guarden los cambios add_action( 'dokan_process_product_meta', 'x_add_fields_save' ); add_action( 'dokan_new_product_added', 'x_add_fields_save' ); add_action( 'woocommerce_product_options_general_product_data', 'x_add_fields' ); add_action( 'woocommerce_process_product_meta', 'x_add_fields_save' ); function x_add_fields_save( $post_id ){ // Campo URL $woocommerce_new_field = $_POST['_new_field']; if( !empty( $woocommerce_new_field) ) update_post_meta( $post_id, '_new_field', esc_attr( $woocommerce_new_field ) ); //Campos tipo enlaces $woocommerce_follow = isset( $_POST['_follow'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_follow', esc_attr( $woocommerce_follow ) ); $woocommerce_nofollow = isset( $_POST['_nofollow'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_nofollow', esc_attr( $woocommerce_nofollow ) ); //Campo numero de enlaces $woocommerce_numenlaces = $_POST['_numenlaces']; update_post_meta( $post_id, '_numenlaces', esc_attr( $woocommerce_numenlaces ) );
The page I need help with: [log in to see the link]
- The topic ‘Save custom fields data’ is closed to new replies.