markdown 将自定义字段添加到Woocommerce变体中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 将自定义字段添加到Woocommerce变体中相关的知识,希望对你有一定的参考价值。

```php
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
/**
 * Create new fields for variations
 *
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
	// Text Field
	woocommerce_wp_text_input( 
		array( 
			'id'          => '_text_field[' . $variation->ID . ']', 
			'label'       => __( 'My Text Field', 'woocommerce' ), 
			'placeholder' => 'http://',
			'desc_tip'    => 'true',
			'description' => __( 'Enter the custom value here.', 'woocommerce' ),
			'value'       => get_post_meta( $variation->ID, '_text_field', true )
		)
	);
	// Number Field
	woocommerce_wp_text_input( 
		array( 
			'id'          => '_number_field[' . $variation->ID . ']', 
			'label'       => __( 'My Number Field', 'woocommerce' ), 
			'desc_tip'    => 'true',
			'description' => __( 'Enter the custom number here.', 'woocommerce' ),
			'value'       => get_post_meta( $variation->ID, '_number_field', true ),
			'custom_attributes' => array(
							'step' 	=> 'any',
							'min'	=> '0'
						) 
		)
	);
	// Textarea
	woocommerce_wp_textarea_input( 
		array( 
			'id'          => '_textarea[' . $variation->ID . ']', 
			'label'       => __( 'My Textarea', 'woocommerce' ), 
			'placeholder' => '', 
			'description' => __( 'Enter the custom value here.', 'woocommerce' ),
			'value'       => get_post_meta( $variation->ID, '_textarea', true ),
		)
	);
	// Select
	woocommerce_wp_select( 
	array( 
		'id'          => '_select[' . $variation->ID . ']', 
		'label'       => __( 'My Select Field', 'woocommerce' ), 
		'description' => __( 'Choose a value.', 'woocommerce' ),
		'value'       => get_post_meta( $variation->ID, '_select', true ),
		'options' => array(
			'one'   => __( 'Option 1', 'woocommerce' ),
			'two'   => __( 'Option 2', 'woocommerce' ),
			'three' => __( 'Option 3', 'woocommerce' )
			)
		)
	);
	// Checkbox
	woocommerce_wp_checkbox( 
	array( 
		'id'            => '_checkbox[' . $variation->ID . ']', 
		'label'         => __('My Checkbox Field', 'woocommerce' ), 
		'description'   => __( 'Check me!', 'woocommerce' ),
		'value'         => get_post_meta( $variation->ID, '_checkbox', true ), 
		)
	);
	// Hidden field
	woocommerce_wp_hidden_input(
	array( 
		'id'    => '_hidden_field[' . $variation->ID . ']', 
		'value' => 'hidden_value'
		)
	);
}
/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {
	// Text Field
	$text_field = $_POST['_text_field'][ $post_id ];
	if( ! empty( $text_field ) ) {
		update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
	}
	
	// Number Field
	$number_field = $_POST['_number_field'][ $post_id ];
	if( ! empty( $number_field ) ) {
		update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
	}
	// Textarea
	$textarea = $_POST['_textarea'][ $post_id ];
	if( ! empty( $textarea ) ) {
		update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
	}
	
	// Select
	$select = $_POST['_select'][ $post_id ];
	if( ! empty( $select ) ) {
		update_post_meta( $post_id, '_select', esc_attr( $select ) );
	}
	
	// Checkbox
	$checkbox = isset( $_POST['_checkbox'][ $post_id ] ) ? 'yes' : 'no';
	update_post_meta( $post_id, '_checkbox', $checkbox );
	
	// Hidden field
	$hidden = $_POST['_hidden_field'][ $post_id ];
	if( ! empty( $hidden ) ) {
		update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
	}
}
```

以上是关于markdown 将自定义字段添加到Woocommerce变体中的主要内容,如果未能解决你的问题,请参考以下文章

markdown 将自定义JS添加到FoundationPress

markdown 将自定义帮助程序添加到Ghost CMS

如何将自定义字段添加到 WooCommerce 注册表单

Netsuite 将自定义字段添加到交易列字段

使用 paypal 节点 sdk 将自定义字段添加到计费协议

Drupal,Ubercart - 将自定义字段添加到结帐表单