Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_product_options_general_product_data', 'art_woo_add_custom_fields' );
- function art_woo_add_custom_fields() {
- global $product, $post;
- echo '<div class="options_group">';// Группировка полей
- // текстовое поле
- woocommerce_wp_text_input( array(
- 'id' => '_text_field',
- 'label' => __( 'Доля %', 'woocommerce' ),
- 'placeholder' => 'Текстовое поле',
- 'desc_tip' => 'true',
- 'custom_attributes' => array( 'required' => 'required' ),
- 'description' => __( 'Введите здесь значение поля', 'woocommerce' ),
- ) );
- // цифровое поле
- woocommerce_wp_text_input( array(
- 'id' => '_number_field',
- 'label' => __( 'Номер лота', 'woocommerce' ),
- 'placeholder' => 'Ввод чисел',
- 'description' => __( 'Вводятся только числа', 'woocommerce' ),
- 'type' => 'number',
- 'custom_attributes' => array(
- 'step' => 'any',
- 'min' => '0',
- ),
- ) );
- }
- add_action( 'woocommerce_process_product_meta', 'art_woo_custom_fields_save', 10 );
- function art_woo_custom_fields_save( $post_id ) {
- // Сохранение текстового поля
- $woocommerce_text_field = $_POST['_text_field'];
- if ( !empty($woocommerce_text_field) ) {
- update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
- // Сохранение цифрового поля
- $woocommerce_number_field = $_POST['_number_field'];
- if ( !empty($woocommerce_number_field)) {
- update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );
- }
- }}
- add_action( 'woocommerce_shop_loop_item_title', 'artabr_add_field_before_price', 15);
- function artabr_add_field_before_price() {
- global $post, $product;
- $text_field = get_post_meta( $post->ID, '_text_field', true );
- $num_field = get_post_meta( $post->ID, '_number_field', true );
- $textarea_field = get_post_meta( $post->ID, '_textarea', true );
- if ( $num_field ) { ?>
- <div class="number-field">
- <strong>Номер лота: </strong>
- <?php echo $num_field; ?>
- </div>
- <?php }
- }
- add_action( 'woocommerce_after_shop_loop_item', 'artabr_add_field_before_text', 15);
- function artabr_add_field_before_text() {
- global $post, $product;
- $text_field = get_post_meta( $post->ID, '_text_field', true );
- $num_field = get_post_meta( $post->ID, '_number_field', true );
- $textarea_field = get_post_meta( $post->ID, '_textarea', true );
- if ( $text_field ) {
- ?>
- <div class="text-field">
- <strong>Доля %: </strong>
- <?php echo $text_field; ?>
- </div>
- <?php }
- }
- add_action( 'woocommerce_before_add_to_cart_form', 'art_get_text_field_before_add_card' );
- function art_get_text_field_before_add_card() {
- // Вызываем объект товара
- $product = wc_get_product();
- // Записываем значения полей в переменные
- $text_field = $product->get_meta( '_text_field', true );
- $num_field = $product->get_meta( '_number_field', true );
- // Выводим значения полей
- if ( $text_field ) :
- ?>
- <div class="text-field">
- <strong>Доля %: </strong>
- <?php echo $text_field; ?>
- </div>
- <?php endif;
- if ( $num_field ) : ?>
- <div class="number-field">
- <strong>Номер лота: </strong>
- <?php echo $num_field; ?>
- </div>
- <?php endif;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement