@@ -815,12 +815,44 @@ protected function syncSelectedproductType($ID = null)
815
815
*/
816
816
public function suppressInvalidDuplicatedSKUErrorMsg ($ sku_found , $ product_id , $ sku )
817
817
{
818
- $ metas = static ::getProductMetaToCopy ();
819
818
820
- if (in_array ('_sku ' , $ metas )) {
821
- return false ;
822
- } else {
823
- return $ sku_found ;
824
- }
825
- }
826
- }
819
+ //if the sku is not duplicate, no further check needed
820
+ if ( ! $ sku_found ) {
821
+ return false ;
822
+ }
823
+
824
+ /*
825
+ * now check the duplicates
826
+ * this is the same as woocommerce is_existing_sku but
827
+ * gets all the product ids with the matching sku
828
+ */
829
+ global $ wpdb ;
830
+
831
+ // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
832
+ $ postids = $ wpdb ->get_col (
833
+ $ wpdb ->prepare (
834
+ "SELECT $ wpdb ->posts .ID
835
+ FROM $ wpdb ->posts
836
+ LEFT JOIN $ wpdb ->postmeta ON ( $ wpdb ->posts .ID = $ wpdb ->postmeta .post_id )
837
+ WHERE $ wpdb ->posts .post_type IN ( 'product', 'product_variation' )
838
+ AND $ wpdb ->posts .post_status != 'trash'
839
+ AND $ wpdb ->postmeta .meta_key = '_sku' AND $ wpdb ->postmeta .meta_value = %s
840
+ AND $ wpdb ->postmeta .post_id <> %d
841
+ " , wp_slash ( $ sku ), $ product_id
842
+ )
843
+ );
844
+ $ curlang = pll_get_post_language ( $ product_id );
845
+ foreach ( $ postids as $ post_id ) {
846
+ //suppress duplicate sku error on translations only
847
+ $ duplang = pll_get_post_language ( $ post_id );
848
+ //if there is another product in the same language with the same sku
849
+ //disallow and return true from wc_product_has_unique_sku
850
+ if ( $ post_id != $ product_id && $ curlang == pll_get_post_language ( $ post_id ) ) {
851
+ return true ;
852
+ }
853
+ }
854
+ //if we got here, there were no duplicates in the same language
855
+ return false ;
856
+ }
857
+
858
+ }
0 commit comments