Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. add_action( 'woocommerce_product_options_general_product_data', 'art_woo_add_custom_fields' );
  2. function art_woo_add_custom_fields() {
  3. global $product, $post;
  4. echo '<div class="options_group">';// Группировка полей
  5. // текстовое поле
  6. woocommerce_wp_text_input( array(
  7. 'id' => '_text_field',
  8. 'label' => __( 'Доля %', 'woocommerce' ),
  9. 'placeholder' => 'Текстовое поле',
  10. 'desc_tip' => 'true',
  11. 'custom_attributes' => array( 'required' => 'required' ),
  12. 'description' => __( 'Введите здесь значение поля', 'woocommerce' ),
  13. ) );
  14.  
  15. // цифровое поле
  16. woocommerce_wp_text_input( array(
  17. 'id' => '_number_field',
  18. 'label' => __( 'Номер лота', 'woocommerce' ),
  19. 'placeholder' => 'Ввод чисел',
  20. 'description' => __( 'Вводятся только числа', 'woocommerce' ),
  21. 'type' => 'number',
  22. 'custom_attributes' => array(
  23. 'step' => 'any',
  24. 'min' => '0',
  25. ),
  26. ) );
  27.  
  28. }
  29.  
  30. add_action( 'woocommerce_process_product_meta', 'art_woo_custom_fields_save', 10 );
  31. function art_woo_custom_fields_save( $post_id ) {
  32. // Сохранение текстового поля
  33. $woocommerce_text_field = $_POST['_text_field'];
  34. if ( !empty($woocommerce_text_field) ) {
  35. update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
  36. // Сохранение цифрового поля
  37. $woocommerce_number_field = $_POST['_number_field'];
  38. if ( !empty($woocommerce_number_field)) {
  39. update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );
  40. }
  41.  
  42. }}
  43.  
  44.  
  45.  
  46. add_action( 'woocommerce_shop_loop_item_title', 'artabr_add_field_before_price', 15);
  47. function artabr_add_field_before_price() {
  48. global $post, $product;
  49. $text_field = get_post_meta( $post->ID, '_text_field', true );
  50. $num_field = get_post_meta( $post->ID, '_number_field', true );
  51. $textarea_field = get_post_meta( $post->ID, '_textarea', true );
  52. if ( $num_field ) { ?>
  53. <div class="number-field">
  54. <strong>Номер лота: </strong>
  55. <?php echo $num_field; ?>
  56. </div>
  57. <?php }
  58.  
  59. }
  60.  
  61.  
  62. add_action( 'woocommerce_after_shop_loop_item', 'artabr_add_field_before_text', 15);
  63. function artabr_add_field_before_text() {
  64. global $post, $product;
  65. $text_field = get_post_meta( $post->ID, '_text_field', true );
  66. $num_field = get_post_meta( $post->ID, '_number_field', true );
  67. $textarea_field = get_post_meta( $post->ID, '_textarea', true );
  68. if ( $text_field ) {
  69. ?>
  70. <div class="text-field">
  71. <strong>Доля %: </strong>
  72. <?php echo $text_field; ?>
  73. </div>
  74. <?php }
  75.  
  76. }
  77.  
  78. add_action( 'woocommerce_before_add_to_cart_form', 'art_get_text_field_before_add_card' );
  79. function art_get_text_field_before_add_card() {
  80.  
  81. // Вызываем объект товара
  82. $product = wc_get_product();
  83.  
  84. // Записываем значения полей в переменные
  85. $text_field = $product->get_meta( '_text_field', true );
  86. $num_field = $product->get_meta( '_number_field', true );
  87.  
  88. // Выводим значения полей
  89. if ( $text_field ) :
  90. ?>
  91. <div class="text-field">
  92. <strong>Доля %: </strong>
  93. <?php echo $text_field; ?>
  94. </div>
  95. <?php endif;
  96. if ( $num_field ) : ?>
  97. <div class="number-field">
  98. <strong>Номер лота: </strong>
  99. <?php echo $num_field; ?>
  100. </div>
  101. <?php endif;
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement