How to add a SKU field to your WooCommerce booking product

First, why is there no SKU input field for the booking product data?

That is because the SKU is generally used for Inventory and stock management, however with Bookable products the product’s availability is instead managed through the Booking Availability tab or through linked Resources. Because of this, the Inventory tab (with the SKU) is not used on Bookable products and you will not be able to add a SKU.

You can, however, achieve this by using some code. The code below will add a SKU tab for bookable products where you’d be able to include a SKU. The SKU will be shown in the meta data of the booking product, in a column of the product page (just like the standard SKU), in the cart table, the checkout, and in the order screen.

/**
 * Adds the custom product tab.
 */

function custom_product_booking_sku_tab($tabs) {

    $tabs['booking-sku'] = array(
        'label'     => __( 'Booking SKU', 'woocommerce' ),
        'target'    => 'booking_sku_options',
        'class'     => array( 'show_if_booking'  ),
    );

    return $tabs;

}
add_filter( 'woocommerce_product_data_tabs', 'custom_product_booking_sku_tab' );

/**
 * Contents of the booking SKU options product tab.
 */

function booking_sku_options_product_tab_content() {

    global $post;

    ?><div id='booking_sku_options' class='panel woocommerce_options_panel'><?php

        ?><div class='options_group'><?php

            woocommerce_wp_text_input( array(
                'id'                => '_booking_sku',
                'label'             => __( 'Booking SKU', 'woocommerce' ),
                'desc_tip'          => 'true',
                'description'       => __( 'Enter the booking SKU number.', 'woocommerce' ),
                'type'              => 'text',
            ) );

        ?></div>

    </div><?php

}
add_filter( 'woocommerce_product_data_panels', 'booking_sku_options_product_tab_content' );


/**
 * Save the custom fields.
 */

function save_booking_sku_option_fields($post_id) {

    if ( isset( $_POST['_booking_sku'] ) ) :
        update_post_meta( $post_id, '_booking_sku', absint( $_POST['_booking_sku'] ) );
    endif;
    
}
add_action( 'woocommerce_process_product_meta_booking', 'save_booking_sku_option_fields'  );
 
function show_booking_sku_single_product() {
global $product;

?>

<div class="product_meta">
   <?php if ( $product->get_meta('_booking_sku') || $product->is_type( 'booking' ) )  : ?>
      <span class="sku_wrapper"><?php esc_html_e( 'Booking SKU:', 'woocommerce' ); ?> <span class="sku"><?php echo ( $sku = $product->get_meta('_booking_sku') ) ? $sku : esc_html__( 'N/A', 'woocommerce' ); ?></span></span>
   <?php endif; ?>
   </div>
   <?php
}

add_action( 'woocommerce_single_product_summary', 'show_booking_sku_single_product', 40 );

/**
 * Build column for product page
 */

function add_booking_sku_column($columns){
    $columns['booking_sku'] = 'Booking SKU';
    return $columns;
}

add_filter('manage_edit-product_columns', 'add_booking_sku_column');

/**
 * Populate the column in product page
 */
 
function populate_booking_sku_column( $column_name ){
    global $product;
 
    if( $column_name == 'booking_sku' ) {
        if ( $product->get_meta('_booking_sku') || $product->is_type( 'booking' ) )  {
        echo ( $sku = $product->get_meta('_booking_sku'));
        }
    }
}

add_action( 'manage_posts_custom_column', 'populate_booking_sku_column' );

/**
 * Show the Booking SKU in cart table
 */

add_filter( 'woocommerce_cart_item_name', 'showing_booking_sku_in_cart_items', 99, 3 );
function showing_booking_sku_in_cart_items( $item_name, $cart_item, $cart_item_key  ) {
    // The WC_Product object
    $product = $cart_item['data'];
    // Get the booking SKU
    $sku = $product->get_meta('_booking_sku');
    // If the booking sku doesn't exist
    if(empty($sku)) return $item_name;
    // Add the booking sku
    $item_name .= '<br><small class="product-sku">' . __( "Booking SKU: ", "woocommerce") . $sku . '</small>';

    return $item_name;
}

/**
 * Show the Booking SKU in order data
 */

function booking_sku_order_item_headers( $order ) {
    echo '<th class="line_sku sortable" data-sort="your-sort-option">SKU</th>';
}

add_action( 'woocommerce_admin_order_item_headers', 'booking_sku_order_item_headers', 10, 1 );

// Add content
function booking_sku_order_item_values( $product, $item, $item_id ) {
    if ($product) { 
        $sku = $product->get_meta('_booking_sku');     
        echo '<td class="sku_wrapper">' . $sku . '</td>';                
    }
}
add_action( 'woocommerce_admin_order_item_values', 'booking_sku_order_item_values', 10, 3 );

You can add this code to your site following the instructions here:

How to add custom code to your WooCommerce/WordPress site the right way

Have any feedback? Be sure to let me know here: Contact me

3 thoughts on “How to add a SKU field to your WooCommerce booking product

Share your thoughts

Your email address will not be published. Required fields are marked *