8000 [BUG] Duplicate wc-ajax=add_to_cart · Issue #532 · bootscore/bootscore · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[BUG] Duplicate wc-ajax=add_to_cart #532

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

Closed
memeq1 opened this issue Jul 19, 2023 · 5 comments · Fixed by #754 or #694
Closed

[BUG] Duplicate wc-ajax=add_to_cart #532

memeq1 opened this issue Jul 19, 2023 · 5 comments · Fixed by #754 or #694
Labels
ajax cart bug Something isn't working WooCommerce

Comments

@memeq1
Copy link
memeq1 commented Jul 19, 2023

Describe the bug

bootscore to v5.3.1. After update i have duplicate request wc-ajax=add_to_cart. I see this same problem in offical site and dev.

Steps to reproduce

  1. Go to https://bootscore.me/shop/
  2. Click add to cart button
  3. Go to developer console
  4. duplicate ?wc-ajax=add_to_cart

Screenshots and Additional Info

image

rec-screen.8.webm

Website link

https://bootscore.me/shop/

@memeq1 memeq1 added the bug Something isn't working label Jul 19, 2023
@crftwrk
Copy link
Member
crftwrk commented Jul 20, 2023

Thank you for reporting. Checked last 3 releases and AJAX request gets twice on WooCommerce 7.8 and bootScore 5.3.0.

As described in https://developer.woocommerce.com/2023/05/24/woocommerce-7-8-beta-1-released/, WooCommerce removed the global

wp_enqueue_script('wc-cart-fragments');

for performance reason. Because our mini-cart is hard-coded in the header.php and not in a widget position the script has been added to the theme to keep AJAX functionality as recommended in linked blog post in bootScore v5.3.0.

8000
//Scripts and Styles
function wc_scripts() {
// Enable fragments script (disabled in WooCommerce 7.8.0 if mini-cart widget is not in a widget position)
// See https://developer.woocommerce.com/2023/05/24/woocommerce-7-8-beta-1-released/
wp_enqueue_script('wc-cart-fragments');
// Get modification time. Enqueue files with modification date to prevent browser from loading cached scripts and styles when file content changes.
$modificated_WooCommerceJS = date('YmdHi', filemtime(get_template_directory() . '/woocommerce/js/woocommerce.js'));
// WooCommerce JS
wp_enqueue_script('woocommerce-script', get_template_directory_uri() . '/woocommerce/js/woocommerce.js', array(), $modificated_WooCommerceJS, true);
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
}

So, v5.3.0 ships the updates for WooCommerce 7.8 and for now, I have no idea why this is happen and how to solve that. But I think this is a minor issue and we can skip that for now. WooCommerce will for sure do improvements there.

Edit:

Edit:

It's something here:

$('a.ajax_add_to_cart').on('click', function (e) {
e.preventDefault();
$('.woocommerce-error, .woocommerce-message, .woocommerce-info').remove();
// Get product name from product-title=""
let prod_title = '';
prod_title = $(this).attr('product-title');
$(document.body).trigger('adding_to_cart', []);
let $thisbutton = $(this);
let href = '';
try {
href = $thisbutton.prop('href').split('?')[1];
if (href.indexOf('add-to-cart') === -1) return;
} catch (err) {
return;
}
let product_id = href.split('=')[1];
let data = {
product_id: product_id
};
$(document.body).trigger('adding_to_cart', [$thisbutton, data]);
$.ajax({
type: 'post',
url: wc_add_to_cart_params.wc_ajax_url.replace(
'%%endpoint%%',
'add_to_cart'
),
data: data,
complete: function (response) {
$thisbutton.addClass('added').removeClass('loading');
},
success: function (response) {
if (response.error & response.product_url) {
console.log(response.error);
} else {
$(document.body).trigger('added_to_cart', [
response.fragments,
response.cart_hash,
$thisbutton
]);
console.log('Error-: ' + response.error);
//Remove existing notices
$('.woocommerce-error, .woocommerce-message, .woocommerce-info').remove();
let notice = '';
if (response.error == true) {
let message = `<?= sprintf(__('You cannot add another "%s" to your cart.', 'woocommerce'), '{{product_title}}') ?>`;
notice = `<div class="woocommerce-error">${message.replace('{{product_title}}', prod_title)}</div>`;
} else {
let message = `<?= sprintf(_n('%s has been added to your cart.', '%s have been added to your cart.', 1, 'woocommerce'), '“{{product_title}}”') ?>`;
notice = `<div class="woocommerce-message">${message.replace('{{product_title}}', prod_title)}</div>`;
}
// Add new notices to offcanvas
setTimeout(function () {
$('.woocommerce-mini-cart').prepend(notice);
}, 100);
}
}
});
});

@memeq1
Copy link
Author
memeq1 commented Aug 8, 2023

Yes, this click event causes wc-ajax to be called twice. I modified/removed the a.ajax_add_to_cart event. Works normaly

@crftwrk
Copy link
Member
crftwrk commented Aug 8, 2023

Do you have removed the entire click event?

@memeq1
Copy link
Author
memeq1 commented Aug 8, 2023

Yes, I'm missing the notification in the mini cart, but I don't have any duplicate quantity and call add_to_cart. I keept only triggers

                offCanvasCart.addClass('loading').offcanvas('show');
            })

            $(document.body).on('added_to_cart', function(){
                offCanvasCart.removeClass('loading');
           
            });

           ```

@crftwrk crftwrk added ajax cart wontfix This will not be worked on labels Aug 9, 2023
@crftwrk
Copy link
Member
crftwrk commented Aug 9, 2023

Ok, got it. Of course, the alerts have an AJAX request as well. Why this is now twice since WooCommerce dropped the wc-cart-fragments is mystic, but I think we have to accept this.

Again, thanks for reporting

@crftwrk crftwrk closed this as completed Aug 9, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in v6.0.0-beta1 Aug 9, 2023
@crftwrk crftwrk reopened this Apr 25, 2024
@crftwrk crftwrk removed the wontfix This will not be worked on label Apr 25, 2024
@crftwrk crftwrk moved this from Done to Todo in v6.0.0-beta1 Apr 25, 2024
@crftwrk crftwrk linked a pull request Apr 26, 2024 that will close this issue
@crftwrk crftwrk mentioned this issue Apr 26, 2024
@crftwrk crftwrk moved this from Todo to In Progress in v6.0.0-beta1 Apr 26, 2024
@crftwrk crftwrk moved this from In Progress to Todo in v6.0.0-beta1 Apr 26, 2024
6EBA @crftwrk crftwrk moved this from Todo to Done in v6.0.0-beta1 Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ajax cart bug Something isn't working WooCommerce
Projects
No open projects
Status: Done
2 participants
0