Disable a specific woocommerce notice error message - woocommerce

I'm trying to remove a specific woocommerce message that appear late in the day when I add a product in the cart but not compelet the chechout
The message is " the item xx has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance"
I don't know what is the reason for this error message,
But I want to completely remove it.

Ok
After some searching , I found this support ticket on Wordpress.org
After Purchase, get a WooCommerce Error
and found this code
function customize_wc_errors( $error ) {
if ( strpos( $error, 'entfernt' ) !== false ) {
return '';
} elseif ( strpos( $error, 'removed' ) !== false ) {
return '';
} else {
return $error;
}
}
add_filter( 'woocommerce_add_error', 'customize_wc_errors' );
hoping to remove that annoying message

Related

Need to get submission_id of elementor form to add in reference and content of the mail

I use the elementor pro form for my contact form. I want to add the submission_id to the mail. I want to display the id in the reference header and in the body of the mail. Like a ticket-System.
EXP: New Mail from xxx - ID: 4566334
I tried to use the following code to add a new shortcode, that gives back the submission_id, but ist not working. Is empty and dont have a value:
add_shortcode( 'submission_id', 'get_submission_id' );
function get_submission_id() {
if ( ! isset( $_POST['form_id'] ) || ! isset( $_POST['_wpnonce'] ) ) {
return '';
}
$form_id = intval( $_POST['form_id'] );
$nonce_value = sanitize_text_field( $_POST['_wpnonce'] );
if ( ! wp_verify_nonce( $nonce_value, 'elementor-pro-form-' . $form_id ) ) {
return '';
}
$submission_data = ElementorPro\Modules\Forms\Classes\Form::get_instance( $form_id )->get_submission_data();
if ( ! $submission_data || ! isset( $submission_data['_id'] ) ) {
return '';
}
return $submission_data['_id'];
}
Any other way to add the submission id from elementor form to the mails?
I tried different plugins, but i dont want to pay for a plugin, just to get the id.
Also tried to add my own shortcode the the function.php, but i doesnt give back a value.
UPDATE: It seems that this part doesnt work
if ( ! isset( $_POST['form_id'] ) || ! isset( $_POST['_wpnonce'] ) ) {
return 'Error 01';
}
I had the same problem. I solved it in the simplest way. My unique id is a code from date and time.
I added a hidden text field to the form. Then in the advanced tab I selected dynamic data, page, current time.
You can set any date/time format along with any character/word....

Hide shipping methods if a virtual product is in the cart

If a woocommerce cart has a virtual item only it does not show shipping methods.
I have a unique situation where I'd like this to be the case also if the cart contains physical products but at least one virtual product (ie any virtual product in the cart, regardless of whatever else is in the cart, hide shipping methods). Whereas if no virtual products are in the cart then show shipping methods as normal.
I've tried the code below but it doesn't seem to work:
Thanks for your help
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping_address ){
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->is_virtual() ) {
$needs_shipping_address = false;
break; // Stop the loop
}
}
return $needs_shipping_address;
}
Thanks, it was throwing the error "Uncaught Error: Call to a member function get_cart() on null" but have managed to fix it with this:
if ( is_null( WC()->cart ) ) {
wc_load_cart();
} else
Let me know if there is a better way to do it? Thanks
add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping ){
// Loop through cart items
if ( is_null( WC()->cart ) ) {
wc_load_cart();
} else {
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->is_virtual() ) {
$needs_shipping = false;
break; // Stop the loop
}
}
return $needs_shipping;
}
}
I think I figured out how to do it for you.
The Hook you're using in your example is not the right one since it's just filtering the shipping address callback.
What I think you need to do is use the woocommerce cart needs shipping hook.
Since it was throwing an error you could try to only run the code on the cart and checkout page. You can do that by using if ( is_cart() || is_checkout() ) {
Try the edited code below and let me know how it works.
Have a wonderful day!
add_filter( 'woocommerce_cart_needs_shipping', 'remove_virtual_product_shipping_methods' );
function remove_virtual_product_shipping_methods( $needs_shipping ){
//Only execute code on cart and checkout page.
if ( is_cart() || is_checkout() ) {
//Loop trough cart items
foreach ( WC()->cart->get_cart() as $item ) {
//If a product in cart is a vritual product remove all shipping
if ( $item['data']->is_virtual() ) {
$needs_shipping = false;
break;
}
}
return $needs_shipping;
}
}

Removing Continue Shopping button from Added to Cart Notice

Currently when someone adds a product to our website it says: "X Product" has been added to your cart | and then has a "Continue Shopping" button in that notice that is on the right.
Since we only sell 2 products we want to remove the continue shopping button completely but still say the rest of the message, and keep "X Product" as a link.
I've been using the following code (but it replaces Continue Shopping with Checkout and I'd prefer to just remove the button completely instead). I just can't figure out how to remove the button but still keep the rest of the message exactly the same:
add_filter( 'woocommerce_continue_shopping_redirect', 'my_changed_woocommerce_continue_shopping_redirect', 10, 1 );
function my_changed_woocommerce_continue_shopping_redirect( $return_to ){
$return_to = wc_get_page_permalink( 'checkout' );
return $return_to;
}
add_filter( 'wc_add_to_cart_message_html', 'my_changed_wc_add_to_cart_message_html', 10, 2 );
function my_changed_wc_add_to_cart_message_html($message, $products){
if (strpos($message, 'Continue shopping') !== false) {
$message = str_replace("Continue shopping", "Checkout", $message);
}
return $message;
}
Use preg_replace to find the string containing the full link and return the new message with all the original HTML minus the link.
add_filter('wc_add_to_cart_message_html','remove_continue_shoppping_button',10,2);
function remove_continue_shoppping_button($message, $products) {
if (strpos($message, 'Continue shopping') !== false) {
return preg_replace('/<a.*<\/a>/m','', $message);
} else {
return $message;
}
}
/* Start Disable Continue Shopping Message after Add to Cart
*/
add_filter( 'wc_add_to_cart_message', function( $string, $product_id = 0 ) {
$start = strpos( $string, '<a href=' ) ?: 0;
$end = strpos( $string, '</a>', $start ) ?: 0;
return substr( $string, $end ) ?: $string;
});
/* End Disable Continue Shopping Message after Add to Cart
*/
If anyone is interested the following code fixed it perfectly, just replace id-407 with whatever page id your Cart page is:
/* Remove Continue Shopping Button Add Cart */
body.page-id-407 .woocommerce-message .button {
display: none
}

WooCommerce inquiry if no price available

I'm helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.
However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.
Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?
Had the exact same issue and couldn't find a plugin or a solution anywhere so I figured a workaround myself:
You need to edit file
/wp-content/themes/your-theme-name/woocommerce/single-product/add-to-cart/simple.php
(if it's not there just copy it from woocommerce plugin
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php)
and on line 14 where it says
if ( ! $product->is_purchasable() ) return;
you need to comment it out and write something like
if ( ! $product->is_purchasable() ) {
// put your code here
return;
}
and in the //put your code here line you can enter for example a shortcode for a form or a more complicated solution would be to put code for a button that when clicked will open up a popup form.
Still working on that ;)
Maybe too late, but I have had same issue currently.
Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404
Notice about: && '' !== $this->get_price()
/**
* Returns false if the product cannot be bought.
*
* #return bool
*/
public function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}
So you need to write a filter like this to override default:
add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );
Try the following code snippet. You just have to put it in your functions.php. You don't need a plugin or to overwrite WooCommerce files in your child theme.
// Inquiry link if no price available
function add_inquiry_link_instead_price( $price, $product ) {
if ( '' === $product->get_price() || 0 == $product->get_price() ) :
return ''.__( 'Jetzt anfragen' ).'';
endif;
}
add_filter( 'woocommerce_get_price_html', 'add_inquiry_link_instead_price', 100, 2 );

Redirect user after first login in wordpress?

This code checks if a user is logging in for the first time, that is after registration. I want to redirect him to a custom page if so. Otherwise, redirect him to the homepage or admin page.
function mylogin_redirect() {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
header("Location: http://example.com/custompage");
} elseif( current_user_can( 'manage_options' )) {
header("Location: http://example.com/wp-admin/");
} else {
header("Location: http://example.com/");
}
}
}
add_action('wp_head', 'mylogin_redirect');
But it doesn't work? My guess is it doesn't get hooked into wp_head...
I tried the following using login_redirect filter:
function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
return get_bloginfo('url') . "/custompage/";
} elseif( current_user_can( 'manage_options' )) {
return admin_url();
} else {
return get_bloginfo('url');
}
}
}
add_filter('login_redirect', 'mylogin_redirect');
Though it logs me in, it doesn't get me anywhere but to http://example.com/wp-login.php instead with a blank page.
UPDATE:
Ok, I don't know what's happening. Using the filter hook, I can get to the intended destination only after second login. Well not really second login but on the second click of the login button. I did it like so: enter credentials -> login -> (wrong page) -> hit back button -> enter credentials again -> login -> (correct page). Weird.
You need to adjust your filter call like so;
// filter name, callback, priority, accepted args
add_filter('login_redirect', 'mylogin_redirect', 10, 3);
Redirecting users on first login in WordPress: Cookie-based solution & User meta table based solution

Resources