【问题标题】:Add a product custom field to Admin product bulk edit form in WooCommerce在 WooCommerce 中向管理员产品批量编辑表单添加产品自定义字段
【发布时间】:2017-09-05 09:27:04
【问题描述】:

我在我的 WooCommerce 产品中添加了一个自定义字段,例如这个问题/答案:
Display a custom product field before short description in WooCommerce

是否可以将此自定义字段添加到产品批量编辑特殊页面(可从管理产品列表页面访问)

【问题讨论】:

  • 质量是什么意思?
  • @Reigel 我选择了几个产品,然后从水平菜单中选择编辑。你看: 1. ibb.co/jqd0wv - 选择并点击“编辑”按钮。 2 - 我可以一次编辑多个产品 - ibb.co/cT0KpF
  • 在谷歌上搜索wordpress bulk action tut

标签: php wordpress woocommerce custom-fields product


【解决方案1】:

是的,可以为您的自定义字段批量编辑产品'_text_field' (如您链接的问题/答案中)

您可以在编辑页面的开头或结尾添加此自定义字段。

  • 一开始你将使用这个钩子:woocommerce_product_bulk_edit_start
  • 最后这个:woocommerce_product_bulk_edit_end

代码(这里是自定义字段开头)

// Add a custom field to product bulk edit special page
add_action( 'woocommerce_product_bulk_edit_start', 'custom_field_product_bulk_edit', 10, 0 );
function custom_field_product_bulk_edit() {
    ?>
        <div class="inline-edit-group">
            <label class="alignleft">
                <span class="title"><?php _e('T. dostawy', 'woocommerce'); ?></span>
                <span class="input-text-wrap">
                    <select class="change_t_dostawy change_to" name="change_t_dostawy">
                    <?php
                        $options = array(
                            ''  => __( '— No change —', 'woocommerce' ),
                            '1' => __( 'Change to:', 'woocommerce' ),
                        );
                        foreach ( $options as $key => $value ) {
                            echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>';
                        }
                    ?>
                    </select>
                </span>
            </label>
            <label class="change-input">
                <input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( 'Enter Termin dostawy', 'woocommerce' ); ?>" value="" />
            </label>
        </div>
    <?php
}

// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
    if ( $product->is_type('simple') || $product->is_type('external') ){
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        if ( isset( $_REQUEST['_t_dostawy'] ) )
            update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['_t_dostawy'] ) );
    }
}

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

此代码已经过测试并且可以工作。你会得到这个:

【讨论】:

  • @LoicTheAztec 如果您想按百分比更新数字(如自定义价格字段)或增加固定值(因此使用当前值并添加),这将如何改变? - 如果有多个选项,我怎么能看到选择了哪个选项? $_REQUEST['fieldname'] 中是否有任何内容可以告诉我选择了哪个选项?
【解决方案2】:

是的,这是可能的,并且没有任何钩子和代码:

  • 安装插件 WooCommerce 批量编辑器 (WOOBE):https://wordpress.org/plugins/woo-bulk-editor/
  • 转到“元字段”选项卡并在其中添加您的元键,然后按“保存”按钮
  • 在“设置”选项卡中使用该新元键激活列
  • 使用“批量编辑”选项卡进行操作

就是这样:)

附言文档:https://bulk-editor.com/document/meta-fields/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2015-07-12
    • 2019-01-13
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多