• Resolved EbichuWashu

    (@ebichuwashu)


    Hello everybody!

    $filters[] = 'woocommerce_short_description';

    I just do not know how to work with my fields. I’ve tried everything I could think, but nothing works.

    Below are some of my fields:

    functions.php

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    
    function woo_add_custom_general_fields() {
    
    global $woocommerce, $post;
    echo '<div class="options_group">'; 
    
      // Select
    woocommerce_wp_select(
         array(
              'id' => '_product_type',
              'label' => __( 'Product type', 'woocommerce' ),
              'options' => array(
              		'Doujinshi' => __( 'Doujinshi', 'woocommerce' ),
              		'Manga' => __( 'Manga', 'woocommerce' ),
             		'Novel' => __( 'Novel', 'woocommerce' ),
              )
          )
    );
    woocommerce_wp_select(
         array(
              'id' => '_product_condition',
              'label' => __( 'Product condition', 'woocommerce' ),
              'options' => array(
              	'Used' => __( 'Used', 'woocommerce' ),
              	'New' => __( 'New', 'woocommerce' ),
              )
          )
    );
      echo '</div>';
    }
    
    function woo_add_custom_general_fields_save( $post_id ){
    
    // Select product type
    $woocommerce_select = $_POST['_product_type'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_product_type', esc_attr( $woocommerce_select ) );
    else
        update_post_meta( $post_id, '_product_type', '' );
    
    // Select product condiction
    $woocommerce_select = $_POST['_product_condition'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_product_condition', esc_attr( $woocommerce_select ) );
    else
        update_post_meta( $post_id, '_product_condition', '' );
    
    }

    short-description.php

    <?php
    $product_type = get_post_meta($post->ID, '_product_type', true);
    if( !empty( $product_type ) ){
      echo '<b>? Product type: </b>'.$product_type.'<br />';
    } ?>
    <?php
    $product_condition = get_post_meta($post->ID, '_product_condition', true);
    if( !empty( $product_condition ) ){
      echo '<b>? Product condition: </b>'.$product_condition.'<br />';
    } ?>

    And I would like to add them to:

    add_filter( 'c2c_linkify_text_filters', 'more_text_replacements' );
    function more_text_replacements( $filters ) {
        $filters[] = 'the_meta'; // Here you could put in the name of any filter you want
        return $filters;
    }

    I hope someone can help me! Any help is welcome! Thank you!

    https://www.ads-software.com/plugins/linkify-text/

Viewing 1 replies (of 1 total)
  • Thread Starter EbichuWashu

    (@ebichuwashu)

    Ok! I managed to solve my problem.

    I will post the solution to case other people have this problem:

    In short-description.php (theme), where is the code that makes my customs fields show, I had to apply filters (I’m not good with programming, so I do not have a very strong sense of syntax).

    Before:

    <?php
    $product_type = get_post_meta($post->ID, '_product_type', true);
    if( !empty( $product_type ) ){
      echo '<b>? Product type: </b>'.$product_type.'<br />';
    } ?>
    <?php
    $product_condition = get_post_meta($post->ID, '_product_condition', true);
    if( !empty( $product_condition ) ){
      echo '<b>? Product condition: </b>'.$product_condition.'<br />';
    } ?>

    After:

    <?php
    $product_type = apply_filters('custom_field_product_type', get_post_meta($post->ID, '_product_type', true));
    if( !empty( $product_type ) ){
      echo '<b>? Product type: </b>'.$product_type.'<br />';
    }
    ?>
    <?php
    $product_condition = apply_filters('custom_field_product_condition', get_post_meta($post->ID, '_product_condition', true));
    if( !empty( $product_condition ) ){
      echo '<b>? Product condition: </b>'.$product_condition.'<br />';
    } ?>

    Into the functions.php (theme):

    Example of code Linkify Text:

    // Enable text linkification for custom fields
    add_filter( 'c2c_linkify_text_filters', 'more_text_replacements' );
    function more_text_replacements( $filters ) {
        $filters[] = 'the_meta'; // Here you could put in the name of any filter you want
        return $filters;
    }

    With changes to custom fields work:

    // Enable text linkification for custom fields
    add_filter( 'c2c_linkify_text_filters', 'more_text_replacements' );
    function more_text_replacements( $filters ) {
        $filters['short_description'] = 'woocommerce_short_description'; //to work in the short-description of woocommerce single product
        $filters['product_type'] = 'custom_field_product_type';
        $filters['product_condition'] = 'custom_field_product_condition';
        return $filters;
    }

    I hope this can help others with similar problem!

    Cheers! ^-^

Viewing 1 replies (of 1 total)
  • The topic ‘How to add custom fields to $ filters[] (urgent)’ is closed to new replies.