Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

How can I import custom fields that I created myself?

$
0
0

Replies: 0

How can I import custom fields that I created myself? I attach a screenshot of these fields in the admin panel and .php file

add_action( 'woocommerce_product_options_general_product_data', 'demolink_woo_add_custom_fields' );
function demolink_woo_add_custom_fields() {

	echo '<div class="options_group">';// Группировка полей

	// текстовое поле
	woocommerce_wp_text_input(
		[
			'id'                => 'number_phone_field',
			'label'             => __( 'Номер телефона', 'woocommerce' ),
			'placeholder'       => 'Номер телефона',
			'desc_tip'          => 'true',
			'custom_attributes' => [ 'required' => 'required' ],
			'description'       => __( 'Введите здесь значение поля', 'woocommerce' ),
		]
		
	);
	
	woocommerce_wp_text_input(
		[
			'id'                => 'fiz_adres',
			'label'             => __( 'Физический адрес компании', 'woocommerce' ),
			'placeholder'       => 'Физический адрес компании',
			'desc_tip'          => 'true',
			'custom_attributes' => [ 'required' => 'required' ],
			'description'       => __( 'Введите здесь значение поля', 'woocommerce' ),
		]
		
	);
	
	woocommerce_wp_text_input(
		[
			'id'                => 'OGRN',
			'label'             => __( 'ОГРН', 'woocommerce' ),
			'placeholder'       => 'ОГРН',
			'desc_tip'          => 'true',
			'custom_attributes' => [ 'required' => 'required' ],
			'description'       => __( 'Введите здесь значение поля', 'woocommerce' ),
		]
		
	);
	
	woocommerce_wp_text_input(
		[
			'id'                => 'INN',
			'label'             => __( 'ИНН', 'woocommerce' ),
			'placeholder'       => 'ИНН',
			'desc_tip'          => 'true',
			'custom_attributes' => [ 'required' => 'required' ],
			'description'       => __( 'Введите здесь значение поля', 'woocommerce' ),
		]
		
		
		
	);
}

add_action( 'woocommerce_process_product_meta', 'art_woo_custom_fields_save', 10 );
function art_woo_custom_fields_save( $post_id ) {

	// Вызываем объект класса
	$product = wc_get_product( $post_id );

	// Сохранение текстового поля
	$text_field = isset( $_POST['number_phone_field'] ) ? sanitize_text_field( $_POST['number_phone_field'] ) : '';
	$product->update_meta_data( 'number_phone_field', $text_field );
	
	$text_field = isset( $_POST['OGRN'] ) ? sanitize_text_field( $_POST['OGRN'] ) : '';
	$product->update_meta_data( 'OGRN', $text_field );
	
	$text_field = isset( $_POST['INN'] ) ? sanitize_text_field( $_POST['INN'] ) : '';
	$product->update_meta_data( 'INN', $text_field );
	
	$text_field = isset( $_POST['fiz_adres'] ) ? sanitize_text_field( $_POST['fiz_adres'] ) : '';
	$product->update_meta_data( 'fiz_adres', $text_field );	

	// Сохраняем все значения
	$product->save();
}

// Отображение значения настраиваемого поля
echo get_post_meta ( $post->ID , 'number_phone_field', true );
echo get_post_meta ( $post->ID , 'OGRN', true );
echo get_post_meta ( $post->ID , 'INN', true );
echo get_post_meta ( $post->ID , 'fiz_adres', true );

add_action( 'woocommerce_product_meta_start', 'art_get_fields_tab_meta_start' );
function art_get_fields_tab_meta_start() {

	// Вызываем объект товара
	$product = wc_get_product();

	// Записываем значения полей в переменные
	$text_field     = $product->get_meta( 'number_phone_field', true );
	$OGRN     = $product->get_meta( 'OGRN', true );
	$INN     = $product->get_meta( 'INN', true );
	$fiz_adres     = $product->get_meta( 'fiz_adres', true );	

	// Выводим значения полей
	if ( $text_field ) :
		?>
		<div class="text-field">
			Номер телефона:
			<span style="font-weight:200; color:#777"><?php echo $text_field; ?></span>
		</div>
	<?php endif;
	
	if ( $text_field ) :
		?>
		<div class="text-field">
			ОГРН:
			<span style="font-weight:200; color:#777"><?php echo $OGRN; ?></span>
		</div>
	<?php endif;
	
	if ( $text_field ) :
		?>
		<div class="text-field">
			ИНН:
			<span style="font-weight:200; color:#777"> <?php echo $INN; ?> </span>
		</div>
	<?php endif;
	
		if ( $text_field ) :
		?>
		<div class="text-field">
			Физический адрес компании:
			<span style="font-weight:200; color:#777"><?php echo $fiz_adres; ?> </span>
		</div>
	<?php endif;
}

Viewing all articles
Browse latest Browse all 59525

Trending Articles