首页 > 解决方案 > 在 woocommerce 产品的 quickedit 函数中添加我的自定义输入字段

问题描述

我正在尝试在 woo 商务产品的快速编辑功能中添加我的自定义输入字段。

我在我的产品中创建了一个自定义输入字段。如果我直接进入每个产品的编辑页面,我只能编辑和保存这个自定义输入字段。

创建自定义输入字段:

function cfwc_create_custom_field() {
    $args = array(
        'id'            => 'custom_product_code',
        'label'         => __( 'Product Code:', 'cfwc' ),
        'class'                 => 'cfwc-custom-field',
        'desc_tip'      => false,
        'description'   => __( '', 'ctwc' ),
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_field' );

保存自定义输入字段:

function cfwc_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['custom_product_code'] ) ? $_POST['custom_product_code'] : '';
    $product->update_meta_data( 'custom_product_code', sanitize_text_field( $title ) );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );

如果我在编辑页面中编辑产品,此代码可以正常工作。我想要的是在我的产品的快速编辑功能中编辑这个自定义输入字段,这样用户就不需要去每个产品的编辑页面。

任何帮助,将不胜感激。谢谢。

标签: wordpresswoocommerce

解决方案


将您的上述功能添加cfwc_create_custom_field到操作中,例如-

add_action( 'woocommerce_product_quick_edit_end', 'cfwc_create_custom_field' );

并将您的数据保存在do_action( 'woocommerce_product_quick_edit_save', $product );挂钩中。


推荐阅读