[resolved] Add to Cart Variation JS Lack Scope (4 posts)

  1. royho
    Plugin Author

    Posted 3 years ago #


    I am trying to customize a theme using Woocommerce and I have encountered a couple of hurdles.

    1. It seems the design of woocommerce in terms of AJAX add to cart is only allow simple products to do so.

    2. add to cart variation js is only loaded when in single products page so that prevents developers from using that on other pages via ajax unless we dequeue and enqueue it again without the is_product() check.

    3. add to cart variation js seems to have no scope because the original design was to function only on a single products page but this is a serious limitation because what if I wanted to utilize this js in other pages as stated on item 2 and i have say a grid of products. Without scope, the jquery selects would select all products in the grid instead of a specific product with scope. For example, if I click on a variation dropdown, it would trigger changes to all selects in all products instead of only the one I clicked...Again because it is lacking scope.

    Would appreciated if anyone could give some insights to these hurdles.


  2. Mike Jolley
    Plugin Author

    Posted 3 years ago #

    1/ Correct. We cannot allow other types which *could* require extra user input, like variations.

    2/3 Would it help if the scripts were always registered (but not enqueued)? As you said its only been designed for single variation page usage.

    Jump on github to discuss any potential changes or to submit core changes yourself https://github.com/woothemes/woocommerce We're more active on there than here, and this is more of a feature request than support.

  3. delatdecatsini
    Posted 3 years ago #

    For variations AJAX add to widget cart, just add ".product_type_variable" to the line #9 of add-to-cart.js(or add-to-cart.mini.js minified file) in plugins/woocommerce/assets/js/frontend, #9 become:

    if ($thisbutton.is('.product_type_simple, .product_type_downloadable, .product_type_virtual, .product_type_variable')) {

    then in template folder single-product/add-to-cart change line #64 become:

    <button type="submit" class="add_to_cart_button single_add_to_cart_button button alt product_type_<?php echo $product->product_type; ?>" data-product_id="<?php echo $product->id; ?>"><?php echo apply_filters('single_add_to_cart_text', __('Add to cart', 'woocommerce'), $product->product_type); ?></button>

    The problem is that the product name is add to widget cart but without name of variation follow. Hope anyone help.

  4. Shashank Shekhar
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic