将产品自定义字段添加到 WooCommerce 中的管理产品批量编辑表单

int*_*law 2 php wordpress product custom-fields woocommerce

我在我的 WooCommerce 产品中添加了一个自定义字段,如这个问题/答案:
在 WooCommerce 中的简短描述之前显示自定义产品字段

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

Loi*_*tec 6

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

\n\n

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

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

代码(自定义字段位于此处的开头)

\n\n
// Add a custom field to product bulk edit special page\nadd_action( \'woocommerce_product_bulk_edit_start\', \'custom_field_product_bulk_edit\', 10, 0 );\nfunction custom_field_product_bulk_edit() {\n    ?>\n        <div class="inline-edit-group">\n            <label class="alignleft">\n                <span class="title"><?php _e(\'T. dostawy\', \'woocommerce\'); ?></span>\n                <span class="input-text-wrap">\n                    <select class="change_t_dostawy change_to" name="change_t_dostawy">\n                    <?php\n                        $options = array(\n                            \'\'  => __( \'\xe2\x80\x94 No change \xe2\x80\x94\', \'woocommerce\' ),\n                            \'1\' => __( \'Change to:\', \'woocommerce\' ),\n                        );\n                        foreach ( $options as $key => $value ) {\n                            echo \'<option value="\' . esc_attr( $key ) . \'">\' . $value . \'</option>\';\n                        }\n                    ?>\n                    </select>\n                </span>\n            </label>\n            <label class="change-input">\n                <input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( \'Enter Termin dostawy\', \'woocommerce\' ); ?>" value="" />\n            </label>\n        </div>\n    <?php\n}\n\n// Save the custom fields data when submitted for product bulk edit\nadd_action(\'woocommerce_product_bulk_edit_save\', \'save_custom_field_product_bulk_edit\', 10, 1);\nfunction save_custom_field_product_bulk_edit( $product ){\n    if ( $product->is_type(\'simple\') || $product->is_type(\'external\') ){\n        $product_id = method_exists( $product, \'get_id\' ) ? $product->get_id() : $product->id;\n\n        if ( isset( $_REQUEST[\'_t_dostawy\'] ) )\n            update_post_meta( $product_id, \'_text_field\', sanitize_text_field( $_REQUEST[\'_t_dostawy\'] ) );\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n

在此输入图像描述

\n