I've got the following function that modifies the checkout fields in WooCommerce. The code works fine in PHP 7.0 but after upgrading to 7.2 the checkout page just displays a white screen.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['label'] = 'Mobile number';
$fields['billing']['billing_address_2']['label'] = ' ';
$fields['billing']['billing_first_name']['class'] = '';
$fields['billing']['billing_last_name']['class'] = '';
$fields['shipping']['shipping_first_name']['class'] = '';
$fields['shipping']['shipping_last_name']['class'] = '';
return $fields;
}
If I comment out some of the $fields[ ... lines the page works again, however it seems to be random which ones I comment out to make it work!
First of all: Why does this upset PHP 7.2? I'd really like to understand why this breaks the page.
Second: How can I make this customisation work in PHP 7.2?
-
FYI: My testing environment only has the WooCommerce plugin activated and is using the Twenty Seventeen theme
Related
I have developed a payment gateway for WooCommerce. The thing is, when I developed it, the version of WooCommerce was 4.7.0 (It still is in my localhost) but in the production server the version is 5.1.0. I had to use a webhook like so:
function webhook() {
$data = json_decode($this->df_check_payment(), true);
$code = $data['result']['code'];
$description = $data['result']['description'];
if ($code != WC_Gateway_Datafast::DF_SUCCESS_CODE){
wc_add_notice( __("Payment failed -> Code: $code Description: $description" , 'gateway'), 'error' );
wp_safe_redirect( wc_get_page_permalink( 'cart' ) );
} else{
$orderId = $data['customParameters']['SHOPPER_documentoId'];
$order = wc_get_order( $orderId );
$order->set_transaction_id($data['id']);
$order->payment_complete();
wp_safe_redirect($this->get_return_url($order));
}
}
This works fine in localhost and the wc_add_notice is shown in the cart page but it's not working in production. I've tried with wc_get_page_permalink( 'checkout' ) but it doesn't work either, the notice is not shown. My guess is it has to do with the WC version but if that's the case, what's wrong? Did the API change? I really couldn't say if it was working in production with version 4.7.0 because I developed it before someone decided to update the production's wc plugin.
EDIT: I have updated the WC version in localhost and it works fine. Production still doesn't work. I thought maybe a plugin was putting something on top of the notice so it wasn't visible. I examined the HTML looking for the text in the notice but it's not there, which means it's not being generated at all.
This is how it looks in localhost (notice shows just fine):
And this is production (no notice whatsoever):
I am working on a website running WordPress and woocommerce
I have a custom function to add stuff to the cart which is the following:
function custom_add_products_to_cart(){
WC()->cart->empty_cart();
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
This is quite a straightforward function. All it does is iterating through a list of products id and launching the default Woocommerce add to cart function.
The issue I've got is that this works perfectly fine on my localhost (also debugging it ut behaves just like expected) but when I try it on a test server it doesn't work.
The function is firing (i tried to print some messages) but the cart is not emptying and the new products are not added.
I check the code, commit and revision, and everything is correct.
What else can it be?
My last thought was on the version of PHP:
my localhost runs 7.1.2 while the test server runs 7.0.22 - can it be the PHP version or not? any idea on what else I could try?
Sorry if I cannot provide much more details but unfortunately there's not much more to add...
Also, I am not posting this to WordPress community for now as I think it's not a WordPress related stuff (nor woocommerce) but rather PHP code (maybe my function is somehow wrong?) or PHP version
Thanks in advance to everyone
i think the problem is here
file_get_contents('php://input');
depending on your PHP configuration, maybe you need to change the allow_url_fopen setting in you php.ini.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.
I recommend using cURL over file_get_contents() anyways, since it was built for this.
At the end i simply solve this by calling two functions:
the first one empties the cart:
function empty_cart(){
WC()->cart->empty_cart();
}
and the second one carries on adding to the cart:
function custom_add_products_to_cart(){
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
and it now works properly.
I am developing a custom plugin for woocommerce. For now i support it for few version of woocommerce. So i want to check and show incompatibility error if some is using lower version of woocommerce than the version i minimal support.
I want to show the error message on plugin page in admin panel under the my plugin listed.
I have function to get woocommerce version and checking incompatibility using if else condition. But i have no idea how to display the error message as i want.
So please help.
Thanks in Advance.
This is how I do it in my own plugin:
add_action( 'plugins_loaded', 'so_31217783_version_test' );
function so_31217783_version_test(){
$required_woo = '2.1.0';
if ( ! defined( 'WC_VERSION' ) || version_compare( WC_VERSION, $required_woo, '<' ) ) {
add_action( 'admin_notices', 'so_31217783_admin_notice' );
return false;
}
// add the rest of your actions here
// they will only be triggered if the
// version test has been passed
}
function so_31217783_admin_notice() {
echo '<div class="error"><p>' . sprintf( __( 'My custom plugins requires at least WooCommerce version %s in order to function. Please upgrade WooCommerce.', 'your-custom-function' ), $required_woo ) . '</p></div>';
}
The base explanation is that you check the version of WooCommerce very early on and then shut down your plugin if the minimum version is not met. You also add a function to the admin_notices hook so that you can tell the user what has happened.
I am a WP noob but very comfortable in PHP.
I am working with a client and we have built a product customization tool as an Angular.js single page application. When the product is finished being customized we are seeking to inject it into a WooCommerce cart so the client can check out. To do this we are $_POSTing the data to a PHP file in the root directory of the WP install. The code to catch it looks like:
require_once('./wp-load.php' );
global $woocommerce;
$woocommerce->session->set_customer_session_cookie(true);
$woocommerce->cart->empty_cart();
$id_arr = $_GET['productID'];
$pdfName = $_GET['pdfName'];
for($i=0; $i<count($id_arr); $i++){
$id = $id_arr[$i];
if ($id==0) continue;
if ($i==0){
$ret = $woocommerce->cart->add_to_cart($id, 1, '', '', array('pdfName'=>$pdfName));
}else{
$ret = $woocommerce->cart->add_to_cart($id);
}
}
wp_redirect(site_url().'/cart/');
The products are all correctly added to the cart but after checkout there is no sign of the metadata. After extensive research, I have found an article here: https://wpml.org/forums/topic/woocommerce-add-to-cart-does-not-work-with-wpml-activated/ that shows me that plugins can cause this behavior. So I have two specific questions?
Does my code make sense, am I creating the metadata array correctly?
Do I need to create something in WooCommerce called pdfName before I can do this?
Is there another way that metadata can be added to an order in
WooCommerce that may work around this problem?
It looks like you are adding the metadata correctly. However, as soon as you refresh, WooCommerce re-creates the cart data. Therefore you have to tell WooCommerce to maintain the metadata when it is pulling the cart from the stored session. Well, at least that is my understanding of it. So I think you need to filter thee $cart_item as it is run through the woocommerce_get_cart_item_from_session filter:
add_filter( 'woocommerce_get_cart_item_from_session', 'so_29660316_get_cart_item_from_session', 11, 2 );
function so_29660316_get_cart_item_from_session( $cart_item, $values ) {
if ( isset( $values['pdfName'] ) ) {
$cart_item['pdfName'] = $values['pdfName'];
}
return $cart_item;
}
So here i find myself, again, struggling with wordpress wysiwyg editor.
a client of mine requested to migrate his website to WP. No probs, a breeze :).
Really was easy, migrated from one DB structure to the other, and everything went OK.
Now I have a problem. The old site, used an editor that added <br> and <p> tags to the content in order to format it (sounds legit to me). But wordpress will not allow these tags. whenever the client tries to edit a post, WP removes all the HTML tags it considers "illegal".
So I went on the search. First I tried to install some recommended plugins I found for this problem (such as this one). Didn't work at all for me (for some others it did i believe)...
Then I found a post that told me to add a function to the function.php file which will remove the filters :
function mod_mce($initArray) {
$initArray['verify_html'] = false;
return $initArray;
}
add_filter('tiny_mce_before_init', 'mod_mce');
and also this:
function my_tinymce( $init ) {
$ext = 'div[id|name|class|style]';
if ( isset( $init['extended_valid_elements'] ) ) {
$init['extended_valid_elements'] .= ',' . $ext;
} else {
$init['extended_valid_elements'] = $ext;
}
return $init;
}
add_filter( 'tiny_mce_before_init', 'my_tinymce' );
functions from this thread.
Nope, didn't work also...
Someone - any idea? It seems so silly, but there is so much debate around this subject...
Thanks
You may try this to remove filters that wpautop uses to filter content and excerpt, just put these in your funcions.php file
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
Reference: WordPess wpautop
To allow older content with p and br to load in tinyMCE
function my_tinymce_config( $init ) {
$init['remove_linebreaks'] = false;
$init['convert_newlines_to_brs'] = true;
$init['remove_redundant_brs'] = false;
return $init;
}
add_filter('tiny_mce_before_init', 'my_tinymce_config');
Reference: tinyMCE Configuration look at Cleanup/Output and try playing with these.
Another way could be helpful Reference
tinyMCE.init({
...
verify_html : false
});