Woocommere checkout page error? - woocommerce

I'm trying to find the origin of this error but i can't find it.
This error randomly shows up on the checkout page, any idea what could be causing this?

If you not neccesary to use postal code you can disable them by adding this function to yout functions.php
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}

Related

Fire function when Stripe has error in WooCommerce

I am trying to fire a custom function when a credit card error occurs in the WooCommerce checkout flow.
I seem to be able to get standard woo errors by using but it seems that the stripe plugin does not us woocommerce_add_error
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );
I found the following hook in the WooCommerce Stripe documentation, but I can't seem to get it to work with add_action
wc_gateway_stripe_process_payment_error ($error, $order) – Called when
an error occurs during the process payment event.
add_action( 'wc_gateway_stripe_process_payment_error', 'test_this' );
What am I missing? Is there another filter or hook that I should be using?
As per documentation https://docs.woocommerce.com/document/stripe/ this hook take 2 params $error, $order so you have to tell it to WordPress:
add_action( 'wc_gateway_stripe_process_payment_error', 'test_this', 10, 2 );
function test_this($error, $order) {
// something
}
It's working fine on my side
The only way I could figure out how to do this is by watching for dod inserts on the dom. This is the code I used below.
Note: You should probably use mutated events these days but for some reason, I couldn't figure it out.
$(document).on('DOMNodeInserted', function(e) {
if ( $(e.target).hasClass('woocommerce-error') ) {
console.log('stripe input error');
}
});

Wordpress : add_action not working in plugin or functions.php

I've got two functions:
add_action( 'woocommerce_order_status_completed' , 'after_order_complete' , 10);
function after_order_complete( $order_id )
{
error_log("woocommerce_order_status_completed", 0);
}
add_action( 'wp_login', 'after_login', 10, 2);
function after_login( $user_login, $user ) {
error_log("wp_login", 0);
}
that I can't seem to get called, tried in a plugin and functions.php but they never fire and no errors are logged.
How is it I debug this to find out why they fail or does anyone know how to get these to work?
In the same code I have another add_action but this time using user_register and that works as expected from plugin or functions.php
Edit:
I have the wp_login figured out, action does get called but only when first visiting the back end, site is front end only for users so used membership system hook instead.

Remove YouTube embedding with wp_oembed_remove_provider()

I am trying to remove YouTube embedding from some of my WordPress output, so it only shows the link.
I have tried the following code without any luck.
wp_oembed_remove_provider('#http://(www\.)?youtube\.com/watch.*#i');
wp_oembed_remove_provider('#https://(www\.)?youtube\.com/watch.*#i');
wp_oembed_remove_provider('#http://youtu\.be/.*#i');
wp_oembed_remove_provider('#https://youtu\.be/.*#i');
echo apply_filters("the_content", $result->text);
What am I doing wrong? And how am I doing this correctly?
Chances are you're at a point where it's too late to remove the providers. Try hooking into plugins_loaded or init:
add_action( 'init', 'so26743803_wp_oembed_remove_provider' );
function so26743803_wp_oembed_remove_provider(){
wp_oembed_remove_provider('#https://youtu\.be/.*#i');
// etc.
}
Edit (untested): unsetting providers with the oembed_providers filter (see this Q):
add_filter( 'oembed_providers', 'so26743803_oembed_providers' );
function so26743803_oembed_providers( $providers )
{
unset( $providers['#http://youtu\.be/.*#i'] );
// etc.
return $providers;
}

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Resources