Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

php - Add and save admin product variations custom field in Woocommerce

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your last hooked function you have a missing argument, which is a similar to $loop argument in your 3rd function. So I have made 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]) );
}

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+


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...