WooCommerce - unset “ removed notice…” on cart page

后端 未结 4 1237
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 05:24

actions and filters. On my WooCommerce site I get the following message when I remove a product from the shopping cart:

\"\" removed. Und         


        
相关标签:
4条回答
  • 2020-12-20 05:49

    You can do that in different ways:

    1. Overriding the notices.php template:
    You have first (if not done yet) to copy woocommerce templates folder inside your active child theme or theme, then rename it woocommerce. Then open/edit notices/notices.php and try to replace the code:

    <?php
    /**
     * Show messages
     * ... Blabla ... / ... blabla ...
     * @version     1.6.4
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    if ( ! $messages ){
        return;
    }
    
    ?>
    
    <?php foreach ( $messages as $message ) : // Change your template code from here
        if ( strpos( $message, 'removed' ) === false ) : ?>
        <div class="woocommerce-info"><?php echo wp_kses_post( $message ); ?></div>
    <?php endif;
    endforeach; ?>
    

    2. Using hooks:

    function remove_added_to_cart_notice()
    {
        $notices = WC()->session->get('wc_notices', array());
    
        foreach( $notices['notices'] as $key => &$notice){
            if( strpos( $notice, 'removed' ) !== false){
                $added_to_cart_key = $key;
                break;
            }
        }
        unset( $notices['notices'][$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);
    

    3. Using CSS (with something like):

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

    References:

    • Wordpress - Woocommerece remove "Added to Cart" message
    • New wc-notice-functions.php on github
    0 讨论(0)
  • 2020-12-20 05:50

    Just an update regarding Overriding the notices.php template:

    <?php foreach ( $notices as $notice ) :
            if ( strpos( $notice['notice'], 'removed' ) === false ) : ?>
            <div class="woocommerce-message"<?php echo wc_get_notice_data_attr( $notice ); ?> role="alert">
                <?php echo wc_kses_notice( $notice['notice'] ); ?>
            </div>
            <?php endif;
        endforeach; ?>
    

    Had to have add the 'notice' array key to the strpos method or it wasn't finding the "removed" string within the notice message. Hope this helps others who were having issues attempting to use this template override method.

    0 讨论(0)
  • 2020-12-20 06:04

    I had to change this to get it to work. Specifically, the array field is singular notice or at least it is now.

    $notices = WC()->session->get('wc_notices', array());
    foreach( $notices['notice'] as $key => &$notice){
        if( strpos( $notice, 'whilst' ) !== false){ 
            $BadNotice_key = $key;
            unset( $notices['notice'][$BadNotice_key] );
            WC()->session->set('wc_notices', $notices);
            break;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 06:04

    1. Easy way: in wp-content/plugins/woocommerce/includes/class-wc-form-handler.php

    2. remove/disable this line: wc_add_notice( $removed_notice ); (line 523) like this

                if ( $product && $product->is_in_stock() && $product->has_enough_stock( $cart_item['quantity'] ) ) {
                    $removed_notice  = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
                    $removed_notice .= ' <a href="' . esc_url( wc_get_cart_undo_url( $cart_item_key ) ) . '" class="restore-item">' . __( 'Undo?', 'woocommerce' ) . '</a>';
                } else {
                     $removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
                }
                 // wc_add_notice( $removed_notice );
            }
    
    0 讨论(0)
提交回复
热议问题