Hello @omrto,
Thanks for reaching out.
As you already know, currently the feature that you wanted is not available. We already have this feature request in our queue https://github.com/pluginever/wc-serial-numbers/issues/251
I’ll notify you when this update gets released.
Hope you’re enjoying Serial Numbers for WooCommerce. Have a great day!
Thread Starter
omrto
(@omrto)
Thank you for your reply @bappi. I’ll keep an eye out for this.
Thread Starter
omrto
(@omrto)
@bappi, Here is a code that may or may not be useful to you. I got from ChatGPT. It is written to accommodate my unique needs, but I haven’t tried it out yet. Our company has been exploring the idea of developing our own custom ecommerce/production system in Laravel. If I come back around to this and try it out I will update this thread.
// Get the current order items
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
// Loop through each order item
foreach ( $order_items as $item_id => $item ) {
// Get the product SKU and formatted date
$product_sku = $item->get_product()->get_sku();
$date = date( "dmY" );
// Check the database for duplicate serial numbers
$serial_number_check = check_for_duplicate_serial_number( $product_sku, $date, $order->get_order_number() );
// If there's no duplicate, generate the serial number
if ( ! $serial_number_check ) {
$serial_number = "{$product_sku}-{$date}-{$order->get_order_number()}";
}
// If there is a duplicate, increment a letter at the end of the serial number
else {
$serial_number = "{$product_sku}-{$date}-{$order->get_order_number()}-{$serial_number_check}";
}
// Save the serial number as a custom field for the product
update_post_meta( $item_id, 'Serial#', $serial_number );
}
// Function to check for duplicates in the database
function check_for_duplicate_serial_number( $product_sku, $date, $order_number ) {
global $wpdb;
// Query the database for serial numbers that match the current product SKU, date, and order number
$results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'Serial#' AND meta_value LIKE %s", "{$product_sku}-{$date}-{$order_number
network error
}%") );
// If there are no duplicates, return false
if ( count( $results ) === 0 ) {
return false;
}
// If there are duplicates, increment a letter at the end of the serial number
else {
$letters = range('a', 'z');
$last_letter = end( $results )->meta_value[-1];
$current_index = array_search( $last_letter, $letters );
return $letters[$current_index + 1];
}
}