【问题标题】:Displaying custom fields values under SKU in single product pages在单个产品页面中显示 SKU 下的自定义字段值
【发布时间】:2016-12-23 22:39:21
【问题描述】:

我正在自定义 WooCommerce,我想在产品页面中添加和显示自定义文本(条件和品牌)。

该职位位于“有货”或“SKU”元数据下。我已经设法创建和保存自定义字段,但如何将这些元值打印到产品页面。

请帮忙!

这是我在 functions.php 中的代码:

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Text Field
function woo_add_custom_general_fields() {
  global $woocommerce, $post;
  echo '<div class="options_group">';
  woocommerce_wp_text_input( 
    array( 

        'id'          => '_conditions', 
        'label'       => __( 'Conditions', 'woocommerce' ), 
        'placeholder' => 'i.e: brand-new; refurbished; defected...',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the conditions of the products here.', 'woocommerce' ) 
    )
);
  echo '</div>';
  woocommerce_wp_text_input( 
    array( 

        'id'          => '_bands', 
        'label'       => __( 'Brands', 'woocommerce' ), 
        'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
        'desc_tip'    => 'true',
        'description' => __( 'Enter names of the Brands of the products if any.', 'woocommerce' ) 
    )
);
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

谢谢

【问题讨论】:

    标签: php wordpress woocommerce custom-fields product


    【解决方案1】:

    您问题中提供的代码不完整,应该是这样的:

    // Enabling and Displaying Fields in backend
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    function woo_add_custom_general_fields() {
        global $woocommerce, $post;
    
        echo '<div class="options_group">';
    
        woocommerce_wp_text_input( array( // Text Field type
            'id'          => '_conditions', 
            'label'       => __( 'Conditions', 'woocommerce' ), 
            'placeholder' => 'i.e: brand-new; refurbished; defected...',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the conditions of the products here.', 'woocommerce' ) 
        ) );
    
        woocommerce_wp_text_input( array( // Text Field type
            'id'          => '_brands', // ===> NOT '_bands'
            'label'       => __( 'Brands', 'woocommerce' ), 
            'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
            'desc_tip'    => 'true',
            'description' => __( 'Enter names of the Brands of the products if any.', 'woocommerce' ) 
        ));
    
        echo '</div>'; // Closing </div> tag HERE
    }
    
    // Save Fields values to database when submitted (Backend)
    add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
    function woo_save_custom_general_fields( $post_id ){
    
        // Saving "Conditions" field key/value
        $conditions_field = $_POST['_conditions'];
        if( !empty( $conditions_field ) )
            update_post_meta( $post_id, '_conditions', esc_attr( $conditions_field ) );
    
        // Saving "Brands" field key/value
        $brands_field = $_POST['_brands'];
        if( !empty( $brands_field ) )
            update_post_meta( $post_id, '_brands', esc_attr( $brands_field ) );
    }
    

    现在,要在您的产品页面中显示此元数据值,您将在挂钩函数中使用 get_post_meta() 函数。在下面,您将看到 woocommerce_single_product_summary 挂钩中的所有挂钩模板及其优先级(显示顺序):

    /**
         * woocommerce_single_product_summary hook
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
     */
    

    “库存”或“SKU”数据由优先级为 40 的 woocommerce_template_single_meta 显示。现在您需要在后面显示您的海关字段值。然后,您可以为此目的选择 45 的优先级。

    以下代码将在“SKU”下的产品页面中输出您的自定义字段值:

    add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
    function woo_display_custom_general_fields_values() {
        global $product;
    
        echo '<p class="custom-conditions">Conditions: ' . get_post_meta( $product->id, '_conditions', true ) . '</p>';
        echo '<p class="custom-brands">Brands: ' . get_post_meta( $product->id, '_brands', true ) . '</p>';
    }
    

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

    此代码已经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 2021-06-24
      • 2021-03-04
      • 2020-07-14
      • 2017-11-16
      • 1970-01-01
      相关资源
      最近更新 更多