Hi,
This might be a basic question but not sure how to do the following:
I have a button inside of woocommerce up-sell product query loop (which works just fine). The following php is not returning the product_id of the associated product, so I can add to cart.
Console echo says post_id is not defined.
<?php
add_action( 'wp_footer', 'update_add_to_cart_button_text' );
function update_add_to_cart_button_text() {
?>
<script>
jQuery(document).ready(function($) {
$('.prod-up-sell__btn').click(function(e) {
e.preventDefault();
var button = $(this);
var productID = {post_id};
console.log('Product ID:', productID);
Instead of the dynamic data format above, I also tried
var productID = button.closest('.bricks-loop-item').data('product-id');
but that too did not work. Console says that the productID is undefined.
Ok - Do you know of a better way to add to cart on button click from a query loop (and also display a notice that itâs been added)? I canât be the first person to try to do this.
I could of course just make the button link //your-site/add-to-cart={post_id)? but then I wonât get the visual success notice Iâd like
Thank you @digismith - I really appreciate you sending that link.
I got the JS working, all good. Adds to cart, shows the âview cartâ link.
Strangely the CSS isnât working thoughâŠ
In WPCodeBox, I replaced the â.rootâ class with a different custom class I assigned the button in bricks. Nothing displays / animates on load or added.
I canât see your markup/structure, but normally ROOT is a pseudo selector for âmain ID of elementâ - not a class.
So if I have a button, and in Bricks the element has id #brxe-abcde
Root = #brxe-abcde
Which suggests you should change root to the ID of the element you wish to target (as it shows in bricks), not a class.
Sure, makes sense. So just to be parallel with the website example, I put the code back into the css style area on the button. I tried changing root to the element ID as suggested but bricks is smarter than that and just turns it back to root. No change in behavior. (i.e., no icon spin). I also tried it in WPCodebox with the #brxe-abcdef.loading ⊠(I did replace abcdef with the correct id) and that didnât have effect either.
And your icon has the class â.loadingâ applied to it? And somewhere else you actually define the âspinâ animation?
Frankly this looks a bit fiddly for me to understand the structure, and thus target class. I would view the page, right-click, inspect and find the button / icon, double check what class is applied to it.