• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

QUESTION Code Working Directly in file but when added to snippet and snippet added to file code isn't working properly

Messages
2
Likes
0
Points
1
#1
Alright so I have this custom php code in my woocommerce order-details.php template file. It works fine. So I decided this will be a pain in the ass every time I want to change the freebies out. So I added it to woody. Then I added this to my order-details.php file:

PHP:
<div style="font-size: 18px; font-weight: bold;"><?php echo do_shortcode('[wbcr_php_snippet id="2850"]'); ?></div>
So now when the email gets sent it errors out with a floatVal of 0 everytime. I wanted to try it a different way like removing the html_entity_decode and adding &#36; to my pattern to see if that works better but adding &#36; immediately converts it into a dollar sign and thus doesn't strip the code correctly and errors out with a float of 0 as well.

Any help with this is most appreciated I'm still new with php and am having troubles with this.

Heres my Woody PHP Code Snippet:

PHP:
$totalVal = preg_replace('/[^0-9.]/u', '', html_entity_decode($total['value']));

        if ($totalVal > 0 && $totalVal <= 99.99 ){

            echo 'For Spending $' . $totalVal . ' you have earned your self a free Blah';

            

        } elseif ($totalVal >= 100 && $totalVal <=149.99 ){

            echo 'For Spending $' . $totalVal . ' you have earned your self a free Blah' ;

        

        

        } elseif ($totalVal >= 150){

        echo 'For Spending $' . $totalVal . ' you have earned your self a free blah' ;

        

        } else {

            echo 'Error! Please notify site administration about this error!';

            var_dump($totalVal);

        }
 
Messages
2
Likes
0
Points
1
#3
Well the total varaible contains two items so technicall an array but you could see that. Umm 1 slot is a label with a static value of Total then the second is a string with some html that is the total sum of all line items. Im not sure but is it possible the snippet isnt getting access to the php files variables or maybe the snippet is processed then inserted to the file and then file processed?
 

Kirill

Support team
Staff member
Messages
22
Likes
0
Points
1
#4
Hello

Im not sure but is it possible the snippet isnt getting access to the php files variables
It's true.

In fact, the snippet code is executed inside the callback function with its own scope.
You can pass variables as shortcode attributes. This is suitable for simple data types such as strings and numbers.
Example [wbcr_php_snippet id="xxx" simple="example"]
The attribute “simple” is available in the snippet as the $simple variable. The attribute name can contain Latin lowercase letters, digits and underscore.

Or, alternatively, you can use keyword global.

Best regards