1

How can I run the following woodmart theme jquery script based on a php condition?

The jQuery script here asks for age validation on the website and restricts the page if there is no validation.

I just want to use this code for some category products but I don't know how to add condition to jQuery script and I am bad at javascript.

(function($) {
    woodmartThemeModule.ageVerify = function() {
        
        if ( typeof Cookies === 'undefined' ) {
            return;
        }

        if ( woodmart_settings.age_verify !== 'yes' || Cookies.get('woodmart_age_verify') === 'confirmed') {
            return;
        }
        

        $.magnificPopup.open({
            items          : {
                src: '.wd-age-verify'
            },
            type           : 'inline',
            closeOnBgClick : false,
            closeBtnInside : false,
            showCloseBtn   : false,
            enableEscapeKey: false,
            removalDelay   : 500,
            tClose         : woodmart_settings.close,
            tLoading       : woodmart_settings.loading,
            callbacks      : {
                beforeOpen: function() {
                    this.st.mainClass = 'mfp-move-horizontal wd-promo-popup-wrapper';
                }
            }
        });

        $('.wd-age-verify-allowed').on('click', function(e) {
            e.preventDefault();
            Cookies.set('woodmart_age_verify', 'confirmed', {
                expires: parseInt(woodmart_settings.age_verify_expires),
                path   : '/',
                secure : woodmart_settings.cookie_secure_param
            });

            $.magnificPopup.close();
        });

        $('.wd-age-verify-forbidden').on('click', function(e) {
            e.preventDefault();
            $('.wd-age-verify').addClass('wd-forbidden');
        });
    };

    $(document).ready(function() {
        woodmartThemeModule.ageVerify();
    });
    
})(jQuery);

UPDATE
The code here is working now, echo is no more, I also added 999 as priority and it works fine that way.

<?php

add_action( 'wp_footer', 'add_age_verify', 999 );
 function add_age_verify() {

 if( is_product_category( array( 4201, 4500, 4300 ) ) ) {

?>
<script type="text/javascript"> (function($) {
     woodmartThemeModule.ageVerify = function() {
        
        if ( typeof Cookies === 'undefined' ) {
            return;
        }

        if ( woodmart_settings.age_verify !== 'yes' || Cookies.get('woodmart_age_verify') === 'confirmed') {
            return;
        }
        

        $.magnificPopup.open({
            items          : {
                src: '.wd-age-verify'
            },
            type           : 'inline',
            closeOnBgClick : false,
            closeBtnInside : false,
            showCloseBtn   : false,
            enableEscapeKey: false,
            removalDelay   : 500,
            tClose         : woodmart_settings.close,
            tLoading       : woodmart_settings.loading,
            callbacks      : {
                beforeOpen: function() {
                    this.st.mainClass = 'mfp-move-horizontal wd-promo-popup-wrapper';
                }
            }
        });

        $('.wd-age-verify-allowed').on('click', function(e) {
            e.preventDefault();
            Cookies.set('woodmart_age_verify', 'confirmed', {
                expires: parseInt(woodmart_settings.age_verify_expires),
                path   : '/',
                secure : woodmart_settings.cookie_secure_param
            });

            $.magnificPopup.close();
        });

        $('.wd-age-verify-forbidden').on('click', function(e) {
            e.preventDefault();
            $('.wd-age-verify').addClass('wd-forbidden');
        });
    };

    $(document).ready(function() {
        woodmartThemeModule.ageVerify();
    });
    
   })(jQuery); </script>
<?php
  }
}
ersezar
  • 57
  • 6
  • You wrote *"it didn't work"*.. what was the error then? Maybe we can try to fix it. – Peter Krebs Jul 14 '22 at 15:19
  • In this way, while editing `Syntax error, unexpected T_ECHO` I am getting an error like, actually php uses javascript but it's something like a string error I guess that's it, I guess I'm missing something. – ersezar Jul 14 '22 at 15:32
  • 1
    [edit] your question to post the full error message and point out which line caused it (since we can't see your line numbers). Otherwise we're just guessing. In the meantime, some background reading for you: [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – ADyson Jul 14 '22 at 15:40
  • The site does not open and only such an error is encountered. Display errors enabled. Error: `Parse error: syntax error, unexpected 'echo' (T_ECHO) in /home/domain/public_html/wp-content/themes/woodmart-child/functions.php on line 1212` – ersezar Jul 14 '22 at 16:02
  • Line 1212 has `echo ' – ersezar Jul 14 '22 at 16:12
  • You can see from the code highlighting where the problem with our echo call is. If you write `echo '...'` you must escape all `'` inside the echo call with `\'`. – Peter Krebs Jul 15 '22 at 07:04
  • I no longer use echo and the output is given without any problems. – ersezar Jul 15 '22 at 07:35

2 Answers2

0

You can send a JQuery ajax call to the php script, the php script then sends it back to the javascript file and then you can easily use the variable within javascript.

$.ajax({
  url: 'path/to/your/php/file',
  type: 'get',
  success: (res) => {
   //do things when you get the response
  },
  error: (err) => {
   //do things when you get the error, error is optional
  },
})

or you can even simplify it

$.get('url/to/your/script', (res) => {
  //do things with the response
})
thijmende1
  • 18
  • 2
  • Currently there is such way for javascript, in php I will use the function in php, so what should I do? `/js/scripts/global/ageVerify.js` – ersezar Jul 14 '22 at 15:25
  • This is the action and condition I use in php. `add_action('wp_footer', 'add_age_verify', 10 ); function add_age_verify () { if( is_product_category( array( 4201, 4500, 4300 ) ) { // something }` – ersezar Jul 14 '22 at 15:27
  • Your answer is a correct way but I have no idea how to use it, if possible can you make me a suitable example with jQuery script and PHP condition? – ersezar Jul 14 '22 at 19:37
0

I copied the jQuery script into the child theme and then <script src='/wp-content/themes/woodmart-child/js/scripts/global/ageVerify.js'></script> to show the jQuery script, while doing this I added the conditions with PHP and now it works fine.

The final version of the code with PHP is like this.

<?php
  add_action( 'wp_footer', 'add_age_verify_jquery', 999 );
  function add_age_verify_jquery() {
    if ( has_term(array('jacket', 'fridge', 'hats', 'magic wand'), 'product_cat')) {
?>
      <script src='/wp-content/themes/woodmart-child/js/scripts/global/ageVerify.js'></script>
<?php
    }
  }
?>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
ersezar
  • 57
  • 6