在我的WooCommerce商店模板中,我正在使用Ajax在archive-product.php页面中动态加载单个产品内容。这部分正在工作。但是缺少用于选择产品变体的javascript,因此我尝试在成功的ajax请求后将脚本添加到html。我可以加载脚本,但是仍然不能选择产品版本。没有事件被触发。好像我缺少什么...
我的Javascript:
jQuery('.project-preview').on('click',function(){ var theId = $(this).attr('data-project-id'); var div = $('#product-container'); $.ajax({ type: "POST", url: singleprojectajax.ajaxurl, data : {action : 'load_single_product_content', post_id: theId }, success: function(data){ div.html(data); loadVariationScript(); }, error : function() { } }); }); function loadVariationScript () { jQuery.getScript("../wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart-variation.min.js"); jQuery.getScript("../wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart-variation.js"); }
在functions.php中
add_action( 'wp_enqueue_scripts', 'child_add_scripts' ); function child_add_scripts(){ wp_register_script( 'pa_script', get_stylesheet_directory_uri() . '/main.js', array('jquery'), true ); wp_localize_script( 'pa_script', 'singleprojectajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); wp_enqueue_script('pa_script'); } function load_single_product_content () { $post_id = intval(isset($_POST['post_id']) ? $_POST['post_id'] : 0); if ($post_id > 0) { $the_query = new WP_query(array('p' => $post_id, 'post_type' => 'product')); if ($the_query->have_posts()) { while ($the_query->have_posts()) : $the_query->the_post(); wc_get_template_part( 'content', 'single-product' ); endwhile; } else { echo "There were no products found"; } } wp_die(); } add_action('wp_ajax_load_single_product_content', 'load_single_product_content'); add_action('wp_ajax_nopriv_load_single_product_content', 'load_single_product_content');
WooCommerce脚本“ add-to-cart-variations.js”:https : //searchcode.com/codesearch/view/95139130/