在Woocommerce中添加和保存管理产品变体自定义字段 [英] Add and save admin product variations custom field in Woocommerce

查看:114
本文介绍了在Woocommerce中添加和保存管理产品变体自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我获得了以下代码,这使我可以将条形码字段添加到产品的库存选项"中.

So I've got the following code which makes me add a Barcode field to the Inventory Options of a product.

现在,我还想将其添加到每个版本中,这样当我通过WooCommerce销售点插件扫描产品的条形码时,可以轻松添加版本产品.

Now I also want to add this to each variations so I can easily add Variation Products when I scan the Barcode of the product via the WooCommerce Point of Sale plugin.

这是我目前得到的:

// Add Barcode field in simple product inventory options
add_action('woocommerce_product_options_sku','add_barcode',10,0);
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'),
            'value'       => get_post_meta($post->ID,'_barcode',true)
        )
    );
}

// Save Barcode field value for simple product inventory options
add_action('woocommerce_process_product_meta','save_barcode',10,1);
function save_barcode($post_id){
    if(!empty($_POST['_barcode']))
    update_post_meta($post_id,'_barcode',sanitize_text_field($_POST['_barcode']));
}

// Add a Barcode field in product variations options
add_action('woocommerce_product_after_variable_attributes','add_barcode_variations',10,3);
function add_barcode_variations($loop,$variation_data,$variation){
    woocommerce_wp_text_input(
        array(
            'id'          => '_barcode[' . $variation->ID . ']',
            'label'       => __('Variation Barcode','woocommerce'),
            'placeholder' => 'Scan Barcode',
            'desc_tip'    => 'true',
            'description' => __('Scan barcode.','woocommerce'),
            'value'       => get_post_meta($variation->ID,'_barcode',true)
        )
    );
}

// Save Barcode field for product variations options
add_action( 'woocommerce_save_product_variation','save_barcode_variations',10,2);
function save_barcode_variations($post_id){
    $barcode = $_POST['_barcode'][$post_id];
    if(!empty($barcode)) update_post_meta($post_id,'_barcode',sanitize_text_field($barcode));
}

// Set POS Custom Code
add_filter('woocommerce_pos_barcode_meta_key','pos_barcode_field');
function pos_barcode_field(){
    return '_barcode';
}

但是这里的问题是,现在我添加了一部分变化,如果我更新产品,则库存"设置中的主条形码字段将显示数组",而不是提供的条形码.

But the problem here is, that with that I now added a part for the variation, that if I update the product the main barcode field in the Inventory settings shows "Array" instead of the provided barcode.

我认为这与ID的变化有关,除了结尾处的variationID之外,其他版本的ID与原始字段相同.扫描产品时,该ID要求与我使用的WooCommerce POS插件相同的原因在该ID上被过滤.

I assume that this has something to do with the ID being the same for the variations as the original field other than the variationID at the end. The reason the ID requires to be the same as the WooCommerce POS plugin I'm using, is being filtered on that ID when I scan a product.

但是目前还不知道要更改为正确保存库存条形码"字段和变体条形码"字段需要更改的内容.

But currently can't figure out, to what I have to change to make both the Inventory Barcode Field and the Variation Barcode field to be saved properly.

我想将变体字段添加到变体SKU字段下方,但无法直接找到合适的钩子来完成此操作.

As well as I'd like to add the variation field below the variation SKU field, but can't directly find the proper hook to do this.

预先感谢您提供更多信息.

Thanks in advance for further information.

推荐答案

在上一个挂钩函数中,您缺少一个参数,该参数与第三个函数中的 $loop 参数相似.因此,我对您的代码没有什么改动:

In your last hooked function you have a missing argument, which is a similar to $loop argument in your 3rd function. So I have maid little changes in your code:

// Add product Barcode custom field
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' => __('This is the Scan barcode field for this product.','woocommerce')
    ) ); 
}

// Save product Barcode custom field
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'] ) );
}

// Add Variation Barcode custom field
add_action( 'woocommerce_variation_options_pricing', 'add_barcode_variation_custom_field', 10, 3 );
function add_barcode_variation_custom_field( $loop, $variation_data, $variation ){

    $variation_barcode = get_post_meta($variation->ID,"_barcode", true );
    if( ! $variation_barcode ) $variation_barcode = "";

    woocommerce_wp_text_input( array(
        'id'          => '_barcode['.$loop.']',
        'label'       => __('Variation Barcode','woocommerce'),
        'placeholder' => 'Scan Barcode',
        'desc_tip'    => 'true',
        'description' => __('This is the Scan barcode field for this variation.','woocommerce')
        'value'       => get_post_meta($variation->ID,"_barcode", true ),
    ) );
}

// Save Variation Barcode custom field value
add_action( 'woocommerce_save_product_variation', 'save_barcode_variation_custom_field', 10, 2 );
function save_barcode_variation_custom_field( $variation_id, $i ){
    if( isset($_POST['_barcode'][$i]) )
        update_post_meta( $variation_id, '_barcode', sanitize_text_field($_POST['_barcode'][$i]) );
}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,可用于WooCommerce 2.6+和3.0 +

This code is tested and works for WooCommerce version 2.6+ and 3.0+

这篇关于在Woocommerce中添加和保存管理产品变体自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆