Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('after_wcfm_products_manage', function() {
- ?>
- <style>
- #wcfm_products_manage_form .wcfm-title-error {
- margin: -20px 0 10px !important;
- color: red;
- font-size: 12px;
- margin-top: 5px;
- }
- </style>
- <script>
- jQuery(function($) {
- const $form = $('#wcfm_products_manage_form');
- const displayError = (message) => {
- $form.find('.wcfm-title-error').remove();
- if (message) {
- $form.find('#pro_title').after(`<p class="wcfm-title-error wcfm_validation_error">${message}</p>`);
- }
- };
- if($form.length) {
- const $productTitle = $form.find('#pro_title');
- $($productTitle).on('blur', function() {
- displayError(null);
- if($productTitle.length && $productTitle.val()) {
- let title = $productTitle.val().trim();
- $('#wcfm_products_manage_form_general_expander').block({
- message: null,
- overlayCSS: {
- background: '#fff',
- opacity: 0.6
- }
- });
- var data = {
- action: 'wcfm_is_product_title_exists',
- title: title,
- product_id: $form.find('#pro_id').val() || 0,
- wcfm_ajax_nonce: wcfm_params.wcfm_ajax_nonce,
- }
- $.ajax({
- type: 'POST',
- url: wcfm_params.ajax_url,
- data: data,
- success: function(response) {
- if(!response.success) {
- displayError(response.data);
- $productTitle.focus().select();
- }
- $('#wcfm_products_manage_form_general_expander').unblock();
- }
- });
- }
- });
- }
- });
- </script>
- <?php
- });
- add_action('wp_ajax_wcfm_is_product_title_exists', function() {
- if ( ! check_ajax_referer( 'wcfm_ajax_nonce', 'wcfm_ajax_nonce', false ) ) {
- wp_send_json_error( esc_html__( 'Invalid nonce! Refresh your page and try again.', 'wc-frontend-manager' ) );
- }
- if ( !current_user_can( 'manage_woocommerce' ) && !current_user_can( 'wcfm_vendor' ) && !current_user_can( 'seller' ) && !current_user_can( 'vendor' ) && !current_user_can( 'shop_staff' ) ) {
- wp_send_json_error( esc_html__( 'You don’t have permission to do this.', 'woocommerce' ) );
- }
- $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : '';
- $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
- if(!$title) {
- wp_send_json_error('Title is required');
- }
- $args = [
- 'post_type' => 'product',
- 'post_status' => 'any',
- 's' => $title,
- 'posts_per_page' => 1,
- 'post__not_in' => [$product_id],
- ];
- $query = new WP_Query($args);
- if($query->have_posts()) {
- wp_send_json_error('Product with this title already exists');
- } else {
- wp_send_json_success('Product title is available');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement