Unset removed cart item notice on WooCommerce cart page

我怕爱的太早我们不能终老 提交于 2019-12-30 12:08:10

问题


Is there a way to hide/remove the notice "Product removed. Undo?" on the cart page, when the customer removed the item from the cart?

I tried:

add_filter( 'woocommerce_cart_item_removed_title', '__return_null' );

but this only hides the title of the product, that has been removed...

I couldn't find the right hook of the rest of the line ("removed" and "Undo?")


回答1:


To unset "{item_name} removed. Undo?" notice on cart page, you can trick Woocommerce with the following:

add_action( 'template_redirect', 'null_removed_cart_item_message'  );
function null_removed_cart_item_message() {
    // Only on cart page
    if( ! is_cart() ) return;

    // Get the WC notices array stored in WC_Session
    $wc_notices = (array) WC()->session->get( 'wc_notices' );
    $found      = false; // Initializing

    // Check that we have at least one "success" notice type
    if( isset($wc_notices['success']) && sizeof($wc_notices['success']) ) {
        // Loop through "success" notices type
        foreach( $wc_notices['success'] as $key => $wc_notice ) {
            // Remove notices that contain the word "removed" from the array
            if ( strpos($wc_notice, 'removed') !== false ) {
                unset($wc_notices['success']);
                $found = true;
            }
        }
    }

    if( $found ) {
        // Set back the notices array to WC_Session
        WC()->session->set( 'wc_notices', $wc_notices );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Important: Depending on your installation language, you could need to change the word "removed" in the function code to the one that is convenient in your installation language.




回答2:


I had the same problem, solved using the following statement:

add_filter('woocommerce_cart_item_removed_notice_type', '__return_null');

Code goes in function.php file of your active child theme (or active theme).



来源:https://stackoverflow.com/questions/54502988/unset-removed-cart-item-notice-on-woocommerce-cart-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!