Wordpress - Woocommerece remove “Added to Cart” me

2020-04-07 04:59发布

问题:

I'm looking to remove the wording and area that says "Product Successfully Added to Cart" after I add an item to the cart. I just want there to be nothing, no message and no space for the message.

Here's the site: http://www.tinytreasurehunts.com The code is in woocommerece-functions.php

Any thoughts?

回答1:

To solve this at PHP level, add the following template file (and structure) to your theme:
/wp-content/themes/YOUR-THEME/woocommerce/shop/messages.php:

<?php
/**
 * Show messages
 *
 * @author      brasofilo
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( ! $messages ) return;

foreach ( $messages as $message ) : 
    // The message does not contain the "add to cart" string, so print the message
    // http://stackoverflow.com/q/4366730/1287812
    if ( strpos( $message, 'added to your cart' ) === false ) :
        ?>
            <div class="woocommerce-message"><?php echo wp_kses_post( $message ); ?></div>
        <?php 
    endif;
endforeach;

See: Template Structure + overriding templates via a theme



回答2:

Use one of these:

Older version

$woocommerce->clear_messages(); 

Version 2.3

wc_clear_notices();


回答3:

Use CSS and set the display to none for the ID or associated class.

 
.page-id-522 .woocommerce_message {
     display: none;
}

This is specific to page id 522. Make sure this doesn't also hide other useful messages like credit card declines, etc.



回答4:

Just use simple CSS:

.single-product .woocommerce-message {
display: none !important;
}


回答5:

Add this code to your themes functions.php file. It will remove only that message. It should trigger on just the pages where it is likely to occur.

function remove_added_to_cart_notice()
{
    $notices = WC()->session->get('wc_notices', array());

    foreach( $notices['success'] as $key => &$notice){
        if( strpos( $notice, 'has been added' ) !== false){
            $added_to_cart_key = $key;
            break;
        }
    }
    unset( $notices['success'][$added_to_cart_key] );

    WC()->session->set('wc_notices', $notices);
}
add_action('woocommerce_before_single_product','remove_added_to_cart_notice',1);
add_action('woocommerce_shortcode_before_product_cat_loop','remove_added_to_cart_notice',1);
add_action('woocommerce_before_shop_loop','remove_added_to_cart_notice',1);

I've pasted this answer from my own answer at Remove/Hide Woocommerce Added to Cart Message but Keep/Display Coupon Applied Message



回答6:

Update for WooCommerce Version 2.1.6

The template is located in a new directory and file. Same code and solution as above.

/wp-content/plugins/woocommerce/templates/notices/success.php



回答7:

Use .post .woocommerce_message{display:none;} at the end of your theme files or in your child theme.