在WooCommerce产品选项编辑页面中,在SKU之前显示自定义字段

问题描述 投票:1回答:1

所以我有以下功能在“产品清单”选项卡中添加条形码字段。但是这个字段是在所有其他内容之后添加的,我希望在SKU代码之前有这个。

function add_barcode(){
    global $woocommerce,$post;
    woocommerce_wp_text_input(
        array(
            'id'          => '_barcode',
            'label'       => __('Barcode','woocommerce'),
            'placeholder' => 'Scan Barcode',
            'desc_tip'    => 'true',
            'description' => __('Scan barcode.','woocommerce')
        ));
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');

无论如何将函数/字段放在SKU之前,也就是在实际挂钩之前,就像woocommerce_before_product_options_inventory_product_data?

感谢您提前提出的任何建议。

php wordpress woocommerce product inventory
1个回答
2
投票

在SKU之前不可能放置任何自定义场 您可以查看显示产品库存字段的html-product-data-inventory.php source code file

但您可以在SKU字段之后显示“条形码”自定义字段(例如)

为此,你必须在woocommerce_product_options_sku动作钩子中挂钩自定义函数。您的代码中还有一些缺少的东西用于显示保存的值。

最后,在保存或更新产品时,您需要另一个功能来保存该值。

这是完整的代码:

add_action('woocommerce_product_options_sku','add_barcode_custom_field' );
function add_barcode_custom_field(){
    woocommerce_wp_text_input( array(
        'id'          => '_barcode',
        'label'       => __('Barcode','woocommerce'),
        'placeholder' => 'Scan Barcode',
        'desc_tip'    => 'true',
        'description' => __('Scan barcode.','woocommerce')
    ) ); 
}

add_action( 'woocommerce_process_product_meta', 'save_barcode_custom_field', 10, 1 );
function save_barcode_custom_field( $post_id ){
    if( isset($_POST['_barcode']) )
        update_post_meta( $post_id, '_barcode', esc_attr( $_POST['_barcode'] ) );
}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。

此代码经过测试,适用于WooCommerce版本2.6+和3.0+

© www.soinside.com 2019 - 2024. All rights reserved.