Home Forums Theme Support Dokan Adding metadata to product form

This topic is: resolved

This topic contains 2 replies, has 2 voices, and was last updated by  Nayem 3 years, 11 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #53028

    Bryan
    Member
    Post count: 20

    I am attempting to add a meta field to the new product and edit product form. I have gotten the field to appear on the back-end admin panel and on the pages for the new product and the edit product page. However, I am only able to successfully save the meta from the back-end admin panel which I have disabled for users. I need the meta to be saved when the seller creates a new product or updates an existing one.

    I have done the following:
    Added this code which stores the meta successfully from the back-end admin panel:

    
    add_action( 'woocommerce_product_options_general_product_data', 'x_add_fields' );
    function x_add_fields() {
     
      global $woocommerce, $post;
      
      echo '<div class="options_group">';
      
      // Number Field
    	woocommerce_wp_text_input( 
    		array( 
    			'id'                => '_new_field', 
    			'label'             => __( 'New Field', 'woocommerce' ), 
    			'placeholder'       => '', 
    			'description'       => __( 'Enter the amount for the required deposit.', 'woocommerce' ),
    			'type'              => 'number', 
    			'custom_attributes' => array(
    				'step' 	=> 'any',
    				'min'	=> '0'
    			) 
    		)
    	);
      
      
      echo '</div>';
    	
    }
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'x_add_fields_save' );
    function x_add_fields_save( $post_id ){
    		
    	// Number Field
    	$woocommerce_new_field = $_POST['_new_field'];
    	if( !empty( $woocommerce_new_field) )
    		update_post_meta( $post_id, '_new_field', esc_attr( $woocommerce_new_field ) );
    	
    }
    

    I have added the following code to the new product form on the front-end, the field appears but does not store the new data when submitted:

    
    <div class="dokan-form-group">
         <div class="dokan-input-group">
              <span class="dokan-input-group-addon"><?php echo get_woocommerce_currency_symbol(); ?></span>
              <input class="dokan-form-control" name="_new_field" id="_new_field" type="text" placeholder="New Field" value="<?php echo dokan_posted_input( '_new_field' ); ?>">
         </div>
    </div>
    

    I have added the following code to the edit product form on the front-end, the field appears but does not store the new data when submitted:

    
    <div class="dokan-form-group">
         <div class="dokan-input-group">
              <span class="dokan-input-group-addon"><?php echo get_woocommerce_currency_symbol(); ?></span>
              <?php dokan_post_input_box( $post_id, '_service_deposit', array( 'placeholder' => 'Enter Deposit' ) ); ?>
         </div>
    </div>
    

    Please help me to store this meta when creating and updating a product from the front end. Thank you.

    #53212

    Bryan
    Member
    Post count: 20

    Issue solved, I simply needed to add the following dokan hooks before function x_add_fields_save( $post_id ):

    add_action( ‘dokan_process_product_meta', ‘x_add_fields_save' );
    add_action( ‘dokan_new_product_added', ‘x_add_fields_save' );

    Works like magic!

    #53310

    Nayem
    Moderator
    Post count: 6480

    Hello Bryan,

    I am very glad to know that your issue is now solved. Great work.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Adding metadata to product form’ is closed to new replies.