-->

Display and save a Custom Field in product variati

2019-01-28 18:26发布

问题:

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

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.

Here is what I got currently:

// 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.

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.

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.

回答1:

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_action('woocommerce_product_options_sku','add_barcode', 10, 0 );
function add_barcode(){

    global $woocommerce, $post;

    // getting the barcode value if exits
    $product_barcode = get_post_meta( $post->ID, '_barcode', true );
    if( ! $product_barcode ) $product_barcode = '';

    // Displaying the barcode custom field
    woocommerce_wp_text_input( array(
        'id'          => '_barcode',
        'label'       => __('Barcode','woocommerce'),
        'placeholder' => 'Scan Barcode',
        'desc_tip'    => 'true',
        'description' => __('Scan barcode.','woocommerce')
    ), $product_barcode); // <== added "$product_barcode" here to get the value if exist

}

add_action( 'woocommerce_process_product_meta', 'save_barcode', 10, 1 );
function save_barcode( $post_id ){

    $product_barcode_field = $_POST['_barcode'];
    if( !empty( $product_barcode_field ) )
        update_post_meta( $post_id, '_barcode', esc_attr( $product_barcode_field ) );

}

add_action( 'woocommerce_product_after_variable_attributes','add_barcode_variations',10 , 3 );
function add_barcode_variations( $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' => __('Scan barcode.','woocommerce'),
        'value' => $variation_barcode,
    ) );
}
//Save Variation Barcode
add_action( 'woocommerce_save_product_variation','save_barcode_variations', 10 ,2 );
function save_barcode_variations( $variation_id, $loop ){

    $barcode = $_POST["_barcode_$loop"];
    if(!empty($barcode))
        update_post_meta( $variation_id, '_barcode', sanitize_text_field($barcode) );
}

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

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



回答2:

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

the file that need to be edited to add fields under SKU is plugins/woocommerce/includes/admin/meta-boxes/views/html-variation-admin.php