Skip to content

Commit

Permalink
introduces the wp_product_has_unique_sku() function
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiosanches committed Jun 27, 2014
1 parent 2cb7b93 commit 0a4b638
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 126 deletions.
12 changes: 2 additions & 10 deletions includes/admin/class-wc-admin-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,16 +817,8 @@ private function quick_edit_save( $post_id, $product ) {

if ( $new_sku !== $sku ) {
if ( ! empty( $new_sku ) ) {
$sku_exists = $wpdb->get_var( $wpdb->prepare("
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_type = 'product'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
", $new_sku ) );

if ( ! $sku_exists ) {
$unique_sku = wp_product_has_unique_sku( $post_id, $new_sku );
if ( $unique_sku ) {
update_post_meta( $post_id, '_sku', $new_sku );
}
} else {
Expand Down
30 changes: 4 additions & 26 deletions includes/admin/meta-boxes/class-wc-meta-box-product-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,20 +1056,9 @@ public static function save( $post_id, $post ) {
update_post_meta( $post_id, '_sku', '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty( $new_sku ) ) {
if (
$wpdb->get_var( $wpdb->prepare("
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_type = 'product'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
AND $wpdb->postmeta.post_id <> $post_id LIMIT 1
", $new_sku ) )
) {

$unique_sku = wp_product_has_unique_sku( $post_id, $new_sku );
if ( ! $unique_sku ) {
WC_Admin_Meta_Boxes::add_error( __( 'Product SKU must be unique.', 'woocommerce' ) );

} else {
update_post_meta( $post_id, '_sku', $new_sku );
}
Expand Down Expand Up @@ -1506,20 +1495,9 @@ public static function save_variations( $post_id, $post ) {
update_post_meta( $variation_id, '_sku', '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty( $new_sku ) ) {
if (
$wpdb->get_var( $wpdb->prepare( "
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_type IN ('product', 'product_variation')
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
AND $wpdb->postmeta.post_id <> %d LIMIT 1
", $new_sku, $variation_id ) )
) {

$unique_sku = wp_product_has_unique_sku( $variation_id, $new_sku );
if ( ! $unique_sku ) {
WC_Admin_Meta_Boxes::add_error( __( 'Variation SKU must be unique.', 'woocommerce' ) );

} else {
update_post_meta( $variation_id, '_sku', $new_sku );
}
Expand Down
67 changes: 32 additions & 35 deletions includes/api/class-wc-api-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,22 @@ protected function save_product_meta( $id, $data ) {

// SKU
if ( isset( $data['sku'] ) ) {
$sku = $this->save_product_sku( $id, $data['sku'] );

if ( is_wp_error( $sku ) ) {
return $sku;
$sku = get_post_meta( $id, '_sku', true );
$new_sku = wc_clean( $data['sku'] );

if ( '' == $new_sku ) {
update_post_meta( $id, '_sku', '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty( $new_sku ) ) {
$unique_sku = wp_product_has_unique_sku( $id, $new_sku );
if ( ! $unique_sku ) {
return new WP_Error( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product' ), array( 'status' => 400 ) );
} else {
update_post_meta( $id, '_sku', $new_sku );
}
} else {
update_post_meta( $id, '_sku', '' );
}
}
}

Expand Down Expand Up @@ -1022,10 +1034,22 @@ protected function save_variations( $id, $data ) {

// SKU
if ( isset( $variation['sku'] ) ) {
$sku = $this->save_product_sku( $variation_id, $variation['sku'] );

if ( is_wp_error( $sku ) ) {
return $sku;
$sku = get_post_meta( $variation_id, '_sku', true );
$new_sku = wc_clean( $variation['sku'] );

if ( '' == $new_sku ) {
update_post_meta( $variation_id, '_sku', '' );
} elseif ( $new_sku !== $sku ) {
if ( ! empty( $new_sku ) ) {
$unique_sku = wp_product_has_unique_sku( $variation_id, $new_sku );
if ( ! $unique_sku ) {
return new WP_Error( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product' ), array( 'status' => 400 ) );
} else {
update_post_meta( $variation_id, '_sku', $new_sku );
}
} else {
update_post_meta( $variation_id, '_sku', '' );
}
}
}

Expand Down Expand Up @@ -1241,33 +1265,6 @@ protected function save_variations( $id, $data ) {
return true;
}

/**
* Save product SKU.
*
* @version 2.2
* @param int $id
* @param string $sku
* @return bool|WP_Error
*/
private function save_product_sku( $id, $sku ) {
$sku_found = $wpdb->get_var( $wpdb->prepare( "
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_type = 'product'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
AND $wpdb->posts.ID <> %s
", wc_clean( $sku, $id ) ) );

if ( $sku_found ) {
return new WP_Error( 'woocommerce_api_product_sku_already_exists', __( 'The SKU already exists on another product' ), array( 'status' => 400 ) );
}

update_post_meta( $id, '_sku', wc_clean( $sku ) );
return true;
}

/**
* Save product shipping data
*
Expand Down
138 changes: 83 additions & 55 deletions includes/wc-product-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function get_product( $the_product = false, $args = array() ) {
*/
function wc_update_product_stock( $product_id, $new_stock_level ) {
$product = get_product( $product_id );

if ( $product->get_stock_quantity() !== $new_stock_level ) {
$product->set_stock( $new_stock_level );
}
Expand Down Expand Up @@ -206,55 +206,55 @@ function wc_get_featured_product_ids() {
* @return string
*/
function wc_product_post_type_link( $permalink, $post ) {
// Abort if post is not a product
if ( $post->post_type !== 'product' )
return $permalink;

// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;

// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'product_cat' );

if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$product_cat = _x( 'uncategorized', 'slug', 'woocommerce' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$product_cat = $first_term->slug;
}

$find = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%post_id%',
'%category%',
'%product_cat%'
);

$replace = array(
date_i18n( 'Y', strtotime( $post->post_date ) ),
date_i18n( 'm', strtotime( $post->post_date ) ),
date_i18n( 'd', strtotime( $post->post_date ) ),
date_i18n( 'H', strtotime( $post->post_date ) ),
date_i18n( 'i', strtotime( $post->post_date ) ),
date_i18n( 's', strtotime( $post->post_date ) ),
$post->ID,
$product_cat,
$product_cat
);

$replace = array_map( 'sanitize_title', $replace );

$permalink = str_replace( $find, $replace, $permalink );

return $permalink;
// Abort if post is not a product
if ( $post->post_type !== 'product' )
return $permalink;

// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;

// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'product_cat' );

if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$product_cat = _x( 'uncategorized', 'slug', 'woocommerce' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$product_cat = $first_term->slug;
}

$find = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%post_id%',
'%category%',
'%product_cat%'
);

$replace = array(
date_i18n( 'Y', strtotime( $post->post_date ) ),
date_i18n( 'm', strtotime( $post->post_date ) ),
date_i18n( 'd', strtotime( $post->post_date ) ),
date_i18n( 'H', strtotime( $post->post_date ) ),
date_i18n( 'i', strtotime( $post->post_date ) ),
date_i18n( 's', strtotime( $post->post_date ) ),
$post->ID,
$product_cat,
$product_cat
);

$replace = array_map( 'sanitize_title', $replace );

$permalink = str_replace( $find, $replace, $permalink );

return $permalink;
}
add_filter( 'post_type_link', 'wc_product_post_type_link', 10, 2 );

Expand Down Expand Up @@ -307,11 +307,11 @@ function wc_get_formatted_variation( $variation, $flat = false ) {
}

// If this is a term slug, get the term's nice name
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $name ) ) ) ) {
$term = get_term_by( 'slug', $value, esc_attr( str_replace( 'attribute_', '', $name ) ) );
if ( ! is_wp_error( $term ) && $term->name )
$value = $term->name;
}
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $name ) ) ) ) {
$term = get_term_by( 'slug', $value, esc_attr( str_replace( 'attribute_', '', $name ) ) );
if ( ! is_wp_error( $term ) && $term->name )
$value = $term->name;
}

if ( $flat ) {
$variation_list[] = wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . urldecode( $value );
Expand Down Expand Up @@ -507,3 +507,31 @@ function wc_get_product_types() {
'variable' => __( 'Variable product', 'woocommerce' )
) );
}

/**
* Check if product sku is unique.
*
* @since 2.2
* @param int $product_id
* @param string $sku
* @return bool
*/
function wp_product_has_unique_sku( $product_id, $sku ) {
global $wpdb;

$sku_found = $wpdb->get_var( $wpdb->prepare( "
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
WHERE $wpdb->posts.post_type IN ( 'product', 'product_variation' )
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
AND $wpdb->postmeta.post_id <> %d LIMIT 1
", $sku, $product_id ) );

if ( $sku_found ) {
return false;
} else {
return true;
}
}

0 comments on commit 0a4b638

Please sign in to comment.