Переглянути джерело

studiou-wc-mandatory-products: fix adding product in product category edit.

Dalibor Votruba 1 рік тому
батько
коміт
324c18d764

+ 71 - 0
studiou-wc-mandatory-products/assets/js/admin.js

@@ -0,0 +1,71 @@
+/**
+ * StudioU WC Mandatory Products Admin JavaScript
+ */
+jQuery(function($) {
+    'use strict';
+
+    // Initialize WooCommerce product search on existing elements
+    function initProductSearch() {
+        $('.wc-product-search').each(function() {
+            if ($(this).data('select2')) {
+                // Already initialized
+                return;
+            }
+
+            $(this).selectWoo({
+                allowClear: $(this).data('allow_clear') ? true : false,
+                placeholder: $(this).data('placeholder'),
+                minimumInputLength: 1,
+                escapeMarkup: function(m) {
+                    return m;
+                },
+                ajax: {
+                    url: wc_enhanced_select_params.ajax_url,
+                    dataType: 'json',
+                    delay: 250,
+                    data: function(params) {
+                        return {
+                            term: params.term,
+                            action: $(this).data('action') || 'woocommerce_json_search_products_and_variations',
+                            security: wc_enhanced_select_params.search_products_nonce,
+                            exclude: $(this).data('exclude'),
+                            include: $(this).data('include'),
+                            limit: $(this).data('limit')
+                        };
+                    },
+                    processResults: function(data) {
+                        var terms = [];
+                        if (data) {
+                            $.each(data, function(id, text) {
+                                terms.push({id: id, text: text});
+                            });
+                        }
+                        return {
+                            results: terms
+                        };
+                    },
+                    cache: true
+                }
+            });
+        });
+    }
+
+    // Initialize product search for existing fields
+    $(document).ready(function() {
+        initProductSearch();
+    });
+
+    // Add new mandatory product row
+    $(document).on('click', '.add-mandatory-product', function() {
+        var template = wp.template('mandatory-product-row');
+        $('#studiou_mandatory_products_container').append(template());
+        
+        // Initialize product search for the new row
+        initProductSearch();
+    });
+    
+    // Remove mandatory product row
+    $(document).on('click', '.remove-mandatory-product', function() {
+        $(this).closest('.mandatory-product-row').remove();
+    });
+});

+ 4 - 2
studiou-wc-mandatory-products/directory-structure.txt

@@ -1,7 +1,9 @@
 studiou-wc-mandatory-products/
 ├── assets/
-│   └── css/
-│       └── frontend.css
+│   ├── css/
+│   │   └── frontend.css
+│   └── js/
+│       └── admin.js
 ├── includes/
 │   ├── class-studiou-wcmp-admin.php
 │   └── class-studiou-wcmp-frontend.php

+ 10 - 48
studiou-wc-mandatory-products/includes/class-studiou-wcmp-admin.php

@@ -148,33 +148,6 @@ class StudioU_WCMP_Admin {
                 </p>
             </div>
         </script>
-        
-        <script>
-            jQuery(function($) {
-                // Initialize select2
-                $(document).ready(function() {
-                    $('.wc-product-search').selectWoo({
-                        width: '100%'
-                    });
-                });
-                
-                // Add new row
-                $('.add-mandatory-product').on('click', function() {
-                    var template = wp.template('mandatory-product-row');
-                    $('#studiou_mandatory_products_container').append(template());
-                    
-                    // Re-initialize select2 for the new row
-                    $('#studiou_mandatory_products_container .wc-product-search:last').selectWoo({
-                        width: '100%'
-                    });
-                });
-                
-                // Remove row
-                $(document).on('click', '.remove-mandatory-product', function() {
-                    $(this).closest('.mandatory-product-row').remove();
-                });
-            });
-        </script>
         <?php
     }
 
@@ -227,26 +200,6 @@ class StudioU_WCMP_Admin {
                 </p>
             </div>
         </script>
-        
-        <script>
-            jQuery(function($) {
-                // Add new row
-                $('.add-mandatory-product').on('click', function() {
-                    var template = wp.template('mandatory-product-row');
-                    $('#studiou_mandatory_products_container').append(template());
-                    
-                    // Initialize select2 for the new row
-                    $('#studiou_mandatory_products_container .wc-product-search:last').selectWoo({
-                        width: '100%'
-                    });
-                });
-                
-                // Remove row
-                $(document).on('click', '.remove-mandatory-product', function() {
-                    $(this).closest('.mandatory-product-row').remove();
-                });
-            });
-        </script>
         <?php
     }
 
@@ -306,8 +259,17 @@ class StudioU_WCMP_Admin {
         }
         
         // WooCommerce admin scripts
-        wp_enqueue_script('wc-enhanced-select');
         wp_enqueue_style('woocommerce_admin_styles');
+        wp_enqueue_script('wc-enhanced-select');
+        
+        // Add our custom JS to properly initialize SelectWoo
+        wp_enqueue_script(
+            'studiou-wcmp-admin-js',
+            STUDIOU_WCMP_PLUGIN_URL . 'assets/js/admin.js',
+            array('jquery', 'wc-enhanced-select', 'wp-util'),
+            STUDIOU_WCMP_VERSION,
+            true
+        );
     }
 
     /**