Home Forums WoodMart support forum When an SKU contains zeros at the beginning, WoodMart search fails

When an SKU contains zeros at the beginning, WoodMart search fails

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #585509

    Jeriss Cloud Center
    Participant

    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);
    #585616

    Jeriss Cloud Center
    Participant

    You can close this ticket, the issue is no longer valid.

    I’ve solved the issue by updating my custom code.

    function ensure_13_digit_sku( $sku ) {
    return str_pad($sku, 13, ‘0’, STR_PAD_LEFT);
    }

    add_action('woocommerce_admin_process_product_object', 'pre_process_product_sku');
    function pre_process_product_sku( $product ) {
        $sku = isset($_POST['_sku']) ? wc_clean( wp_unslash($_POST['_sku']) ) : '';
        $product->set_sku( ensure_13_digit_sku($sku) );
    }
    add_action('woocommerce_admin_process_variation_object', 'pre_process_variation_sku');
    function pre_process_variation_sku( $variation, $i ) {
        $sku = isset($_POST['variable_sku'][$i]) ? wc_clean( wp_unslash($_POST['variable_sku'][$i]) ) : '';
        $variation->set_sku( ensure_13_digit_sku($sku) );
    }
    #585873

    Hung Pham
    Participant

    Hi Jeriss Cloud Center,

    Thanks for reaching to us.

    Glad to hear your issue has been resolved. Keep us in mind for future questions and concerns, we’re always here to help!

    Regards,

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘When an SKU contains zeros at the beginning, WoodMart search fails’ is closed to new replies.