M
M
Maxim2018-02-05 13:59:38
WordPress
Maxim, 2018-02-05 13:59:38

Adding Custom Wordpress/Woocommerce Fields?

Added a field before the price that would look like
5a783706966cd411952063.jpeg
functions.php

Created a dropdown list
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_select( array(
       'id'      => '_select',
       'label'   => 'Значение перед ценой',
       'options' => array(
          ''   => __( 'нет', 'woocommerce' ),
          'от'   => __( 'от', 'woocommerce' ),
       ),
    ) );
  echo '</div>';
}

5a78378272a70230111913.jpeg
Save the value
add_action( 'woocommerce_process_product_meta', 'art_woo_custom_fields_save', 10 );
function art_woo_custom_fields_save( $post_id ) {
   $woocommerce_select = $_POST['_select'];
   if ( $woocommerce_select ) {
      update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
   }else {
      delete_post_meta( $post_id, '_select');
   }
  }

Display value before price
add_action( 'woocommerce_get_price_suffix', 'art_get_select_before_add_card' );
function art_get_select_before_add_card() {
  global $post;
  echo '<span class="price_suffix">';
  echo get_post_meta( $post->ID, '_select', true );
  echo '</span>';
}


Displays everything correctly, but in the admin panel in the item item for some reason the value is duplicated
5a7837ff0b356152271694.jpeg
where to look for an error?
PS

если в выпадающем списке ставлю значения
...
'от1'   => __( 'от2', 'woocommerce' ),
...

то получаем "от1от1цена"

Thank you for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Dmitriev, 2018-02-16
@Dmitryev

We display the value before the price.
Here in one echo will not work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question