Skip to content

Instantly share code, notes, and snippets.

@nurul-umbhiya
Last active February 1, 2024 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurul-umbhiya/e55a227527afeda94d4a93a76c694d31 to your computer and use it in GitHub Desktop.
Save nurul-umbhiya/e55a227527afeda94d4a93a76c694d31 to your computer and use it in GitHub Desktop.
Limit Vendor to Publish 6 Products Daily
add_filter( 'dokan_can_edit_product', function( $errors ) {
if ( ! isset( $_POST['dokan_update_product'] ) ) {
return $errors;
}
if ( ! isset( $_POST['dokan_edit_product_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['dokan_edit_product_nonce'] ), 'dokan_edit_product' ) ) {
return $errors;
}
$post_id = isset( $_POST['dokan_product_id'] ) ? absint( $_POST['dokan_product_id'] ) : 0;
$product = wc_get_product( $post_id );
if ( ! $post_id || ! $product ) {
return $errors;
}
if ( $product->get_status() !== 'auto-draft' ) {
return $errors;
}
$daily_post_limit = 6;
$today_start = dokan_current_datetime()->setTime( 0, 0, 0 )->getTimestamp();
$today_end = dokan_current_datetime()->setTime( 23, 59, 59 )->getTimestamp();
$products = wc_get_products(
[
'date_created' => sprintf( '%1$d...%2$d', $today_start, $today_end ),
'limit' => $daily_post_limit + 1,
'return' => 'ids',
]
);
if ( count( $products ) >= $daily_post_limit ) {
error_log( 'count( $products ) > $daily_post_limit' );
$errors[] = sprintf( __( 'You have reached your daily limit of %d products.', 'dokan-lite' ), $daily_post_limit );
}
return $errors;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment