簡體   English   中英

將自定義字段添加到 Woocommerce 產品快速編輯管理面板

[英]Add custom field to Woocommerce products quick edit admin panel

我有這個 php 腳本到 functions.php 文件,用於將自定義字段(條碼/ean 編號)添加到 Woocommerce 產品編輯管理面板。

//add barcode field
function add_barcode(){
    woocommerce_wp_text_input(
        array(
            'id' => '_barcode',
            'label' => __( 'Barcode', 'woocommerce' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( "Enter barcode number.", "woocommerce" )
        )
    );
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');

function add_barcode_save( $product ){
    if( isset( $_POST['_barcode'] ) ) {
        $product->update_meta_data( '_barcode', sanitize_text_field( $_POST['_barcode'] ) );
    } else {
        $product->delete_meta_data( '_barcode' );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'add_barcode_save' );

自定義字段顯示正確,我可以用這個 php 腳本保存。 但我想將此自定義字段添加到快速編輯面板,以加快編輯速度。

有人可以為此寫額外的 php 腳本嗎?

提前致謝。

最后我在google上搜索后找到了解決方案。

//add barcode input to quick edit
function add_barcode_qe()
{?>
<label><span class="title"><?php echo esc_html_e('Barcode', 'woocommerce');?></span>
<span class="input-text-wrap"><?php
woocommerce_wp_text_input(
        array(
            'id' => '_barcode',
            'desc_tip' => false,
            'class' => 'text custom_field',
            'style' => 'margin-bottom:3px;'
        )
    );
echo '</span></label>';
}
add_action('woocommerce_product_quick_edit_start', 'add_barcode_qe');

//populate barcode quick edit field
function populate_brcd_field()
{
?>
    <script>
        (function($) {
            $('#the-list').on('click', '.editinline', function() {

                var post_id = $(this).closest('tr').attr('id');
                post_id = post_id.replace('post-', '');

                var custom_field = $('#jcwc1_product_data_' + post_id).text();
                $('input[name="_barcode"]', '.inline-edit-row').val(custom_field);
            });
        })(jQuery);
    </script>
<?php
}
add_action('admin_footer', 'populate_brcd_field');

//save barcode input value on submit
function add_barcode_qe_save($product)
{
    //$product = wc_get_product();
    $pid = $product->get_id();
    if (isset($_REQUEST['_barcode'])) {
        $brcd = $_REQUEST['_barcode'];
        update_post_meta($pid, '_barcode', wc_clean($brcd));
    } else {
        delete_post_meta($pid, '_barcode');
    }
}

add_action('woocommerce_product_quick_edit_save', 'add_barcode_qe_save');

條形碼輸入字段出現在 SKU 之前。 我如何將它移到 SKU 字段下方?

我想您不知道如何在快速編輯視圖中填充文本字段以顯示條形碼? 我們已經使用了這種方法,但正在努力解決文本字段的填充問題。

條碼字段IMG

// Add boarcode to products

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'barcode',
'label' => __( 'Barcode', 'woocommerce' ),
'placeholder' => 'barcode here',
'desc_tip' => 'true',
'description' => __( 'Product barcode.', 'woocommerce' )
)
);
}

function woo_add_custom_general_fields_save( $post_id ){

// Textarea
$woocommerce_barcode = $_POST['barcode'];
if( !empty( $woocommerce_barcode ) )
update_post_meta( $post_id, 'barcode', esc_html( $woocommerce_barcode ) );

}

//add barcode input to quick edit
function add_barcode_qe()
{?>
<label><span class="title"><?php echo esc_html_e('Barcode', 'woocommerce');?></span>
<span class="input-text-wrap"><?php
woocommerce_wp_text_input(
        array(
            'id' => 'barcode',
            'desc_tip' => false,
            'class' => 'text custom_field',
            'style' => 'margin-bottom:3px;'
        )
    );
echo '</span></label>';
}
add_action('woocommerce_product_quick_edit_start', 'add_barcode_qe');

//populate barcode quick edit field
function populate_brcd_field()
{
?>
    <script>
        (function($) {
            $('#the-list').on('click', '.editinline', function() {
                var post_id = $(this).closest('tr').attr('id');
                post_id = post_id.replace('post-', '');

                var custom_field = $('#jcwc1_product_data_' + post_id).text();
                $('input[name="barcode"]', '.inline-edit-row').val(custom_field);
            });
        })(jQuery);
    </script>
<?php
}
add_action('admin_footer', 'populate_brcd_field');

//save barcode input value on submit
function add_barcode_qe_save($product)
{
    //$product = wc_get_product();
    $pid = $product->get_id();
    if (isset($_REQUEST['barcode'])) {
        $brcd = $_REQUEST['barcode'];
        update_post_meta($pid, 'barcode', wc_clean($brcd));
    } else {
        delete_post_meta($pid, 'barcode');
    }
}

add_action('woocommerce_product_quick_edit_save', 'add_barcode_qe_save');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM