Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

Need PHP Help On Woo Commerce Function

I have a routine I have developed with the help of some experts and it worked great on my test site.  But now it won't write a message in the Order Notes box of Woo Commerce.  I am using 3.5.4 and that is what is on the test site too.

The function with the help of jquery writes a cookie which I can verify is written each time.  If the cookie is present then it is supposed to put the message "I Want Membership" in the order notes box.  On my XAMPP install it works perfectly but not on my live site.  I have copied all plugins with the exception of the ssl plugins since my test site isn't running as an ssl enabled site.

This is the jquery

<script type="text/javascript">

jQuery(function() {
		jQuery('#cook').click(function(e) {
		e.preventDefault();
		document.cookie = "wantmembership=1; path=/";
              jQuery(this).css('background-color','green');
              jQuery(this).text("I Now Have A Membership!");
			});
	
		
	});

</script>

Open in new window


add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {

        //if (isset($_COOKIE['wantmembership'])) {
        if(isset($_COOKIE["wantmembership"]) && $_COOKIE["wantmembership"]== 1 ) {
//echo "found the cookie1";
    $fields['order']['order_comments']['default'] = 'I Want Membership';
        }
    return $fields;

}

Open in new window


I can see the echo statement but the message is never written into the order comments box.

Can anyone see what is wrong with this function?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

This sounds like a version issue - do you have the same version of relevant pluggins / WP / WC on your live site?

Have you checked the error log for errors?

If the echo's are working then the COOKIE is there so we can assume that somehow WC is not interpreting the fields correctly. That tends to indicate either an error is happening or a version conflict / dependency issue.
Avatar of sharingsunshine

ASKER

Both WP and WC are the same versions.  XAMPP's php is 7.2.8 and the live site PHP is 7.2.11.  Looking at the changelogs there doesn't seem to be that much difference.

XAMPP is a Bitnami WP install and I am not clear how to upgrade the php.  I am not opposed to it if you can tell me how.

Thanks,
It is very unlikely that PHP is the problem

Just to confirm - in you earlier code if you uncomment line 7
//echo "found the cookie1";

Open in new window

The echo displays?
yes, and I confirmed it still works with the upgrades
So this is a WooCommerce problem.

The setting of the $fields values is not carrying through to where it needs to.

According to this page (https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/)
There is no 'default' field - maybe that is the problem
In these docs they indicate there is a 'default' field.

https://stackoverflow.com/questions/45602936/woocommerce-set-checkout-field-values

Open in new window


https://jeroensormani.com/ultimate-guide-to-woocommerce-checkout-fields/

Open in new window


Also, it works fine using that code on the XAMPP system.

Can't find any errors in the logs.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not over valuing one vs the other.  I am simply puzzled it works on my local install.  I also will post a question with the WC authors.

Nevertheless in the meantime, I am going to try another option
https://www.kathyisawesome.com/woocommerce-customize-checkout-fields/

Open in new window

I looked at the WC code and there is no provision for 'default' for order_comments.  

I am going to go a different direction based on this answer I got before

https://www.experts-exchange.com/questions/29114316/Need-Javascript-JQuery-Button-Update-Woo-Commerce-Order-Notes.html

Using jquery it wrote in the order_comments box.  I just need to know how to test for the cookie in jquery so that will be my next question.  I hope you will participate.
I will take a look.
Thanks