Refresh checkout in woocommerce_after_checkout_validation - wordpress

A client is asking to allow users to register during checkout. The problem is there are some logic about memberships so registering can alter the final price and I can't use the standard Woocommerce way.
So i wrote a couple of validations. First i check if everything is in order, if it is a second validation actually creates the customer account, set all meta fields, login the customer.
Now what I need is to fully realod the checkout page and output a notice telling the customer to press the checkout button again.
I'm using woocommerce_after_checkout_validation but I can't make it work.
I tried a wp_redirect and outputting an ugly in the error notice.
Any ideas on how to do this?

I ended up using Javascript hooks to fire an event after the validation. The error checking is pretty bad, in my case it works but it should be more general.
var error_text = jQuery(".woocommerce-error").find("li").first().text();
if (
error_text.trim() ==
"register_success"
) {
window.setTimeout(function () {
location.reload();
}, 1500);
}
});

Related

Unsubscribe Reason Not Working in Mailchimp when using MailChimp.Net Library

I am using mailchimp.net library (https://github.com/brandonseydel/MailChimp.Net) to use mailchimp api. And everything is working fine except that the unsubscribe reason does not work when i update the member.
for example:
Under certain event i am using following code to unsubscribe:
existingMember.Status = Status.Unsubscribed;
existingMember.UnsubscribeReason = "Contact Unsubscribed from contact page" ;
//***Notice the Unsubscribe Reason here, i have different pages which has different unsubscribe reason***
await _mailChimpManager.Members.AddOrUpdateAsync(listUserSubscribed, existingMember)
.ConfigureAwait(false);
Now, Next i created webhook in mailchimp as explained here (https://mailchimp.com/developer/marketing/guides/sync-audience-data-webhooks/#create-a-new-webhook). However the unsubscribe reason is always "manual" and it doesnt shows the note that i entered earlier (i.e. Contact Unsubscribed from contact page) .
Is there anything i am missing? or is there any other way to achieve the same?
Thanks in advance !!!

Gravity Forms call by ajax displays form fields as disabled

When i call Gravity Form in my theme's functions.php using ajax the form fields are disabled.
Any solutions will help me a lot.
add_action('wp_head', 'mytheme_fn');
add_action('wp_ajax_mythem_load_gravityform', 'mythem_load_gravityform_call');
function mytheme_fn(){
?>
<script>
jQuery(document).ready(function () {
var data = {
action: 'mythem_load_gravityform'
};
jQuery.post(
ajaxurl,
data
).success(function (data) {
jQuery('.gformholder').html(data);
});
});
</script> <?php }
function mythem_load_gravityform_call(){
gravity_form(1, false, false, false, null,false);
exit;
}
Thank you in advance.
From a Gravity Forms Support Specialist:
"The issue is caused by GF's implementation of the form rendering logic. They check whether is_admin() returns true (and it does when you call ajax on Wordpress) and if so, they render it like you were in admin."
We have made a number of changes in Gravity Forms 1.9 so and is_admin check is no longer performed when loading forms and fields, which means forms and fields can now be loaded via AJAX and they will render correctly.
Gravity Forms 1.9 has just been released: http://www.gravityhelp.com/gravity-forms-v1-9-released/
Regarding the enhanced ui, this is powered by the chosen.js script and when you make change to the underlying select element you will need to trigger an update of the chosen interface: http://harvesthq.github.io/chosen/options.html#triggerable-events
The issue is caused by GF's implementation of the form rendering logic. They check whether is_admin() returns true (and it does when you call ajax on Wordpress) and if so, they render it like you were in admin.
I can think of two solutions here:
1) dummy endpoint
create a dummy page with template file without head or foot, just a plain gravity_form(...);
Then call this url from your js.
2) plugin
create a plugin, which registers custom query var (using query_vars filter), and then process the query variable by showing the gravity_form(...) output.

Add Validation to Custom User Meta on Profile Page of Wordpress Admin

I want to extend the existing user roles and capabilities of wordpress. I added custom roles and capabilities. I have also added custom user meta (dob, gender, status, etc...)
Everything is good, except, I want that if a user or the admin tries to update the custom user meta, it should be validated via php, I can do it via JS but I want to add validation to PHP as well.
personal_options_update and edit_user_profile_update action hooks,
using this, I can successfully update the values of the custom user meta, but cannot validate. (I want to display validation errors, just like wordpress does normally).
so I tried this technique from this link:
https://wordpress.stackexchange.com/questions/76419/validating-a-new-user-registration-field
and used the user_profile_update_errors action hook.
It does most of the job, I can add validation and add it to wordpress error notification. and if there are errors prevent update of custom user meta.
however I noticed is that even if the new data is valid and no validation error occured, it doesn't update the custom user meta.
below is a sample of my codes:
call back to the user_profile_update_errors action hook, I intentionally did not check if the $errors object has errors to illustrate the somehow the update_user_meta does not get triggered. also this is a function of a class
public function updateCustomUserMeta($errors,$update,$user){
// Check if user has authority to change this
if(!current_user_can('read',$user->ID)){
$errors->add("permission_denied","You do not have permission to update this page");
}
// Var declaration and default values
$mn = "";
// Validation
if(isset($_POST["alumni-middlename"])){
$mn = trim($_POST["alumni-middlename"]);
if(empty($mn)){
$errors->add("middlename_empty","Middle Name Empty");
}
}else{
$errors->add("middlename_not_passed","Middle Name Field Not Passed");
}
update_user_meta($user->ID,"Middle Name",$_POST["alumni-middlename"]);
}// end updateCustomUserMeta
and this is my plugin in bootstrap code:
add_action( 'user_profile_update_errors',array(AlumniUserRolesAndCapabilities::getInstance(),"validateCustomUserMeta"));
To sum up, I want to be able to validate the custom user meta via php and prevent updating of values if there are errors and display it appropriately normally just like what wordpress does. Also if there are no validation errors, udpate the specified custom user metas.
Thanks.
add_action( 'user_profile_update_errors',array(AlumniUserRolesAndCapabilities::getInstance(),"validateCustomUserMeta"),10,3);
thats the answer, the 10 and 3 are the key, it has something to do with the order of execution.
before without those numbers, $user was not sent to the callback, but when I added those numbers at the end, the $user object was passed.
Hope this helps anyone.

Woocommerce: disable payment method with cost on delivery

I have a problem when I select cost on delivery as shipment method: the paypal button doesn't disappear, so the user could pay twice and this is a undesirable behaviour.
I've attached an image to clarify my problem because I'm searching the web but I don't find the solution.
Thanks
A good way how to do this is through jQuery. Put inside the of your document the following
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Then find the file where the checkout page is setup and enter the following:
$(document).ready(function() {
if ($('input[name=your_radio_name]:checked').val() == "the value when the element should be hidden") {
$('#id_of_the_element_that_should_be_hidden').hide();
});
});

Google Analytics events fail to show up

I'm trying to get Google Analytics working on my blog, but the events are failing to show up in my account.
Part of the issue is that I'm using the new async code, but most of the documentation relates to the older synchronous version. I've read a bunch of SO and blog posts but can't quite see what I'm doing wrong.
One mentions that you have to "activate" event tracking in your site's profile, but I can't find where I might do that.
I originally put my Google Analytics code in an external file called ga.events.js, which is still linked from my site. I attached the events from a jQuery loaded event:
$(function () {
$('.rss').click(function() {
trackEvent("engagement", "rss subscribe", "rss subscription link clicks");
});
function trackEvent(category, action, label) {
_gaq.push(['_trackEvent', category, action, label]);
}
});
But I found a post that said you can't link in an external file for Google Analytics, so I also tried the old fashioned onclick approach:
email list
I put the _target="blank" attribute in case the request wasn't completing before the user navigated away from the page.
Executing the code in the Chrome console on my site returns a 0, when I was expecting a boolean value:
_gaq.push(['_trackEvent', 'Engagement', 'click', 'RSS subscription link'])
I've waited 24 hours after each of these tests to see if the event tracking wasn't real time.
What else should I try? Any obvious mistakes?
Figured it out. I had onclick instead of onClick with a capital C. Wait, JavaScript is a case sensitive language? Who knew?
I also had the syntax of the GA call incorrect.
Here's what my working code looks like:
email list
And here's a version that works using jQuery to attach the click handlers:
$(function () {
$('.rss').click(function() {
trackEvent('Engagement', 'Sidebar link click', 'RSS feed subscription');
});
function trackEvent(category, action, label) {
_gaq.push(['_trackEvent', category, action, label,, false]);
}
});

Resources