2014-04-07 74 views
1

我正在编写一个插件,它将过滤“woocommerce_single_product_image_html”以显示产品的自定义图像。Woocommerce:产品变体的自定义图像

我无法通过jquery抓取下拉框的值。到目前为止,我已经尝试过:

jQuery(document).ready(function($) { 
    // Code here will be executed on document ready. Use $ as normal. 

    $('#color').change(function() { 
      var selected = $(this).children("option").filter(":selected").text(); 
      alert(selected); 
    }); 
}); 

我已经能够确认的是,上面的代码工作通过追加“.change()”改变的选择时,触发负载变化事件,但它不会触发下拉框。我的猜测是,这与woocommerece的核心变化事件相冲突。有关如何通过插件完成此任务的任何建议?

回答

5

所以,我终于追查到了这一点。其中一个woocommerce JavaScript文件(assets/js/frontend/add-to-cart-variation.js)发布了以下命令来解除所有更改事件,这就解释了为什么mine没有启动。

this.find('.variations select').unbind('change focusin'); 

我的解决方案是编写一个基本的插件来添加我的功能。幸运的是,woocommerce_variable_add_to_cart函数是可插入的,所以我能够复制它并调整它以加载我自己版本的add-to-cart-variation.js文件,而无需上述取消绑定行。这样,核心插件代码保持不变。

function woocommerce_variable_add_to_cart() { 
    global $product; 

    // Enqueue variation scripts 
    wp_deregister_script('wc-add-to-cart-variation'); 
    wp_register_script('wc-add-to-cart-variation', plugins_url('/js/add-to-cart-variation.js', __FILE__), array('jquery'), WC_VERSION, true); 
    wp_enqueue_script('wc-add-to-cart-variation'); 

    // Load the template 
    wc_get_template('single-product/add-to-cart/variable.php', array(
     'available_variations' => $product->get_available_variations(), 
     'attributes'   => $product->get_variation_attributes(), 
     'selected_attributes' => $product->get_variation_default_attributes() 
    )); 
} 

希望别人也认为此信息有帮助!

相关问题