2

I would like to use ACF to store a variable which I then use in a wc_enqueue_js tag. If the variable is available via php as $couponCheck, I need to make it available as an array inside the 'updated_checkout' function. So then when the coupon is submitted it gets checked with the array.

Does that makes sense?

<?php $couponCheck = get_field('coupons');>
 wc_enqueue_js( "jQuery(function($) { 
 // On coupon change
 $(document.body).on('updated_checkout', function(){
 var couponArray = XXX;
 var couponCheck = $('.woocommerce-remove-coupon').attr('data-coupon');
 if(jQuery.inArray('couponCheck', coupnArray)) {
 $('.gift-with-coupon').show();
 }
 });
 });");
?>

Thanks.

asked May 18, 2021 at 16:00

1 Answer 1

2

You can use PHP json_encode function to convert PHP array to jQuery object.

Then you can use jQuery.inArray to check if couponCheck is present in the Javascript object.

$couponCheck = get_field('coupons');
wc_enqueue_js(
 "jQuery(function($) { 
 // On coupon change
 $(document.body).on('updated_checkout', function(){
 var couponArray = " . json_encode( $couponCheck ) . ";
 var couponCheck = $('.woocommerce-remove-coupon').attr('data-coupon');
 if(jQuery.inArray('couponCheck', couponArray)) {
 $('.gift-with-coupon').show();
 alert('Funziona.');
 }
 });
 });"
);

The code has been tested and works.

answered May 18, 2021 at 18:24
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vincenzo! This is perfect.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.