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”?
- Loading specific ajax from a product
- Update a server-side render Block when woocommerce cart block changed
- How to detect changes in WooCommerce store (react)?
- WooCommerce custom SVG coloring tool [closed]
- jQuery Ajax POST example with PHP
- Is it possible to add custom fields to a WooCommerce attribute term? [closed]
- Woocommerce My Account Endpoint – how to get ID parameter from URL?
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- Button to update order status Woocommerce with WC Marketplace
- WooCommerce: display text instead of raw checkbox value email
- How to create popup same as wordpress popup feature
- WooCommerce single products images doesn’t zoom on hover and doesn’t change clicking on gallery
- How to test nonce with AJAX – Plugin development
- How can I change the functionality of breadcrumbs?
- Variation name in $product->get_name()
- wc_get_products() not return the images details
- Adding Product Name on Admin Panel Order list and User’s My-Account Order List
- woocommerce_register_post action not firing [closed]
- Search in 1) Post, 2) Products (woocommerce) and 3) Custom post type or Taxonomy type on a single page
- Woocommerce REST API – add filter to set maximum query per page for product brands [closed]
- Woocommerce before and after shop loop not works
- WooCommerce Admin order – Only run action if user is loaded
- Creating child theme out of Coeur
- Change Out of Stock Error Message in WooCommerce Cart
- Anonymous users can not access website
- React to AJAX adding to the page
- WordPress post_content gets deleted in cron after wp_update_post
- wordpress site auto redirect to another site when I enable woocommerce
- Disable add to cart button when product is out of stock
- My WordPress site hacked with unwanted popups [closed]
- Woocommerce: Extra added country field not taking value from database
- Which php files, in a WordPress setup, do not need direct web access?