What code should I add to functions.php to remove "Checkout is not available whilst your cart is empty." notice in Woocommerce.
I found the code in includes/wc-template-functions.php that is responsible for displaying this message.
// When on the checkout with an empty cart, redirect to cart page.
if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', 'woocommerce' ), 'notice' );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
Overwriting the core file is NOT an option, any advice?
You can use the woocommerce_checkout_redirect_empty_cart filter hook. Since the message is only displayed if this condition is true
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
Another option is to use the woocommerce_add_notice filter hook and if the message matches, return false
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 );
Related
I currently have this code in functions.php in order to display a message on the checkout page saying the customer has backordered products in their cart:
add_action( 'woocommerce_review_order_before_payment', 'es_checkout_add_cart_notice' );
function es_checkout_add_cart_notice() {
$message = "You have a backorder product in your cart.";
if ( es_check_cart_has_backorder_product() )
wc_add_notice( $message, 'error' );
}
function es_check_cart_has_backorder_product() {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = wc_get_product( $values['data']->get_id() );
if( $cart_product->is_on_backorder() )
return true;
}
return false;
}
What do I need to do to also have this same message display on the CART page? (Before going to Checkout).
Thanks in advance.
Note 1: I changed the code so that it works with 1 hook on both pages as opposed to using 2 different hooks
Note 2: Notice the use of the notice_type
wc_add_notice( __( $message, 'woocommerce' ), 'notice' );
opposite 'error'.
Ultimately, this is not an error
Note 3: optionally you can remove the 'proceed_to_checkout' button (code line is in comment, this doesn't seem to apply here) (see note 2)
So you get:
function es_add_cart_notice() {
// Only on cart and check out pages
if( ! ( is_cart() || is_checkout() ) ) return;
// Set message
$message = "You have a backorder product in your cart.";
// Set variable
$found = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Product on backorder
if( $product->is_on_backorder() ) {
$found = true;
break;
}
}
// true
if ( $found ) {
wc_add_notice( __( $message, 'woocommerce' ), 'notice' );
// Removing the proceed button, until the condition is met
// optional
// remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
}
}
add_action( 'woocommerce_check_cart_items', 'es_add_cart_notice', 10, 0 );
Woocommerce: How to add some html codes below add to cart in single product page? and i want add different codes for each single product!
I used a similar one for metabox, but I can not find a way to adapt it after "add to cart"
---- 1. Backend ----
// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_meta_box',
__( 'Additional Product text <em>(optional)</em>', 'woocommerce' ),
'add_custom_product_content_meta_box',
'product',
'normal',
'default'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){
function add_custom_product_content_meta_box( $post ){
$text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : '';
$args['textarea_rows'] = 6;
echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>';
wp_editor( $text_area, 'custom_text', $args );
echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 );
if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){
function save_custom_product_content_meta_box( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
// Sanitize user input and update the meta field in the database.
if ( isset( $_POST[ 'custom_text' ] ) )
update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) );
}
}
---- 2. Frontend ----
// Add custom text under single product meta
add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 );
function add_custom_product_text() {
global $product;
$custom_text = get_post_meta( $product->get_id(), '_custom_text', true );
if( empty($custom_text) ) return;
echo '<div class="product-extra-text" style="margin-top:30px;">';
echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $custom_text );
echo '</div>';
}
I put the code in parts, but it is united in functions of the theme.
Backend IMAGE:
enter image description here
In your theme's function.php add the code,
add_action('woocommerce_after_add_to_cart_button','show_custom_text');
function show_custom_text() {
call the custom text inside this function
}
I wanted to apply a coupon code to cart if cart have minimum 2 items. if not have then the coupon not will apply and show alter message and if have apply then will show a success message here is my code I have tried not working like I want
add_action( 'woocommerce_before_calculate_totals','conditionally_auto_add_coupon', 30, 1 );
function conditionally_auto_add_coupon( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit
// HERE set the coupon code (in lowercase)
$coupon_code = 'mycode';
$total_item = 0;
if (WC()->cart->has_discount('mycode')) {
foreach( $cart->get_cart() as $cart_item ){
$total_item++;
}
if($total_item < 2){
$cart->remove_coupon( $coupon_code );
wc_add_notice( __('you have only 1 item in cart'), 'alert');
}
else{
$cart->add_discount( $coupon_code );
wc_add_notice( __('coupon added'), 'notice');
}
}
}
Any help is welcome.
Try the following:
add_action( 'woocommerce_before_calculate_totals', 'auto_apply_coupon_conditionally', 10, 1 );
function auto_apply_coupon_conditionally( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$coupon_code = 'summer'; // HERE set the coupon code (in lowercase)
$applied = in_array( $coupon_code, $cart->get_applied_coupons() ) ? true : false;
$item_count = sizeof( $cart->get_cart() );
$total_item = 0;
// Remove coupon
if ( $item_count < 2 && $applied ) {
$cart->remove_coupon( $coupon_code );
wc_clear_notices();
wc_add_notice( __('You have only 1 item in cart'), 'error');
}
// Add coupon
elseif ( $item_count >= 2 && ! $applied ) {
$cart->apply_coupon( $coupon_code );
wc_clear_notices();
wc_add_notice( __('A coupon has been added'), 'notice' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Please use "Smart Coupon For Woocommerce" plugin implementing the auto coupon functionality,
Please refer this code in svn repo.
How can I redirect back to the /cart after I click the 'Undo' link when I delete an item from it?
I am not concerned if the cart is empty or not. I had to implement a template_redirect already, so when a user "removes" an item from the cart it redirects back to the cart rather than the home page. With:
add_action( 'template_redirect', function( ) {
$_var = ( $_GET[ 'removed_item' ] ) ?? null;
//var_dump($_var);
if( $_var == '1' ){
wp_safe_redirect( '/cart', 301 );
die();
}
} );
Can I hook into the same action? I don't see a query string after clicking the 'Undo' tho...
EDIT
I can see where the action is done. It's in the plugin includes/class-ws-form-handler.php, right around line 536... containing:
// Undo Cart Item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
WC()->cart->restore_cart_item( $cart_item_key );
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
EDIT 2:
Adding the following to my redirection above does nothing...
// undo the remove, redirect back to cart
if ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
// Undo Cart Item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
WC()->cart->restore_cart_item( $cart_item_key );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
So, when I var_dump wp_get_referer() I get back the home page of the site, which is not what I am looking for. I do not want to modify this file due to that not being that great of an idea... so, how can I just modify the action instead? The function itself is: public static function update_cart_action()
So... I ended up have to do some voodoo to get this to work.
Here is the hook...
// undo the remove, redirect back to cart
// check if the cart is updated... if it is, proceed
$_cu = apply_filters( 'woocommerce_update_cart_action_cart_updated', true );
if( $_cu ) {
global $woocommerce;
if ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
// item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
// Get the cart
$woocommerce->cart->get_cart();
// restore the item
$woocommerce->cart->restore_cart_item( $cart_item_key );
// now do the redirect
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
It is applied in the init event
In WooCommerce I've implemented #jtsternberg's WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string to allow adding multiple products at once, but I've received many complaints from customers who actually try to use one of the links containing multiple products.
For starters, if the customer clicks checkout and then clicks the browser "back" button, all the item quantities increment. I solved this by redirecting the user to the cart URL stripped of any additional parameters after the add-to-cart behavior completes, but it's not ideal.
What I really want is to check if the item is in the cart first and only add to cart if it isn't there already. Has anyone done something similar?
Working Update:
I ended up modifying the code from #jtsternberg to use a completely separate param name in order to avoid conflict with the default add-to-cart behavior. Then I was able to use #LoicTheAztec's suggested code below by wrapping the behavior in a check to see if that new param exists. Here's the full section:
function custom_product_link() {
if (empty( $_REQUEST['multi-product-add'])) {
return;
}
$product_ids = explode( ',', $_REQUEST['multi-product-add'] );
foreach ( $product_ids as $product_id ) {
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
if ( wc_notice_count( 'error' ) === 0 ) {
// If has custom URL redirect there
if ( $url = apply_filters( 'woocommerce_add_to_cart_redirect', false ) ) {
wp_safe_redirect( $url );
exit;
} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
function check_product_added_to_cart( $passed, $product_id, $quantity) {
if (!empty( $_REQUEST['multi-product-add'])) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['product_id'] == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
// wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
}
return $passed;
}
add_action( 'wp_loaded', 'custom_product_link', 15 );
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
As in the code you are using you have woocommerce_add_to_cart_validation filter hook inside it, you can use it in a custom hooked function to check if products rare already in cart with something like:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
function check_product_added_to_cart( $passed, $product_id, $quantity) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['data']->get_id() == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and this normally should works with your customization…
For product quantities, you can use the $quantity argument with $cart_item['quantity'] in some conditions…