This topic contains 2 reply and 2 voices, and was last updated by Nayem 4 years, 3 months ago
Viewing 2 Posts - 1 through 2 (of 2 total)
Author Posts
June 24, 2015 at 9:35 pm 53028
Nayem 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.
June 25, 2015 at 8:29 pm 53212
Bryan Bryan

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!

June 27, 2015 at 9:13 am 53310
Nayem Nayem

Hello Bryan,

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

Viewing 2 Posts - 1 through 2 (of 2 total)