Advertisement
palsushobhan

wcfm-restrict-duplicate-product-tile

Jun 12th, 2025
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. add_action('after_wcfm_products_manage', function() {
  2.     ?>
  3.     <style>
  4.         #wcfm_products_manage_form .wcfm-title-error {
  5.             margin: -20px 0 10px !important;
  6.             color: red;
  7.             font-size: 12px;
  8.             margin-top: 5px;
  9.         }
  10.     </style>
  11.     <script>
  12.         jQuery(function($) {
  13.             const $form = $('#wcfm_products_manage_form');
  14.             const displayError = (message) => {
  15.                 $form.find('.wcfm-title-error').remove();
  16.                 if (message) {
  17.                     $form.find('#pro_title').after(`<p class="wcfm-title-error wcfm_validation_error">${message}</p>`);
  18.                 }
  19.             };
  20.             if($form.length) {
  21.                 const $productTitle = $form.find('#pro_title');
  22.                 $($productTitle).on('blur', function() {
  23.                     displayError(null);
  24.                     if($productTitle.length && $productTitle.val()) {
  25.                         let title = $productTitle.val().trim();
  26.                         $('#wcfm_products_manage_form_general_expander').block({
  27.                             message: null,
  28.                             overlayCSS: {
  29.                                 background: '#fff',
  30.                                 opacity: 0.6
  31.                             }
  32.                         });
  33.                         var data = {
  34.                             action: 'wcfm_is_product_title_exists',
  35.                             title: title,
  36.                             product_id: $form.find('#pro_id').val() || 0,
  37.                             wcfm_ajax_nonce: wcfm_params.wcfm_ajax_nonce,
  38.                         }
  39.                         $.ajax({
  40.                             type: 'POST',
  41.                             url: wcfm_params.ajax_url,
  42.                             data: data,
  43.                             success: function(response) {
  44.                                 if(!response.success) {
  45.                                     displayError(response.data);
  46.                                     $productTitle.focus().select();
  47.                                 }
  48.                                 $('#wcfm_products_manage_form_general_expander').unblock();
  49.                             }
  50.                         });
  51.                     }
  52.                 });
  53.             }
  54.  
  55.         });
  56.     </script>
  57.     <?php
  58. });
  59. add_action('wp_ajax_wcfm_is_product_title_exists', function() {
  60.     if ( ! check_ajax_referer( 'wcfm_ajax_nonce', 'wcfm_ajax_nonce', false ) ) {
  61.         wp_send_json_error( esc_html__( 'Invalid nonce! Refresh your page and try again.', 'wc-frontend-manager' ) );
  62.     }
  63.     if ( !current_user_can( 'manage_woocommerce' ) && !current_user_can( 'wcfm_vendor' ) && !current_user_can( 'seller' ) && !current_user_can( 'vendor' ) && !current_user_can( 'shop_staff' ) ) {
  64.         wp_send_json_error( esc_html__( 'You don&#8217;t have permission to do this.', 'woocommerce' ) );
  65.     }
  66.     $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : '';
  67.     $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
  68.     if(!$title) {
  69.         wp_send_json_error('Title is required');
  70.     }
  71.     $args = [
  72.         'post_type' => 'product',
  73.         'post_status' => 'any',
  74.         's' => $title,
  75.         'posts_per_page' => 1,
  76.         'post__not_in' => [$product_id],
  77.     ];
  78.     $query = new WP_Query($args);
  79.     if($query->have_posts()) {
  80.         wp_send_json_error('Product with this title already exists');
  81.     } else {
  82.         wp_send_json_success('Product title is available');
  83.     }
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement