繁体   English   中英

结果显示在前端woocommerce

[英]results display on front end woocommerce

在 WooCommerce 我试图在前端显示一些自定义数据:

  1. 自定义运输成本选项卡。 但是在前端产品展示页面上没有获取数据。
  2. 在常规产品设置选项卡下添加的自定义字段很少,但可以在前端产品显示页面上获取。

第一个代码 *( https://stackoverflow.com/a/39010336/8523129

第二个代码:

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');


function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';

    // WooCommerce custom dropdown Select
    woocommerce_wp_select(
       array(
        'id'      => '_select',
        'label'   => __( 'WooCommerce Custom Select Field', 'woocommerce' ),
        'options' => array(
            'one'   => __( 'First Dropdown', 'woocommerce' ),
            'two'   => __( 'Second Dropdown', 'woocommerce' ),
            'three' => __( 'Third Dropdown', 'woocommerce' )
            )
        )
       );
       //WooCommerce Custom Checkbox
       woocommerce_wp_checkbox(
       array(
        'id'            => '_checkbox',
        'wrapper_class' => 'show_if_simple',
        'label'         => __('WooCommerce Custom Checkbox Field', 'woocommerce' )
            )
       );
       // WooCommerce Custom field Type
       ?>
       <p class="form-field custom_field_type">
        <label for="custom_field_type"><?php echo __( 'WooCommerce Custom Field Type', 'woocommerce' ); ?></label>
        <span class="wrap">
            <?php $custom_product_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?>
            <input placeholder="<?php _e( 'Field One', 'woocommerce' ); ?>" class="" type="number" name="_field_one" value="<?php echo $custom_product_field_type[0]; ?>" step="any" min="0" style="width: 80px;" />
            <input placeholder="<?php _e( 'Field Two', 'woocommerce' ); ?>" type="number" name="_field_two" value="<?php echo $custom_product_field_type[1]; ?>" step="any" min="0" style="width: 80px;" />
        </span>
       </p>
   <?php

   ?>
   <?php
       echo '</div>';

   }
   function woocommerce_product_custom_fields_save($post_id)
   {

       // WooCommerce custom dropdown Select
       $woocommerce_custom_product_select = $_POST['_select'];
       if (!empty($woocommerce_custom_product_select))
           update_post_meta($post_id, '_select', esc_attr($woocommerce_custom_product_select));

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

       // WooCommerce Custom field Type
       $custom_product_field_type = array(esc_attr($_POST['_field_one']), esc_attr($_POST['_field_two']));
       update_post_meta($post_id, '_custom_field_type', $custom_product_field_type);
   }
   // Custom Product  Variation Settings
   add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );
   function custom_load_variation_settings_products_fields( $variations ) {

    // duplicate the line for each field
    $variations['_select'] = get_post_meta( $variations[ 'variation_id' ], '_select', true );

    return $variations;
   }

我在显示页面上没有得到任何结果。

如何实现这一目标。

根据您的评论,要在单个产品页面上的自定义产品选项卡中获取和显示产品自定义字段中的数据,您将使用get_post_meta()函数:

// Add product custom product tab
add_filter( 'woocommerce_product_tabs', 'custom_product_tab' );
function custom_product_tab( $tabs ) {

    $tabs['custom_tab'] = array(
        'title'     => __( 'Custom Data', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'custom_data_product_tab_content'
    );

    return $tabs;
}

// The custom product tab content
function custom_data_product_tab_content(){
    global $post;

    // 1. Get the data
    $select = get_post_meta( $post->ID, '_select', true );
    $checkbox = get_post_meta( $post->ID, '_checkbox', true );
    $cf_type = get_post_meta( $post->ID, '_custom_field_type', true );

    // 2. Display the data
    $output = '<div class="custom-data">';

    if( ! empty( $select ) )
        $output .= '<p>'. __('Select field: ').'</span style="color:#96588a;">'.$select.'</span></p>';
    if( ! empty( $checkbox ) )
        $output .= '<p>'. __('Checkbox field: ').'</span style="color:#96588a;">'.$checkbox.'</span></p>';
    if( ! empty( $cf_type[0] ) )
        $output .= '<p>'. __('Number field 1: ').'</span style="color:#96588a;">'.$cf_type[0].'</span></p>';
    if( ! empty( $cf_type[1] ) )
        $output .= '<p>'. __('Number field 2: ').'</span style="color:#96588a;">'.$cf_type[1].'</span></p>';

    echo $output.'</div>';
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

测试和工作。 你会得到类似的东西如果不为空,你的数据将被获取)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM