-1

When I add the same SKU product while publishing a product, I get an invalid and duplicate SKU error, but the product is published without the SKU, I want the product not published without SKU and saved as a draft.

The product that does not enter SKU cannot be published, it is saved as a draft.

How can I do it?

SavPhill
  • 636
  • 6
  • 24

2 Answers2

0

There is currently not a way to make the SKU field in the product settings required. Currently, the only fields that are required in the admin dashboard are those that are required for functionality reasons.

You can allow duplicate SKU by adding this code to your active theme functions.php file.

add_filter( 'wc_product_sku_enabled', '__return_false' );

Use below code for updating status to draft if no SKU

// define the woocommerce_process_product_meta callback 
function action_woocommerce_process_product_meta( $post_id, $post ) {
    // make action magic happen here... 
    if ( empty( $_POST[ '_sku' ] ) ) {
        wp_update_post( array(
            'ID'             => $post_id,
            'post_status'    => 'draft',
        ) );
    }

}

// add the action 
add_action( 'woocommerce_process_product_meta', 'action_woocommerce_process_product_meta', 10, 2 );
mujuonly
  • 11,370
  • 5
  • 45
  • 75
0

It should show the error message that sku is duplicate, and prevent product publishing.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 31 '23 at 02:53