【问题标题】:Add custom fields to WooComerce product setting pages in the shipping tab将自定义字段添加到运输选项卡中的 WooCommerce 产品设置页面
【发布时间】:2017-12-26 00:05:35
【问题描述】:

是否可以在后端的 WooCommerce 产品页面运输选项卡设置中添加一些额外的字段,因为我需要添加 12 个自定义字段。

我试图找到一些相关的钩子但没有成功。我发现的唯一方法是over attributes,但这不是一个方便的解决方案……

如何将自定义字段添加到配送选项卡中的 WooComerce 产品设置页面?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce product shipping


    【解决方案1】:

    这是可能的,你会得到这个(这里我设置了自定义文本字段):

    代码如下:

    // Add custom fields to product shipping tab
    add_action( 'woocommerce_product_options_shipping', 'add_custom_shipping_option_to_products');
    function add_custom_shipping_option_to_products(){
        global $post, $product;
    
    
        echo '</div><div class="options_group">'; // New option group
    
        woocommerce_wp_text_input( array(
            'id'          => '_custom_text_field1',
            'label'       => __( 'My Text Field one', 'woocommerce' ),
            'placeholder' => 'something',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $post->ID, '_custom_meta_field1', true ),
        ) );
    
        woocommerce_wp_text_input( array(
            'id'          => '_custom_text_field2',
            'label'       => __( 'My Text Field two', 'woocommerce' ),
            'placeholder' => 'something',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $post->ID, '_custom_meta_field2', true ),
        ) );
    }
    
    // Save the custom fields values as meta data
    add_action( 'woocommerce_process_product_meta', 'save_custom_shipping_option_to_products' );
    function save_custom_shipping_option_to_products( $post_id ){
    
        $custom_text_field1 = $_POST['_custom_text_field1'];
        if( isset( $custom_text_field1 ) )
            update_post_meta( $post_id, '_custom_meta_field1', esc_attr( $custom_text_field1 ) );
    
        $custom_text_field2 = $_POST['_custom_text_field2'];
        if( isset( $custom_text_field2 ) )
            update_post_meta( $post_id, '_custom_meta_field2', esc_attr( $custom_text_field2 ) );
    }
    

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

    此代码在 WooCommerce 3+ 上经过测试并且有效

    【讨论】:

    • 嗨,谢谢,它也可以在 compare 等插件中自动工作?
    • 嗨,如何将一个字段划分为更像已经存在的维度?是否可以编辑现有字段名称的文本?
    • 那么如何设置数字以十进制形式存储?
    • 以及如何在产品页面的附加信息选项卡中以正确的单位显示它们?
    • 您好,我知道这部分有效,谢谢,我已经实现了。好的,那么我将提出一个新问题,谢谢,但请告诉我如何修改一个与现有维度字段相似的字段?
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2021-02-03
    • 2021-05-02
    • 1970-01-01
    • 2018-09-22
    • 2014-07-12
    • 2021-01-29
    • 2023-04-08
    相关资源
    最近更新 更多