I would like to REMOVE completely the wc_empty_cart_message. My site has no cart. and when the item is removed from checkout the user is redirected to the home page. But then upon browsing to a shop page, the message "your cart is not available whilst your checkout is empty" appears and is completely unnecessary.
I have seen many of questions/answers about how to change the message to something else (I tried some of them which also did not seem to work).
I tried this which seems to be the right thing to do, but for some reason it does not make any visible change on my website.
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
Is there any way to check which hook is creating the message from the front-end? Or any way how I can see what is overruling this command from executing?
To remove the "Your cart is currently empty" message, use:
function action_woocommerce_cart_is_empty() {
// Remove
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
}
add_action( 'woocommerce_cart_is_empty', 'action_woocommerce_cart_is_empty', 9, 0 );
Where the remove_action in an add_action with a lower priority number (9)
To remove the "Checkout is not available whilst your cart is empty" message, use:
function filter_woocommerce_add_notice ( $message ) {
// Equal to (Must be exactly the same).
// If the message is displayed in another language, adjust where necessary!
if ( $message == 'Checkout is not available whilst your cart is empty.' ) {
return false;
}
return $message;
}
add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add_notice', 10, 1 );
Based on the information in the answer from #7uc1f3r and some other tutorials I was checking recently, I discovered that this also does the trick:
add_filter( 'woocommerce_add_notice', '__return_null' );
Related
I need the Woocommerce cart to be cleaned in case I send more than four items to it via url.
I came up with this code, but it only cleans the cart if there are already five items in it.
//in functions.php
add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->get_cart_contents_count() >= 5 )
WC()->cart->empty_cart();
return $passed;
}
Another problem is that, even the code above that is not useful to me, does not work if I add multiple courses via url, as in:
https://exemple.net/cart?fill_cart=100,101,102,103,104,105,106
That is, the code in functions.php only works from the website, and not by url.
All I need is clear the cart when sending more than 4 items by url.
I prefer a solution in PHP, but a JS solution will do. Thanks for who can help me.
I found a solution using the free plugin Cart links for WooCommerce, and making a few change in its code.
I will describe below what I did:
1) To start install the plugin, this will allow you to add products using the fill_cart parameter in url (as described in my question)
2) edit the plugin file soft79-cart-links-for-woocommerce.php found at: yoursite/wp-content/plugins/soft79-cart-links-for-woocommerce/
3) go to line 141 or find the declaration of the method fill_cart
Inside the fill_cart method uncomment (or add) the code to clean the cart:
public function fill_cart( $fill_string ) {
$original_notices = wc_get_notices();
wc_clear_notices();
////Clear the cart
//WC()->cart->empty_cart(); <-- uncomment here!
Save the file. Now any new items that arrive via url must clean the cart first.
I want to add my own custom payment error text (we use Account Funds and want to add an extra prompt)
How can I change, the following which shows when there is not enough funds, from:
Sorry, it seems that there are no available payment methods for your
state. Please contact us if you require assistance or wish to make
alternate arrangements.
to an error message with a link like below:
You do not have sufficent funds to process this order, please top up or upgrade. Thank you.
The text seems to be stored in templates/checkout/payment.php
https://github.com/woocommerce/woocommerce/blob/ef05bfccfc01bb2c621ef1293e61f7c57950670f/templates/checkout/payment.php
How can I change this without it getting wiped out by a Woocommerce version upgrade?
In WordPress, filters are functions that can be hooked to an event (called hooks). During the execution when the event is triggered the filter is applied to the data output generated by the event hook. It is important to remember that filters perform their actions on data they receive and then return that data before it is displayed in the browser.
In the file you attached (payment.php) you have
apply_filters( 'woocommerce_no_available_payment_methods_message' ....
So you can use the filter "woocommerce_no_available_payment_methods_message" to change the text
Create a custom function and add it to functions file or a small plugin.
First we hook our own function with woocommerce event
add_filter( 'woocommerce_no_available_payment_methods_message', 'your_custom_function_name_here' );
Now we define what our function would do.
function your_custom_function_name_here( $content ) {
//your changes here
$content = "bla bla";
// Returns the content.
return $content;
}
You just have to add a filter and apply your change in your child-theme's functions.php file:
add_filter( 'woocommerce_no_available_payment_methods_message', function( $no_gateways_message ) {
return 'You do not have sufficent funds to process this order, please top up or upgrade. Thank you.';
});
Let me know if that worked.
add_filter( 'woocommerce_no_available_payment_methods_message', 'change_payment_message', 10, 2);
function change_payment_message( $value, $arg2 ) {
$message = WC()->customer->get_billing_country()?'You do not have sufficent funds to process this order, pleasetop up or upgrade. Thank you':'Please fill in your details above to see available payment methods.';
return $message;
}
before WooCommerce 3.0 came out my code had worked like a charm to save custom values from the cart into the order on checkout. But since then I'm not able to create custom meta for orders.
Environment: Wordpress 4.9.4 & WooCommerce 3.3.3
Hooks
add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
add_action('woocommerce_checkout_create_order', 'custom_meta_to_order', 20, 1);
The Hook number 1 is the one I tried the most, the 2 one was just an experiment with some literal changes mentioned in this topic.
Function
The following functions-code is related to hook number 1:
if (!function_exists('custom_meta_to_order')) {
function custom_meta_to_order($order_id, $values) {
$order = wc_get_order( $order_id );
$order->update_meta_data('_TESTKEYstart', 'Hello');
if (isset($values['myValue'])) {
$myValue = $values['myValue'];
if (!empty($myValue)) $order->update_meta_data('_myKey', $myValue);
}
$order->update_meta_data('_TESTKEYend', 'Bye');
$order->save();
}
}
I've checked also in the mySQL table table wp_woocommerce_order_itemmeta if at least the two _TESTKEY*-meta-entrys will be created (because they don't have a condition).
But it seems that the meta-keys and values don't getting created via this hook and function.
The function itself getting called, so at least the hooks itselfs are working.
So my question is: "What am I doing wrong?"
UPDATED: There is some errors in your code…
Both hooks have only 1 argument (not 2, so $values doesn't exist)
To get your custom field you should use $_POST['myValue'] instead.
and other things like each hook has a different argument:
$order_id for woocommerce_checkout_update_order_meta
$order for woocommerce_checkout_create_order
Below I have replaced $_POST['myValue'] by $_POST['billing_country'] as you don't give the code for this custom checkout field…
So here are both ways:
1) The best way for me, as explained here:
if ( ! function_exists('custom_meta_to_order') ) {
add_action( 'woocommerce_checkout_create_order', 'custom_meta_to_order', 20, 1 );
function custom_meta_to_order( $order ) {
$order->update_meta_data('_TESTKEYstart', 'Hello');
if (isset($_POST['billing_country'])) {
$myValue = $_POST['billing_country'];
if (!empty($myValue)) $order->update_meta_data('_my_key', $myValue);
}
$order->update_meta_data('_TESTKEYend', 'Bye');
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
2) The other way:
if ( ! function_exists('custom_meta_to_order') ) {
add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
function custom_meta_to_order( $order_id ) {
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order->update_meta_data('_TESTKEYstart', 'Hello');
if (isset($_POST['billing_country'])) {
$myValue = $_POST['billing_country'];
if (!empty($myValue)) $order->update_meta_data('_my_key', $myValue);
}
$order->update_meta_data('_TESTKEYend', 'Bye');
// Save the order data and meta data
$order->save();
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
The proof:
And (in database wp_postmeta table for this order ID):
Tested in WooCommerce version 3.3+
You can use the old way too (which works):
if ( ! function_exists('custom_meta_to_order') ) {
add_action('woocommerce_checkout_update_order_meta', 'custom_meta_to_order', 20, 1);
function custom_meta_to_order( $order_id ) {
update_post_meta( $order_id, '_TESTKEYstart', 'Hello' );
if ( isset( $_POST['billing_country'] ) ) {
$myValue = $_POST['billing_country'];
if (!empty($myValue))
update_post_meta( $order_id, '_my_key', $myValue);
}
update_post_meta( $order_id, '_TESTKEYend', 'Bye');
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
Related: Add extra meta for orders in Woocommerce
Because comments are really hard to read (because of to much restricted formatation), this answer is just a response to the answer from LoicTheAztec.
I wrote a longer answer but it seems gone, so I sorry now for the much shorter one!
First our misunderstanding
You understood that I would like to use custom values from products but in my case it was a little bit other. I wrote an external application which included the wp-load.php and posted then data back to the product-page into the cart.
So the problem showed up here was the attempt to write the data from the cart into the order on checkout.
Recommend ways doesn't worked at first
The recommend ways you suggested all doesn't worked. I also stripped them so much down that they have should work and just write something into the meta. I've no clue which plugin/theme-function pranked me this time here.
But I was able to solve the problem
And many more! Just because I found the blog-post where I found out in the past, how to do this and as addition to my personal luck the author wrote already the changes for WP3.0, related to this process.
Still your post helped me
The errors you showed me bugged me since then and because it was hard to follow and inspect everything with Sublime and CodeIntel (and my start with Symfony itself) I decided to buy PHPStorm which showed and allowed me to fix all of my deprecated (legacy-using) functions by updating them properly.
( Finally no more global-variables: Yay. )
I mean, showing up parameters inline and deprecation-strokes already did a great job. But a bug-free working code-intel/reference which doesn't dies on big projects is just awesome.
That's why I marked your answer now as solution, thanks. Otherwise I would have just fixed the problem maybe (thanks to the authors blog-post) but still would sit on a ticking time bomb.
Completely new to Woocommerce/wordpress here. On the cart page mydomain.local/cart What filter should I use to show/hide a flat rate shipping method at certain times. From admin I managed to add an extra flat rate method and named it "Next day". Now I would like to show that flat rate method only before 4PM. I tried in functions.php
add_filter( 'woocommerce_package_rates', 'custom_change_shipping', 10);
function custom_change_shipping($rates) {
var_dump($rates);
}
Nothing seems to change and also I cant seem to debug the $rates variable as nothing is outputted when var_dump($rates);. I tried it both anonymous and as an admin but nothing seems to work.
Thanks #LoicTheAztec for confirming whether I was using the right filter. My other problem was that I was not able to output var_dump on the /cart page.
I found out that I need to clear my "cart cache" by going to woocommerce->settings->shipping and in the region, disable then enable again and click on save changes.
By doing this I could see my output from var_dump.
Filter posted earlier by #LoicTheAztec (as requested by #robert)
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping', 100 );
function custom_hide_shipping( $rates ) {
$current_time = date_i18n(('H'), current_time('timestamp')); //Uses Timezone Settings > General
$maximum_time = array (
'time' =>'16'); //4PM
if ($current_time >= $maximum['time']){
unset( $rates['flat_rate:X'] ); // change X to your rate id
}
return $rates;
}
I need to do an action when an order is completed.
I Have tried this:
function mysite_woocommerce_payment_complete( $order_id ) {
error_log("callback fired");
}
add_action( 'woocommerce_payment_complete', 'mysite_woocommerce_payment_complete' );
But when I use the checkmark to mark the order as completed here in admin orders screen,
...the hook doesn't fire.
I tried woocommerce_order_status_changed too, it does the action when I place the order, but does nothing when i mark the order completed.
But when I mark the order completed, I get the email about completing it.
Thanks!
Edit:
I tried woocommerce_order_status_changed too, this way:
function mysite_woocommerce_payment_complete($order_id, $old_status, $new_status ) {
error_log("$old_status / $new_status \n");
}
add_action( 'woocommerce_order_status_changed', 'mysite_woocommerce_payment_complete', 99, 3 );
but it fires on purchasing (I selected bank transfer) and shows: "pending / on-hold", but not true - see edi2 doesn't fire on manual backend change from "on hold" to "completed". Neither by checkmark nor in single order interface.
Edit2
woocommerce_order_status_changed and woocommerce_order_status_completed works, it only outputed my testing "error" into debug.log, not to error_log for some reason.
The woocommerce_payment_complete i used previously doesn't apply to methods like bank transfer, that's why that didn't work. Thanks to #helgatheviking for the quick and right answer
Well the completed order email is triggered by the following:
// Triggers for this email
add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );
as seen here in source.
All "transactional email actions" (ie: actions that trigger the sending of an email) get a _notification hook in addition to the normal hook, seen here.
Thus woocommerce_order_status_completed_notification is an additional hook triggered on the woocommerce_order_status_completed hook if woocommerce_order_status_completed is in the woocommerce_email_actions array, which it is by default. To avoid any surprised from the emails, I recommend using the woocommerce_order_status_completed hook which is fired any time there is an order status change, including from within the admin, see this example:
function mysite_woocommerce_payment_complete( $order_id ) {
error_log("callback fired");
}
add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_payment_complete' );