产品页面的额外行/文本字段(WooCommerce)

时间:2017-03-07 作者:TimVanGorp

对于各位专家来说,这是一个简单的问题,但我一直在搜索,找不到准确的工作指南。所以我非常希望有人知道处理这件事的最佳方法:

我想为每个产品(它们是可变产品,不确定这是否重要)添加一个唯一的文本字段,我可以在产品的后端输入文本。此文本将显示在选项卡上方的前端,最好显示在自己创建的行中。

这应该能够完成这些功能。我的孩子主题的php。我已经收集了这些信息来启用后端的文本字段,但它似乎对我不起作用(可能是因为使用了可变产品或最新的Wordpress版本):

// 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_save( $post_id ){

    // Textarea
    $woocommerce_textarea = $_POST[\'_textarea\'];
    if( !empty( $woocommerce_textarea ) )
        update_post_meta( $post_id, \'_textarea\', esc_html( $woocommerce_textarea ) );

}

1 个回复
SO网友:Faysal Mahamud

问题不在于可变产品。这是一个单独的选项卡。因此,如果要在“常规”选项卡中添加自定义字段,它不会影响“变量”选项卡或任何其他选项卡。

但是,我将为您提供更新的产品代码,以添加自定义字段。我正在显示常规数据、变量数据和自定义选项卡字段。

如果要使用函数。php,将我的代码放入函数中。php。

Theme function

    //function.php
//General Fields
add_action( \'woocommerce_product_options_general_product_data\', \'woo_add_custom_general_fields\',10,3 );
add_action( \'woocommerce_process_product_meta\', \'woo_add_custom_general_fields_save\',10,2 );
//variable data
add_action(\'woocommerce_product_after_variable_attributes\', \'variation_settings_fields\', 10, 3);
add_action(\'woocommerce_save_product_variation\',\'save_variation_settings_fields\',10, 2);

//data tab
add_filter( \'woocommerce_product_data_tabs\', \'add_my_custom_product_data_tab\' , 99 , 1 );
add_action( \'woocommerce_product_data_panels\', \'add_my_custom_product_data_fields\',10,3 );
add_action( \'woocommerce_process_product_meta\',\'woocommerce_process_product_meta_fields_save\',10,2 );
function woo_add_custom_general_fields() {

global $woocommerce, $post;
woocommerce_wp_text_input(
  array(
    \'id\'          => \'_text_field\',
    \'label\'       => __( \'My Text Field\', \'woocommerce\' ),
    \'placeholder\' => \'http://\',
    \'desc_tip\'    => \'true\',
    \'value\'       => get_post_meta( $post->ID, \'_text_field\', true ),
    \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' )
  )
);
// Number Field
    woocommerce_wp_text_input(
      array(
        \'id\'          => \'_number_field[\' . $post->ID . \']\',
        \'label\'       => __( \'My Number Field\', \'woocommerce\' ),
        \'desc_tip\'    => \'true\',
        \'description\' => __( \'Enter the custom number here.\', \'woocommerce\' ),
        \'value\'       => get_post_meta( $post->ID, \'_number_field\', true ),
        \'custom_attributes\' => array(
                \'step\'  => \'any\',
                \'min\' => \'0\'
              )
      )
    );
    // Textarea
    woocommerce_wp_textarea_input(
      array(
        \'id\'          => \'_textarea[\' . $post->ID . \']\',
        \'label\'       => __( \'My Textarea\', \'woocommerce\' ),
        \'placeholder\' => \'\',
        \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
        \'value\'       => get_post_meta( $post->ID, \'_textarea\', true ),
      )
    );
    // Select
    woocommerce_wp_select(
      array(
      \'id\'          => \'_select[\' . $post->ID . \']\',
      \'label\'       => __( \'My Select Field\', \'woocommerce\' ),
      \'description\' => __( \'Choose a value.\', \'woocommerce\' ),
      \'value\'       => get_post_meta( $post->ID, \'_select\', true ),
      \'options\' => array(
        \'one\'   => __( \'Option 1\', \'woocommerce\' ),
        \'two\'   => __( \'Option 2\', \'woocommerce\' ),
        \'three\' => __( \'Option 3\', \'woocommerce\' )
        )
      )
    );
    // Checkbox
    woocommerce_wp_checkbox(
      array(
        \'id\'            => \'_checkbox[\' . $post->ID . \']\',
        \'label\'         => __(\'My Checkbox Field\', \'woocommerce\' ),
        \'description\'   => __( \'Check me!\', \'woocommerce\' ),
        \'value\'         => get_post_meta( $post->ID, \'_checkbox\', true ),
        )
    );
    // Hidden field
    woocommerce_wp_hidden_input(
      array(
        \'id\'    => \'_hidden_field[\' . $post->ID . \']\',
        \'value\' => \'hidden_value\'
        )
    );
}

function woo_add_custom_general_fields_save( $post_id ){

// Text Field
$woocommerce_text_field = $_POST[\'_text_field\'];
if( !empty( $woocommerce_text_field ) ){
  update_post_meta( $post_id, \'_text_field\', esc_attr( $woocommerce_text_field ) );
}
  //Number Field
$number_field = $_POST[\'_number_field\'][ $post_id ];
if( ! empty( $number_field ) ) {
  update_post_meta( $post_id, \'_number_field\', esc_attr( $number_field ) );
}
// Textarea
$textarea = $_POST[\'_textarea\'][ $post_id ];
if( ! empty( $textarea ) ) {
  update_post_meta( $post_id, \'_textarea\', esc_attr( $textarea ) );
}

// Select
$select = $_POST[\'_select\'][ $post_id ];
if( ! empty( $select ) ) {
  update_post_meta( $post_id, \'_select\', esc_attr( $select ) );
}

// Checkbox
$checkbox = isset( $_POST[\'_checkbox\'][ $post_id ] ) ? \'yes\' : \'no\';
update_post_meta( $post_id, \'_checkbox\', $checkbox );

// Hidden field
$hidden = $_POST[\'_hidden_field\'][ $post_id ];
if( ! empty( $hidden ) ) {
  update_post_meta( $post_id, \'_hidden_field\', esc_attr( $hidden ) );
}

}

function variation_settings_fields($loop, $variation_data, $variation){
  // Text Field
    woocommerce_wp_text_input(
      array(
        \'id\'          => \'statement[\' . $variation->ID . \']\',
        \'label\'       => __( \'Statement Cost\', \'woocommerce\' ),
        \'placeholder\' => \'Statement Cost\',
        \'desc_tip\'    => \'true\',
        \'description\' => __( \'Statement Cost.\', \'woocommerce\' ),
        \'value\'       => get_post_meta( $variation->ID, \'statement\', true )
      )
    );
    // Number Field
    woocommerce_wp_text_input(
      array(
        \'id\'          => \'_number_field[\' . $variation->ID . \']\',
        \'label\'       => __( \'My Number Field\', \'woocommerce\' ),
        \'desc_tip\'    => \'true\',
        \'description\' => __( \'Enter the custom number here.\', \'woocommerce\' ),
        \'value\'       => get_post_meta( $variation->ID, \'_number_field\', true ),
        \'custom_attributes\' => array(
                \'step\'  => \'any\',
                \'min\' => \'0\'
              )
      )
    );
    // Textarea
    woocommerce_wp_textarea_input(
      array(
        \'id\'          => \'_textarea[\' . $variation->ID . \']\',
        \'label\'       => __( \'My Textarea\', \'woocommerce\' ),
        \'placeholder\' => \'\',
        \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
        \'value\'       => get_post_meta( $variation->ID, \'_textarea\', true ),
      )
    );
    // Select
    woocommerce_wp_select(
      array(
      \'id\'          => \'_select[\' . $variation->ID . \']\',
      \'label\'       => __( \'My Select Field\', \'woocommerce\' ),
      \'description\' => __( \'Choose a value.\', \'woocommerce\' ),
      \'value\'       => get_post_meta( $variation->ID, \'_select\', true ),
      \'options\' => array(
        \'one\'   => __( \'Option 1\', \'woocommerce\' ),
        \'two\'   => __( \'Option 2\', \'woocommerce\' ),
        \'three\' => __( \'Option 3\', \'woocommerce\' )
        )
      )
    );
    // Checkbox
    woocommerce_wp_checkbox(
      array(
        \'id\'            => \'_checkbox[\' . $variation->ID . \']\',
        \'label\'         => __(\'My Checkbox Field\', \'woocommerce\' ),
        \'description\'   => __( \'Check me!\', \'woocommerce\' ),
        \'value\'         => get_post_meta( $variation->ID, \'_checkbox\', true ),
        )
    );
    // Hidden field
    woocommerce_wp_hidden_input(
      array(
        \'id\'    => \'_hidden_field[\' . $variation->ID . \']\',
        \'value\' => \'hidden_value\'
        )
    );
}

function save_variation_settings_fields($post_id){
  // Text Field
  $text_field = $_POST[\'statement\'][ $post_id ];
  if( ! empty( $text_field ) ) {
    update_post_meta( $post_id, \'statement\', esc_attr( $text_field ) );
  }
  // Number Field
  $number_field = $_POST[\'_number_field\'][ $post_id ];
  if( ! empty( $number_field ) ) {
    update_post_meta( $post_id, \'_number_field\', esc_attr( $number_field ) );
  }
  // Textarea
  $textarea = $_POST[\'_textarea\'][ $post_id ];
  if( ! empty( $textarea ) ) {
    update_post_meta( $post_id, \'_textarea\', esc_attr( $textarea ) );
  }

  // Select
  $select = $_POST[\'_select\'][ $post_id ];
  if( ! empty( $select ) ) {
    update_post_meta( $post_id, \'_select\', esc_attr( $select ) );
  }

  // Checkbox
  $checkbox = isset( $_POST[\'_checkbox\'][ $post_id ] ) ? \'yes\' : \'no\';
  update_post_meta( $post_id, \'_checkbox\', $checkbox );

  // Hidden field
  $hidden = $_POST[\'_hidden_field\'][ $post_id ];
  if( ! empty( $hidden ) ) {
    update_post_meta( $post_id, \'_hidden_field\', esc_attr( $hidden ) );
  }
}

function add_my_custom_product_data_tab( $product_data_tabs ) {
  $product_data_tabs[\'my-custom-tab\'] = array(
      \'label\' => __( \'My Custom Tab\', \'my_text_domain\' ),
      \'target\' => \'my_custom_product_data\',
  );
  return $product_data_tabs;
}
function add_my_custom_product_data_fields() {
  global $woocommerce, $post;
    echo \'<div id="my_custom_product_data" class="panel woocommerce_options_panel">\';
      woocommerce_wp_checkbox( array(
          \'id\'            => \'_my_custom_field\',
          \'wrapper_class\' => \'show_if_simple\',
          \'label\'         => __( \'My Custom Field Label\', \'my_text_domain\' ),
          \'description\'   => __( \'My Custom Field Description\', \'my_text_domain\' ),
          \'default\'       => \'0\',
          \'desc_tip\'      => false,
      ) );
    echo \'</div>\';

}

function woocommerce_process_product_meta_fields_save( $post_id ){
$woo_checkbox = isset( $_POST[\'_my_custom_field\'] ) ? \'yes\' : \'no\';
  update_post_meta( $post_id, \'_my_custom_field\', $woo_checkbox );
}
如果您使用自定义插件。Plugin Code//创建类并放置我的代码

    <?php
    /**
     * Add Cutom Fields Woocommerce product type functionality
     */
    class AddCutomFields
    {
    public static function init(){
        //General Fields
        add_action( \'woocommerce_product_options_general_product_data\',array(\'AddCutomFields\', \'woo_add_custom_general_fields\'),10,3 );
        add_action( \'woocommerce_process_product_meta\', array(\'AddCutomFields\', \'woo_add_custom_general_fields_save\'),10,2 );

        //variable data
        add_action(\'woocommerce_product_after_variable_attributes\', array(\'AddCutomFields\',\'variation_settings_fields\'), 10, 3);
        add_action(\'woocommerce_save_product_variation\', array(\'AddCutomFields\',\'save_variation_settings_fields\' ),10, 2);

        //data tab
        add_filter( \'woocommerce_product_data_tabs\', array(\'AddCutomFields\',\'add_my_custom_product_data_tab\') , 99 , 1 );
        add_action( \'woocommerce_product_data_panels\', array(\'AddCutomFields\',\'add_my_custom_product_data_fields\'),10,3 );
        add_action( \'woocommerce_process_product_meta\',  array(\'AddCutomFields\',\'woocommerce_process_product_meta_fields_save\'),10,2 );
    }

    public static function woo_add_custom_general_fields() {

      global $woocommerce, $post;
      woocommerce_wp_text_input(
        array(
          \'id\'          => \'_text_field\',
          \'label\'       => __( \'My Text Field\', \'woocommerce\' ),
          \'placeholder\' => \'http://\',
          \'desc_tip\'    => \'true\',
          \'value\'       => get_post_meta( $post->ID, \'_text_field\', true ),
          \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' )
        )
      );
      // Number Field
          woocommerce_wp_text_input(
            array(
              \'id\'          => \'_number_field[\' . $post->ID . \']\',
              \'label\'       => __( \'My Number Field\', \'woocommerce\' ),
              \'desc_tip\'    => \'true\',
              \'description\' => __( \'Enter the custom number here.\', \'woocommerce\' ),
              \'value\'       => get_post_meta( $post->ID, \'_number_field\', true ),
              \'custom_attributes\' => array(
                      \'step\'  => \'any\',
                      \'min\' => \'0\'
                    )
            )
          );
          // Textarea
          woocommerce_wp_textarea_input(
            array(
              \'id\'          => \'_textarea[\' . $post->ID . \']\',
              \'label\'       => __( \'My Textarea\', \'woocommerce\' ),
              \'placeholder\' => \'\',
              \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
              \'value\'       => get_post_meta( $post->ID, \'_textarea\', true ),
            )
          );
          // Select
          woocommerce_wp_select(
            array(
            \'id\'          => \'_select[\' . $post->ID . \']\',
            \'label\'       => __( \'My Select Field\', \'woocommerce\' ),
            \'description\' => __( \'Choose a value.\', \'woocommerce\' ),
            \'value\'       => get_post_meta( $post->ID, \'_select\', true ),
            \'options\' => array(
              \'one\'   => __( \'Option 1\', \'woocommerce\' ),
              \'two\'   => __( \'Option 2\', \'woocommerce\' ),
              \'three\' => __( \'Option 3\', \'woocommerce\' )
              )
            )
          );
          // Checkbox
          woocommerce_wp_checkbox(
            array(
              \'id\'            => \'_checkbox[\' . $post->ID . \']\',
              \'label\'         => __(\'My Checkbox Field\', \'woocommerce\' ),
              \'description\'   => __( \'Check me!\', \'woocommerce\' ),
              \'value\'         => get_post_meta( $post->ID, \'_checkbox\', true ),
              )
          );
          // Hidden field
          woocommerce_wp_hidden_input(
            array(
              \'id\'    => \'_hidden_field[\' . $post->ID . \']\',
              \'value\' => \'hidden_value\'
              )
          );
    }

    public static function woo_add_custom_general_fields_save( $post_id ){

      // Text Field
      $woocommerce_text_field = $_POST[\'_text_field\'];
      if( !empty( $woocommerce_text_field ) ){
        update_post_meta( $post_id, \'_text_field\', esc_attr( $woocommerce_text_field ) );
      }
        //Number Field
      $number_field = $_POST[\'_number_field\'][ $post_id ];
      if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, \'_number_field\', esc_attr( $number_field ) );
      }
      // Textarea
      $textarea = $_POST[\'_textarea\'][ $post_id ];
      if( ! empty( $textarea ) ) {
        update_post_meta( $post_id, \'_textarea\', esc_attr( $textarea ) );
      }

      // Select
      $select = $_POST[\'_select\'][ $post_id ];
      if( ! empty( $select ) ) {
        update_post_meta( $post_id, \'_select\', esc_attr( $select ) );
      }

      // Checkbox
      $checkbox = isset( $_POST[\'_checkbox\'][ $post_id ] ) ? \'yes\' : \'no\';
      update_post_meta( $post_id, \'_checkbox\', $checkbox );

      // Hidden field
      $hidden = $_POST[\'_hidden_field\'][ $post_id ];
      if( ! empty( $hidden ) ) {
        update_post_meta( $post_id, \'_hidden_field\', esc_attr( $hidden ) );
      }

    }

    public static function variation_settings_fields($loop, $variation_data, $variation){
        // Text Field
          woocommerce_wp_text_input(
            array(
              \'id\'          => \'statement[\' . $variation->ID . \']\',
              \'label\'       => __( \'Statement Cost\', \'woocommerce\' ),
              \'placeholder\' => \'Statement Cost\',
              \'desc_tip\'    => \'true\',
              \'description\' => __( \'Statement Cost.\', \'woocommerce\' ),
              \'value\'       => get_post_meta( $variation->ID, \'statement\', true )
            )
          );
          // Number Field
          woocommerce_wp_text_input(
            array(
              \'id\'          => \'_number_field[\' . $variation->ID . \']\',
              \'label\'       => __( \'My Number Field\', \'woocommerce\' ),
              \'desc_tip\'    => \'true\',
              \'description\' => __( \'Enter the custom number here.\', \'woocommerce\' ),
              \'value\'       => get_post_meta( $variation->ID, \'_number_field\', true ),
              \'custom_attributes\' => array(
                      \'step\'  => \'any\',
                      \'min\' => \'0\'
                    )
            )
          );
          // Textarea
          woocommerce_wp_textarea_input(
            array(
              \'id\'          => \'_textarea[\' . $variation->ID . \']\',
              \'label\'       => __( \'My Textarea\', \'woocommerce\' ),
              \'placeholder\' => \'\',
              \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
              \'value\'       => get_post_meta( $variation->ID, \'_textarea\', true ),
            )
          );
          // Select
          woocommerce_wp_select(
            array(
            \'id\'          => \'_select[\' . $variation->ID . \']\',
            \'label\'       => __( \'My Select Field\', \'woocommerce\' ),
            \'description\' => __( \'Choose a value.\', \'woocommerce\' ),
            \'value\'       => get_post_meta( $variation->ID, \'_select\', true ),
            \'options\' => array(
              \'one\'   => __( \'Option 1\', \'woocommerce\' ),
              \'two\'   => __( \'Option 2\', \'woocommerce\' ),
              \'three\' => __( \'Option 3\', \'woocommerce\' )
              )
            )
          );
          // Checkbox
          woocommerce_wp_checkbox(
            array(
              \'id\'            => \'_checkbox[\' . $variation->ID . \']\',
              \'label\'         => __(\'My Checkbox Field\', \'woocommerce\' ),
              \'description\'   => __( \'Check me!\', \'woocommerce\' ),
              \'value\'         => get_post_meta( $variation->ID, \'_checkbox\', true ),
              )
          );
          // Hidden field
          woocommerce_wp_hidden_input(
            array(
              \'id\'    => \'_hidden_field[\' . $variation->ID . \']\',
              \'value\' => \'hidden_value\'
              )
          );
    }

    public static function save_variation_settings_fields($post_id){
        // Text Field
        $text_field = $_POST[\'statement\'][ $post_id ];
        if( ! empty( $text_field ) ) {
          update_post_meta( $post_id, \'statement\', esc_attr( $text_field ) );
        }
        // Number Field
        $number_field = $_POST[\'_number_field\'][ $post_id ];
        if( ! empty( $number_field ) ) {
          update_post_meta( $post_id, \'_number_field\', esc_attr( $number_field ) );
        }
        // Textarea
        $textarea = $_POST[\'_textarea\'][ $post_id ];
        if( ! empty( $textarea ) ) {
          update_post_meta( $post_id, \'_textarea\', esc_attr( $textarea ) );
        }

        // Select
        $select = $_POST[\'_select\'][ $post_id ];
        if( ! empty( $select ) ) {
          update_post_meta( $post_id, \'_select\', esc_attr( $select ) );
        }

        // Checkbox
        $checkbox = isset( $_POST[\'_checkbox\'][ $post_id ] ) ? \'yes\' : \'no\';
        update_post_meta( $post_id, \'_checkbox\', $checkbox );

        // Hidden field
        $hidden = $_POST[\'_hidden_field\'][ $post_id ];
        if( ! empty( $hidden ) ) {
          update_post_meta( $post_id, \'_hidden_field\', esc_attr( $hidden ) );
        }
    }

    public static function add_my_custom_product_data_tab( $product_data_tabs ) {
        $product_data_tabs[\'my-custom-tab\'] = array(
            \'label\' => __( \'My Custom Tab\', \'my_text_domain\' ),
            \'target\' => \'my_custom_product_data\',
        );
        return $product_data_tabs;
    }
    public static function add_my_custom_product_data_fields() {
        global $woocommerce, $post;
          echo \'<div id="my_custom_product_data" class="panel woocommerce_options_panel">\';
            woocommerce_wp_checkbox( array(
                \'id\'            => \'_my_custom_field\',
                \'wrapper_class\' => \'show_if_simple\',
                \'label\'         => __( \'My Custom Field Label\', \'my_text_domain\' ),
                \'description\'   => __( \'My Custom Field Description\', \'my_text_domain\' ),
                \'default\'       => \'0\',
                \'desc_tip\'      => false,
            ) );
          echo \'</div>\';

    }

    public static function woocommerce_process_product_meta_fields_save( $post_id ){
      $woo_checkbox = isset( $_POST[\'_my_custom_field\'] ) ? \'yes\' : \'no\';
        update_post_meta( $post_id, \'_my_custom_field\', $woo_checkbox );
    }

}
要获取这些值,我们只需要使用流行的get\\u post\\u meta()函数。

<?php
// Display Custom Field Value
echo get_post_meta( $post->ID, \'my-field-slug\', true );
ex. echo get_post_meta( $post->ID, \'_textarea\', true );
// You can also use
echo get_post_meta( get_the_ID(), \'my-field-slug\', true );
ex. echo get_post_meta( get_the_ID(), \'_textarea\', true );

// variable data 
ex. echo get_post_meta( $variation->ID, \'_textarea\', true );
?>
代码经过测试,运行良好。如果你在任何地方遇到麻烦,请不要犹豫,再问任何问题。如果您需要帮助为shipping选项卡或任何其他选项卡添加自定义字段,请告诉我,我将在将来更新代码。

注意。插件和函数的代码。php是相同的。您可以使用任一函数。php或自定义插件代码。

相关推荐