Created
July 31, 2018 07:02
-
-
Save mahbubme/509de791831048ea6cfcfe79607b05c0 to your computer and use it in GitHub Desktop.
Generate unique SKU with WPUF product form - https://mahbub.me/create-unique-sku-wp-user-frontend-custom-field/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wpuf_unique_product_sku_notice( $error ) { | |
if ( isset( $_POST['_sku'] ) ) { | |
global $wpdb; | |
$sku = $_POST['_sku']; | |
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) ); | |
if ( $product_id ) { | |
return __( 'SKU is not unique, you must enter unique SKU' ); | |
} | |
} | |
return ''; | |
} | |
add_filter( 'wpuf_update_post_validate', 'wpuf_unique_product_sku_notice' ); | |
add_filter( 'wpuf_add_post_validate', 'wpuf_unique_product_sku_notice' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment