I am using the code below to ensure that the SKU always contains 13 digits.
This code is working fine but then, in the frontend, when I search for a product based on an SKU that has been formatted with this code, the WoodMart search doesn’t find the product.
Why? Because I believe WoodMart search fails to find products based on SKU if the SKU contains zeros at the beginning of the SKU code.
See videos in private.
I tried then using Relevanssi, and the search works fine with Relevanssi.
I reverted back to default WoodMart search, but then the search doesn’t work in this case.
function ensure_13_digit_sku($sku) {
return str_pad($sku, 13, '0', STR_PAD_LEFT);
}
function ensure_sku_length_on_save($post_id, $post, $update) {
if ($post->post_type !== 'product') {
return;
}
$sku = get_post_meta($post_id, '_sku', true);
if (!$sku || strlen($sku) < 13) {
$new_sku = ensure_13_digit_sku($sku);
update_post_meta($post_id, '_sku', $new_sku);
}
}
add_action('save_post', 'ensure_sku_length_on_save', 10, 3);