Going by your method I would suggest hooking into wp_footer
as well, so that your script would be near the end of the body, that way it will be loaded after all DOM was loaded.
add_action('woocommerce_ajax_added_to_cart', 'set_button_classes');
function set_button_classes () {
add_action('wp_footer', function () {
echo "<script>document.getElementByClassName('added_to_cart wc-forward').classList.add('ct-cart-item', 'ct-offcanvas-trigger');</script>";
});
}
You could also close the php tag and reopen it in the wp_footer
anonymos function to make it easier to understand/see the scrip code
add_action('woocommerce_ajax_added_to_cart', 'set_button_classes');
function set_button_classes () {
add_action('wp_footer', function () {
?>
<script>
document.getElementByClassName('added_to_cart wc-forward').classList.add('ct-cart-item', 'ct-offcanvas-trigger');
</script>
<?php
});
}
Related Posts:
- Javascript / Ajax Error After Migrating To New Domain Name
- Running JavaScript after using WooCommerce Filter Plugin
- woocommerce deposit Form with AJAX and shortcode won’t update but add to previous output
- List of JS events in the WooCommerce frontend
- How to save Clicks of a download link button while it doesn’t matter if we refresh the page or again login/logout
- Hide Add to Cart Button
- WooCommerce add_to_cart() with custom price [closed]
- wc_get_product in a none woocommerce page
- WooCommerce add_to_cart url not working [closed]
- Mini cart in header using divi is not getting updated on changing the cart items count:
- How to make custom total price reactive in navigation
- How to stop a Gutenberg Block from firing multiple GET Requests?
- How to prevent those PHP variables from being cached on WordPress?
- Woocommerce Ajax Add cart not working
- “Cannot read property ‘replace’ of undefined” Javascript error when trying to add attribute terms from add product page [closed]
- woocommerce_package_rates not fired everytime
- Problems adding in-store product managers
- Status 400 for AJAX POST Request with admin-ajax.php
- How can i stop woocommerce checkout event based on api request result?
- Programatically Remove Item from Cart Not Working – Needs Page Refresh
- adding a script with type=”module” to woocommerce admin area
- Force uppercase on some WooCommerce checkout fields input
- In WooCommerce product archive, how can you also set the quantity to be added to cart by typing, instead of just by using +/-?
- WooCommerce update_shipping_method ajax call wipes shipping method set on non-checkout page [closed]
- WooCommerce Ajax Cart Update Stopped Working
- Javascript error when adding an item to the cart [closed]
- CF7 Ajax isn’t working when called inside WooCommerce “woocommerce_before_add_to_cart_button” hook [closed]
- ajax request in wordpress
- WooCommerce event works in jQuery but not in JavaScript
- Add to cart – button should be disabled
- Using the WordPress and WooCommerce REST APIs in the same Node app
- Woocommerce variable product’s total amount by javascript
- Ajax add to cart not updating Elementor mini cart totals
- List of JS events in the WooCommerce frontend
- Woocommerce Checkout Page Total Price Inside Google Pay Script
- WooCommerce variable product can not add to cart using ajax after updating latest version of WooCommerce
- call_user_func_array() expects parameter 1 to be a valid callback, class ‘WC_Rapyd’ does not have a method ‘install’
- Call product search from custom hook
- Deregister dequeue JS scripts except on 1 product page
- How to make wocommerce shop products same heigt
- Woocommerce Variation product added twice to cart
- woocommerce REST api on the website’s front-end
- Format number to 2 decimal places
- Woocommerce Multisite global search how to mod ajax function?
- Pass values to ajax
- Unable to call woocommerce hook in my custom php file
- Fragments to Update Cart Counter not working on homepage – Woocommerce
- Overriding Plugin function in your child theme
- Update / refresh mini cart, after custom AJAX add_to_cart event
- AJAX to add to cart multiple products woocommerce
- Can’t update WooCommerce payment method title
- currentURL is not defined
- Using category and its sub-categories products attributes as filter in products pages
- Retrieve Woocommerce Cart Url with javascript/jquery [closed]
- Removing Product from Woocommerce checkout page using Ajax
- How to echo a PHP code into WooCommerce variation product?
- add_filter not works in ajax
- SyntaxError: unterminated string literal – if WordPress Debug in wp-config is set to true
- How to insert link, with lightbox, that opens specific page content only
- How to add ajax loading icon after click add to cart button on woocommerce
- Woocommerce custom product with JS script, can’t understand how it works [closed]
- Removing ‘woocommerce-no-js’ class from body
- WordPress error
- Trigger a JavaScript function based on the data fetched from Woo commerce hook
- Remove Woocommerce product from cart with ajax/fetch
- WooCommerce Modal Fly cart on add to cart – With AJAX
- Woocommerce ajax search can’t work on mobile
- How do I fix this “Uncaught ReferenceError: woodmartThemeModule is not defined”?
- Turn jQuery.ajax() request into XMLHttpRequest (vanilla JavaScript)
- WooCommerce Shop Page Not Found 404
- How can I show an empty WooCommerce cart? [closed]
- Woocommerce Pre Orders – Can i override templates?
- Programmatically adding product attributes to WP / Woo commerce
- Display File Types For Woocommerce Downloadable Products On Product Page
- Programmatically setting Woocommerce product price
- Add product categories to receipt
- Woocommerce specific product template for product type
- How can I use a gravity form to send an e-card?
- Woocommerce Cart Default Language Not Changing
- Make this Fast and Queryable
- Reordering product tab under product image [closed]
- How do I change the default state label for a country on the WooCommerce checkout page?
- woocommerce – customise notice error in checkout page
- Woocommerce, prevent login after registration and redirect to custom URL
- load ajax using admin-ajax.php
- How to add product filter dropdown in woo commerce order page?
- AJAX Success Function using JS substr() fails in WP 4.1 vs 4.0
- Add Attribute to Woocommerce programatically, doesn’t display front end [closed]
- Woocommerce – Add additional item if some item wants to be added to the cart?
- Hook “wp_trash_post” executing multiple time in case of bulk trash products
- Woocommerce to get restaurant table numbers in the checkout fields from the url
- UX Builder not loading for product description edits
- Sending billing details via POST request to an API
- Tax exclusive if coupon applied else tax inclusive in items
- woocommerce: when adding or removing items to cart , it takes cart a few seconds to read changes?
- Product atributes in title of order (not in description)
- How to create custome pdf file of my invioice in wordpress
- How to get sub items of parent menu item?
- WordPress and WooCommerce How to Assign Attribute Based Pricing
- How do I remove the recurring total from thank you for your order email?