How to Add New Product/Edit Product items of WooCommerce from the custom fields

This will customize way the add/edit to the Product of WooCommerce possible from custom fields with add metabox.

If you want to with Advanced Custom Fields, please see it post.

How to Add Basic Product Edit of WooCommerce from the Advanced Custom Fields

 

Settings the Product data of Custom Fields

Settings the Product data of Custom Fields

 

 

 

Step1 Remove the metabox of Product Data of WooCommerce

 

Please add code to functions.php of your theme.

add_action( 'load-post.php' , 'custom_product_basic_load' );
add_action( 'load-post-new.php' , 'custom_product_basic_load' );

function custom_product_basic_load() {
	
	add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product' );
	
}

function custom_product_basic_add_meta_boxes_product() {
	
	remove_meta_box( 'woocommerce-product-data' , 'product' , 'normal' );

}

 

 

Step2 Add new metabox

 

Please add code to custom_product_basic_load function.

add_meta_box( 'custom_product_basic_metabox' , __( 'Custom Product Data' ) , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high' );

 

And please add code to functions.php.

function custom_product_basic_metabox( $post ) {
	
}

 

It is looks like this.

 

 

 

Add metabox

Add metabox

 

 

Step3 Add fields to the basic fields

 

It will create the custom fields for the basic field of General of Simple products of WooCommerce.

 

General Basic Price Fields

General Basic Price Fields

 

 

Please add code to custom_product_basic_metabox function.

function custom_product_basic_metabox( $post ) {
	
?>

	<input type="hidden" name="product_type" value="simple" />
	
	<h4><?php _e( 'General' ); ?></h4>
	
	<?php woocommerce_wp_text_input( array( 'id' => '_regular_price', 'label' => __( 'Regular Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); ?>
	
	<?php woocommerce_wp_text_input( array( 'id' => '_sale_price', 'data_type' => 'price', 'label' => __( 'Sale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')' ) ); ?>
	
	<input type="hidden" name="_sku" value="<?php echo esc_attr( get_post_meta( $post->ID, '_sku', true ) ); ?>" />

<?php

}

 

It looks like this.

 

Add Price Fields

Add Price Fields

 

Also it will add other fields in the same way.

 

 

 

Step4 Add Inventory fields

 

In this example it will simple manage the stock.

Please add code to custom_product_basic_metabox function.

	<h4><?php _e( 'Inventory' ); ?></h4>

	<input type="hidden" name="_manage_stock" value="" />
	<input type="hidden" name="_stock" value="" />
	<input type="hidden" name="_backorders" value="no" />

	<?php woocommerce_wp_select( array( 'id' => '_stock_status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array( 'instock' => __( 'In stock', 'woocommerce' ), 'outofstock' => __( 'Out of stock', 'woocommerce' ) ), 'desc_tip' => true ) ); ?>

	<input type="hidden" name="_sold_individually" value="yes" />

 

It looks like this.

 

Add Stock Status Fields

Add Stock Status Fields

 

 

Step5 Add Shipping Class Field

 

This step is add the Shipping Class. In like this way.

 

Shipping Class Fields of WooCommerce

Shipping Class Fields of WooCommerce

 

 

Please add code to custom_product_basic_metabox function.

	<h4><?php _e( 'Shipping' ); ?></h4>

	<?php $classes = get_the_terms( $post->ID, 'product_shipping_class' ); ?>
	<?php $current_shipping_class = ''; if ( $classes && ! is_wp_error( $classes ) ) $current_shipping_class = current( $classes )->term_id; ?>
	<label for="product_shipping_class"><?php _e( 'Shipping class', 'woocommerce' ); ?></label>
	<?php wp_dropdown_categories( array( 'taxonomy' => 'product_shipping_class', 'hide_empty' => 0, 'show_option_none' => __( 'No shipping class', 'woocommerce' ), 'name' => 'product_shipping_class', 'id' => 'product_shipping_class', 'selected' => $current_shipping_class, 'class' => 'select short' ) ); ?>

 

It looks like this.

 

Add Shipping Class Fields

Add Shipping Class Fields

 

 

Step6 Save the Custom Fields and relation Terms

 

This step is save the value of the added field.

Please add code to custom_product_basic_load function.

	add_action( 'save_post' , 'custom_product_basic_save_post' , 10 , 3 );

 

And please add code to functions.php.

NOTEproduct_type and product_shipping_class is not custom fields. 🙂
There is terms.

function custom_product_basic_save_post( $post_id , $post_after , $post_before ) {
	
	if ( empty( $post_id ) or empty( $post_after ) ) {
	
		return;

	}

	if( empty( $post_after->post_type ) or $post_after->post_type != 'product' ) {
		
		return false;
		
	}
	
	if ( defined( 'DOING_AUTOSAVE' ) or is_int( wp_is_post_revision( $post_after ) ) or is_int( wp_is_post_autosave( $post_after ) ) ) {

		return;

	}
	
	if ( isset( $_POST['_regular_price'] ) ) {
		
		$_regular_price = '';
		
		if( $_POST['_regular_price'] !== '' ) {

			$_regular_price = wc_format_decimal( $_POST['_regular_price'] );
			
		}
		
		update_post_meta( $post_id, '_regular_price', $_regular_price );
		
	}

	if ( isset( $_POST['_sale_price'] ) ) {
		
		$_sale_price = '';
		
		if( $_POST['_sale_price'] !== '' ) {

			$_sale_price = wc_format_decimal( $_POST['_sale_price'] );
			
		}
		
		update_post_meta( $post_id, '_sale_price', $_sale_price );
		
	}
	
	update_post_meta( $post_id, '_price', $_regular_price );

	if ( isset( $_POST['_sku'] ) ) {
		
		$_sku = '';
		
		if( !empty( $_POST['_sku'] ) ) {

			$_sku = wc_clean( stripslashes( $_POST['_sku'] ) );
			
		}
		
		update_post_meta( $post_id, '_sku', $_sku );
		
	}

	if ( isset( $_POST['_manage_stock'] ) ) {
		
		$_manage_stock = '';
		
		if( !empty( $_POST['_manage_stock'] ) ) {

			$_manage_stock = 'yes';
			
		}
		
		update_post_meta( $post_id, '_manage_stock', $_manage_stock );
		
	}

	if ( isset( $_POST['_stock'] ) ) {
		
		$_stock = '';
		
		if( !empty( $_POST['_stock'] ) ) {

			$_stock = wc_stock_amount( $_POST['_stock'] );
			
		}
		
		update_post_meta( $post_id, '_stock', $_stock );
		
	}
	
	if ( isset( $_POST['_backorders'] ) ) {
		
		$_backorders = '';
		
		if( !empty( $_POST['_backorders'] ) ) {

			$_backorders = wc_clean( $_POST['_backorders'] );
			
		}
		
		update_post_meta( $post_id, '_backorders', $_backorders );
		
	}
	
	if ( isset( $_POST['_stock_status'] ) ) {
		
		$_stock_status = '';
		
		if( !empty( $_POST['_stock_status'] ) ) {

			$_stock_status = wc_clean( $_POST['_stock_status'] );
			
		}
		
		update_post_meta( $post_id, '_stock_status', $_stock_status );
		
	}
	
	if ( isset( $_POST['_sold_individually'] ) ) {
		
		$_sold_individually = '';
		
		if( !empty( $_POST['_sold_individually'] ) ) {

			$_sold_individually = wc_clean( $_POST['_sold_individually'] );
			
		}
		
		update_post_meta( $post_id, '_sold_individually', $_sold_individually );
		
	}

	if ( isset( $_POST['product_type'] ) ) {
		
		$product_type = 'simple';
		
		if( !empty( $_POST['product_type'] ) ) {

			$product_type = sanitize_title( stripslashes(  $_POST['product_type'] ) );
			
		}
		
		wp_set_object_terms( $post_id, $product_type, 'product_type' );
		
	}

	if ( isset( $_POST['product_shipping_class'] ) ) {
		
		$product_shipping_class = 'simple';
		
		if( !empty( $_POST['product_shipping_class'] ) ) {

			$product_shipping_class = absint( $_POST['product_shipping_class'] );
			
		}
		
		wp_set_object_terms( $post_id, $product_shipping_class, 'product_shipping_class');
		
	}

}

 

 

This is complete! 🙂

It look like this.

 

 

Basic Product Item

Basic Product Item

 

 

Full code is here.

<?php


add_action( 'load-post.php' , 'custom_product_basic_load' );
add_action( 'load-post-new.php' , 'custom_product_basic_load' );

function custom_product_basic_load() {
	
	add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product' );
	add_meta_box( 'custom_product_basic_metabox' , __( 'Custom Product Data' ) , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high' );
	add_action( 'save_post' , 'custom_product_basic_save_post' , 10 , 3 );
	
}

function custom_product_basic_add_meta_boxes_product() {
	
	remove_meta_box( 'woocommerce-product-data' , 'product' , 'normal' );

}

function custom_product_basic_metabox( $post ) {
	
?>

	<input type="hidden" name="product_type" value="simple" />
	
	<h4><?php _e( 'General' ); ?></h4>
	
	<?php woocommerce_wp_text_input( array( 'id' => '_regular_price', 'label' => __( 'Regular Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); ?>
	
	<?php woocommerce_wp_text_input( array( 'id' => '_sale_price', 'data_type' => 'price', 'label' => __( 'Sale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')' ) ); ?>
	
	<input type="hidden" name="_sku" value="<?php echo esc_attr( get_post_meta( $post->ID, '_sku', true ) ); ?>" />

	<h4><?php _e( 'Inventory' ); ?></h4>

	<input type="hidden" name="_manage_stock" value="" />
	<input type="hidden" name="_stock" value="" />
	<input type="hidden" name="_backorders" value="no" />

	<?php woocommerce_wp_select( array( 'id' => '_stock_status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array( 'instock' => __( 'In stock', 'woocommerce' ), 'outofstock' => __( 'Out of stock', 'woocommerce' ) ), 'desc_tip' => true ) ); ?>

	<input type="hidden" name="_sold_individually" value="yes" />

	<h4><?php _e( 'Shipping' ); ?></h4>

	<?php $classes = get_the_terms( $post->ID, 'product_shipping_class' ); ?>
	<?php $current_shipping_class = ''; if ( $classes && ! is_wp_error( $classes ) ) $current_shipping_class = current( $classes )->term_id; ?>
	<label for="product_shipping_class"><?php _e( 'Shipping class', 'woocommerce' ); ?></label>
	<?php wp_dropdown_categories( array( 'taxonomy' => 'product_shipping_class', 'hide_empty' => 0, 'show_option_none' => __( 'No shipping class', 'woocommerce' ), 'name' => 'product_shipping_class', 'id' => 'product_shipping_class', 'selected' => $current_shipping_class, 'class' => 'select short' ) ); ?>

<?php

}

function custom_product_basic_save_post( $post_id , $post_after , $post_before ) {
	
	if ( empty( $post_id ) or empty( $post_after ) ) {
	
		return;

	}

	if( empty( $post_after->post_type ) or $post_after->post_type != 'product' ) {
		
		return false;
		
	}
	
	if ( defined( 'DOING_AUTOSAVE' ) or is_int( wp_is_post_revision( $post_after ) ) or is_int( wp_is_post_autosave( $post_after ) ) ) {

		return;

	}
	
	if ( isset( $_POST['_regular_price'] ) ) {
		
		$_regular_price = '';
		
		if( $_POST['_regular_price'] !== '' ) {

			$_regular_price = wc_format_decimal( $_POST['_regular_price'] );
			
		}
		
		update_post_meta( $post_id, '_regular_price', $_regular_price );
		
	}

	if ( isset( $_POST['_sale_price'] ) ) {
		
		$_sale_price = '';
		
		if( $_POST['_sale_price'] !== '' ) {

			$_sale_price = wc_format_decimal( $_POST['_sale_price'] );
			
		}
		
		update_post_meta( $post_id, '_sale_price', $_sale_price );
		
	}
	
	update_post_meta( $post_id, '_price', $_regular_price );

	if ( isset( $_POST['_sku'] ) ) {
		
		$_sku = '';
		
		if( !empty( $_POST['_sku'] ) ) {

			$_sku = wc_clean( stripslashes( $_POST['_sku'] ) );
			
		}
		
		update_post_meta( $post_id, '_sku', $_sku );
		
	}

	if ( isset( $_POST['_manage_stock'] ) ) {
		
		$_manage_stock = '';
		
		if( !empty( $_POST['_manage_stock'] ) ) {

			$_manage_stock = 'yes';
			
		}
		
		update_post_meta( $post_id, '_manage_stock', $_manage_stock );
		
	}

	if ( isset( $_POST['_stock'] ) ) {
		
		$_stock = '';
		
		if( !empty( $_POST['_stock'] ) ) {

			$_stock = wc_stock_amount( $_POST['_stock'] );
			
		}
		
		update_post_meta( $post_id, '_stock', $_stock );
		
	}
	
	if ( isset( $_POST['_backorders'] ) ) {
		
		$_backorders = '';
		
		if( !empty( $_POST['_backorders'] ) ) {

			$_backorders = wc_clean( $_POST['_backorders'] );
			
		}
		
		update_post_meta( $post_id, '_backorders', $_backorders );
		
	}
	
	if ( isset( $_POST['_stock_status'] ) ) {
		
		$_stock_status = '';
		
		if( !empty( $_POST['_stock_status'] ) ) {

			$_stock_status = wc_clean( $_POST['_stock_status'] );
			
		}
		
		update_post_meta( $post_id, '_stock_status', $_stock_status );
		
	}
	
	if ( isset( $_POST['_sold_individually'] ) ) {
		
		$_sold_individually = '';
		
		if( !empty( $_POST['_sold_individually'] ) ) {

			$_sold_individually = wc_clean( $_POST['_sold_individually'] );
			
		}
		
		update_post_meta( $post_id, '_sold_individually', $_sold_individually );
		
	}

	if ( isset( $_POST['product_type'] ) ) {
		
		$product_type = 'simple';
		
		if( !empty( $_POST['product_type'] ) ) {

			$product_type = sanitize_title( stripslashes(  $_POST['product_type'] ) );
			
		}
		
		wp_set_object_terms( $post_id, $product_type, 'product_type' );
		
	}

	if ( isset( $_POST['product_shipping_class'] ) ) {
		
		$product_shipping_class = 'simple';
		
		if( !empty( $_POST['product_shipping_class'] ) ) {

			$product_shipping_class = absint( $_POST['product_shipping_class'] );
			
		}
		
		wp_set_object_terms( $post_id, $product_shipping_class, 'product_shipping_class');
		
	}

}