-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Product id reading issue for variable product add-to-cart(AJAX) #451
Comments
Hi @sagarnasit :) |
Hi @tomalec I have tested on fresh setup with WooCommerce and Google Analytics for WooCommerce plugins. I noticed Apart from that, the only condition is to use AJAX for add-to-cart. I'm using following script to handle AJAX. jQuery(function ($) {
// Open external product in new tab
$( document ).find( '.product.product-type-external form.cart' ).attr( 'target', '_blank' );
const addToCart = function ( e ) {
e.preventDefault();
const form = $( this ),
button = form.find( '.single_add_to_cart_button' ),
quantity = form.find( 'input[type="number"].qty' ).val(),
isVariation = form.hasClass( 'variations_form' ) ?? false,
variationValue = form
.find( 'input[type="hidden"].variation_id' )
.val(),
attributeValue = form.find( 'select[name="attribute_amount"]' ).val();
const data = {};
data.product_id = isVariation
? Number( form.data( 'product_id' ) )
: Number( button.val() );
data.quantity = quantity ? Number( quantity ) : 1;
if ( isVariation && variationValue ) {
data.product_id = Number( variationValue );
}
if ( isVariation && attributeValue ) {
data.attribute_amount = attributeValue;
}
// Abort if don't have required data
if ( ! data.product_id || ! data.quantity ) {
return;
}
button.removeClass( 'added' );
button.addClass( 'loading' );
$( document.body ).trigger( 'adding_to_cart', [ button, data ] );
$.ajax( {
type: 'POST',
url: woocommerce_params.wc_ajax_url
.toString()
.replace( '%%endpoint%%', 'add_to_cart' ),
data: data,
success: function ( response ) {
if ( ! response ) {
return;
}
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
}
$( document.body ).trigger( 'added_to_cart', [
response.fragments,
response.cart_hash,
button,
] );
},
complete: function () {
form.unblock();
},
} );
};
$( document ).on( 'submit', '.product:not(.product-type-external) form.cart', addToCart );
} ); |
@tomalec Yes, it's a custom code that I'm using. I tried your suggestion but it did not work with the following error.
On a simple product, the add-to-cart button is expected to have a value attribute with the product_id. I tried the following, which worked for me, but I'm not sure if that's the correct approach.
|
Sorry, I was using the native DOM API. To set the dataset for the jQuery object, you need to use jQuery API: button.data( 'product_id', data.product_id ); |
@tomalec It's showing the same error using jQuery API too. |
Maybe then stick to vanilla API and just fetch the native object out of the jQuery collection: button[0].dataset.product_id = data.product_id; |
I'm closing this issue as answered. If it still does not work, feel free to reopen. |
Describe the bug:
The plugin displays an error when clicking the add-to-cart button for the variable product. This occurs only when using the ajax add-to-cart button.
Steps to reproduce:
Expected behavior:
The plugin should be able to read the product id.
Actual behavior:
The plugin is not able to read product id for variable products.
Additional details:
Plugin cannot retrieve the product id from the button element for a variable product because the variable product id is set via a hidden input rather than the button value attribute.
Screenshot:
The text was updated successfully, but these errors were encountered: