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

This will customize way the add/edit to the Product of WooCommerce possible from Advanced Custom Fields.

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

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

 

WooCommerce with Advanced Custom Fields

WooCommerce with Advanced 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_acf' );
add_action( 'load-post-new.php' , 'custom_product_basic_load_acf' );

function custom_product_basic_load_acf() {
	
	add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product_acf' );
	
}

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

}

 

 

 

Step2 Create the Custom Fields Group of Advanced Custom Fields

 

Please create the Fields Group correspond to simple product of WooCommerce.
And here, correspond to the follow items.

  • Regular Price (number)
  • Sale Price (number)
  • Stock Status (select)
  • Shipping Class (select)

 

For example. And code is here.

add_action( 'acf/register_fields' , 'custom_product_basic_acf_register_fields' );

function custom_product_basic_acf_register_fields() {
	
	if( function_exists( 'register_field_group' ) ) {
		
		register_field_group(
			array (
				'id' => 'acf_basic-product-type',
				'title' => 'Basic Product Type',
				'fields' => array (
					array (
						'key' => 'field_55c80f8e1f5b6',
						'label' => 'Regular Price',
						'name' => '_regular_price',
						'type' => 'number',
						'required' => 1,
						'default_value' => '',
						'placeholder' => '',
						'prepend' => '',
						'append' => '',
						'min' => '',
						'max' => '',
						'step' => '',
					),
					array (
						'key' => 'field_55c81037f6c94',
						'label' => 'Sale Price',
						'name' => '_sale_price',
						'type' => 'number',
						'default_value' => '',
						'placeholder' => '',
						'prepend' => '',
						'append' => '',
						'min' => '',
						'max' => '',
						'step' => '',
					),
					array (
						'key' => 'field_55c81056f6c95',
						'label' => 'Stock status',
						'name' => '_stock_status',
						'type' => 'select',
						'choices' => array (
							'instock' => 'In stock',
							'outofstock' => 'Out of stock',
						),
						'default_value' => '',
						'allow_null' => 0,
						'multiple' => 0,
					),
					array (
						'key' => 'field_55c81076f6c96',
						'label' => 'Shipping class',
						'name' => 'product_shipping_class',
						'type' => 'select',
						'choices' => array (
						),
						'default_value' => '',
						'allow_null' => 0,
						'multiple' => 0,
					),
				),
				'location' => array (
					array (
						array (
							'param' => 'post_type',
							'operator' => '==',
							'value' => 'product',
							'order_no' => 0,
							'group_no' => 0,
						),
					),
				),
				'options' => array (
					'position' => 'normal',
					'layout' => 'default',
					'hide_on_screen' => array(),
				),
				'menu_order' => 0,
			)
		);

	}
	
}

 

Create Field Group of Advanced Custom Fields

Create Field Group of Advanced Custom Fields

 

 

But this alone will not work.
Because Stock Status and Shipping Class is not loaded. So it need to customize.

 

Not loaded Shipping Class

Not loaded Shipping Class

 

 

 

Step3 Customize the Fields Group code of Advanced Custom Fields

 

Shipping Class is need in the Shipping Class terms.

But get_terms can not be use in custom_product_basic_acf_register_fields function.
Because it have faster fire than init action. So it change the choices of Shipping Class from acf/load_field filter.

Please add code to functions.php.

add_filter( 'acf/load_field/name=product_shipping_class' , 'custom_product_basic_acf_load_field_product_shipping_class' );

function custom_product_basic_acf_load_field_product_shipping_class( $field ) {
	
	$product_shipping_classes = get_terms( 'product_shipping_class' , array( 'hide_empty' => 0 ) );
	
	$product_shipping_class_choices = array();

	if( !empty( $product_shipping_classes ) ) {
		
		foreach( $product_shipping_classes as $product_shipping_class ) {
			
			$product_shipping_class_choices[ $product_shipping_class->term_id ] = esc_attr( $product_shipping_class->name );
			
		}
		
	}
	
	$field['choices'] = $product_shipping_class_choices;
	
	return $field;
	
}

add_filter( 'acf/load_value/name=product_shipping_class' , 'custom_product_basic_acf_load_value_product_shipping_class' , 10 , 3 );

function custom_product_basic_acf_load_value_product_shipping_class( $value , $post_id , $field ) {
	
	$classes = get_the_terms( $post_id , 'product_shipping_class' );

	if ( $classes && ! is_wp_error( $classes ) ) {
		
		$value = current( $classes )->term_id;
		
	}

	return $value;
	
}

 

It looks like this.

 

Add Choices Shipping Class

Add Choices Shipping Class

 

 

 

Step4 Add the other fields.

 

Please add code to custom_product_basic_load_acf function.

add_action( 'edit_form_after_title' , 'custom_product_basic_edit_form_after_title' , 10 , 2 );

 

And please add code to functions.php.

function custom_product_basic_edit_form_after_title( $post ) {
	
?>

	<input type="hidden" name="product_type" value="simple" />
	
	<input type="hidden" name="_sku" value="<?php echo esc_attr( get_post_meta( $post->ID, '_sku', true ) ); ?>" />

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

<?php

}

 

 

Step5 Save the data

 

Some field data is can be possible to save.
But Shipping Class is not custom field. So it is be able to save it.

Please add code to functions.php.

 

add_filter( 'acf/update_value/name=product_shipping_class' , 'custom_product_basic_acf_update_value_product_shipping_class', 10, 3);

function custom_product_basic_acf_update_value_product_shipping_class( $value , $post_id , $field ) {
	
	$value = intval( $value );
	
	$term = term_exists( $value , 'product_shipping_class' );
	
	if ( !empty( $term ) ) {
		
		wp_set_object_terms( $post_id, $value, 'product_shipping_class');
		
	}
	
	$value = '';

	return $value;
	
}

 

And please add code to custom_product_basic_load_acf function.

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

 

And please add code to functions.php.

function custom_product_basic_save_post_acf( $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['_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' );
		
	}

}

 

 

This is complete! 🙂

It look like this.

 

Example Product with ACF

Example Product with ACF

 

 

Full code is here.

<?php


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

function custom_product_basic_load_acf() {
	
	add_action( 'add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product_acf' );

	add_action( 'edit_form_after_title' , 'custom_product_basic_edit_form_after_title' , 10 , 2 );
	add_action( 'save_post' , 'custom_product_basic_save_post_acf' , 10 , 3 );
	
}

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

}

add_action( 'acf/register_fields' , 'custom_product_basic_acf_register_fields' );

function custom_product_basic_acf_register_fields() {
	
	if( function_exists( 'register_field_group' ) ) {
		
		register_field_group(
			array (
				'id' => 'acf_basic-product-type',
				'title' => 'Basic Product Type',
				'fields' => array (
					array (
						'key' => 'field_55c80f8e1f5b6',
						'label' => 'Regular Price',
						'name' => '_regular_price',
						'type' => 'number',
						'required' => 1,
						'default_value' => '',
						'placeholder' => '',
						'prepend' => '',
						'append' => '',
						'min' => '',
						'max' => '',
						'step' => '',
					),
					array (
						'key' => 'field_55c81037f6c94',
						'label' => 'Sale Price',
						'name' => '_sale_price',
						'type' => 'number',
						'default_value' => '',
						'placeholder' => '',
						'prepend' => '',
						'append' => '',
						'min' => '',
						'max' => '',
						'step' => '',
					),
					array (
						'key' => 'field_55c81056f6c95',
						'label' => 'Stock status',
						'name' => '_stock_status',
						'type' => 'select',
						'choices' => array (
							'instock' => 'In stock',
							'outofstock' => 'Out of stock',
						),
						'default_value' => '',
						'allow_null' => 0,
						'multiple' => 0,
					),
					array (
						'key' => 'field_55c81076f6c96',
						'label' => 'Shipping class',
						'name' => 'product_shipping_class',
						'type' => 'select',
						'choices' => array (
						),
						'default_value' => '',
						'allow_null' => 0,
						'multiple' => 0,
					),
				),
				'location' => array (
					array (
						array (
							'param' => 'post_type',
							'operator' => '==',
							'value' => 'product',
							'order_no' => 0,
							'group_no' => 0,
						),
					),
				),
				'options' => array (
					'position' => 'normal',
					'layout' => 'default',
					'hide_on_screen' => array(),
				),
				'menu_order' => 0,
			)
		);

	}
	
}

add_filter( 'acf/load_field/name=product_shipping_class' , 'custom_product_basic_acf_load_field_product_shipping_class' );

function custom_product_basic_acf_load_field_product_shipping_class( $field ) {
	
	$product_shipping_classes = get_terms( 'product_shipping_class' , array( 'hide_empty' => 0 ) );
	
	$product_shipping_class_choices = array();

	if( !empty( $product_shipping_classes ) ) {
		
		foreach( $product_shipping_classes as $product_shipping_class ) {
			
			$product_shipping_class_choices[ $product_shipping_class->term_id ] = esc_attr( $product_shipping_class->name );
			
		}
		
	}
	
	$field['choices'] = $product_shipping_class_choices;
	
	return $field;
	
}

add_filter( 'acf/load_value/name=product_shipping_class' , 'custom_product_basic_acf_load_value_product_shipping_class' , 10 , 3 );

function custom_product_basic_acf_load_value_product_shipping_class( $value , $post_id , $field ) {
	
	$classes = get_the_terms( $post_id , 'product_shipping_class' );

	if ( $classes && ! is_wp_error( $classes ) ) {
		
		$value = current( $classes )->term_id;
		
	}

	return $value;
	
}

function custom_product_basic_edit_form_after_title( $post ) {
	
?>

	<input type="hidden" name="product_type" value="simple" />
	
	<input type="hidden" name="_sku" value="<?php echo esc_attr( get_post_meta( $post->ID, '_sku', true ) ); ?>" />

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

<?php

}

add_filter( 'acf/update_value/name=product_shipping_class' , 'custom_product_basic_acf_update_value_product_shipping_class', 10, 3);

function custom_product_basic_acf_update_value_product_shipping_class( $value , $post_id , $field ) {
	
	$value = intval( $value );
	
	$term = term_exists( $value , 'product_shipping_class' );
	
	if ( !empty( $term ) ) {
		
		wp_set_object_terms( $post_id, $value, 'product_shipping_class');
		
	}
	
	$value = '';

	return $value;
	
}

function custom_product_basic_save_post_acf( $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['_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' );
		
	}

}