Adding a custom text under the X button that removes items from cart

…衆ロ難τιáo~ 提交于 2019-12-07 20:51:58

问题


With Wordpress and WooCommerce, I am trying to add a custom text "delete item" under the "x" in the shopping cart tthat removes items from cart.

I tried to "inspect element" to find where this "x" text icon was located, but I am hitting a brick wall.

Any suggestions on how I can find this, and amend the "x" button to include text underneath?

Thanks.


回答1:


add_filter('woocommerce_cart_item_remove_link', 'remove_icon_and_add_text', 10, 2);

function remove_icon_and_add_text($string, $cart_item_key) {
    $string = str_replace('class="remove"', '', $string);
    return str_replace('×', 'Delete Item', $string);
}

Please try below code snippet in your active theme's functions.php




回答2:


This little cross text icon is located on the WooCommerce templates cart/cart.php and cart/mini-cart.php. But instead overriding this templates, you can use the dedicated woocommerce_cart_item_remove_link filter hook, to achieve what you want to do.

Here is a working tested code that will add 'Delete item' below the red cross cart icon:

add_filter( 'woocommerce_cart_item_remove_link', 'custom_filter_wc_cart_item_remove_link', 10, 2 );
function custom_filter_wc_cart_item_remove_link( $sprintf, $cart_item_key ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $sprintf;

    // HERE Define your additional text
    $add_text = __('Delete item', 'woocommerce');

    // HERE Define the style of the text
    $styles = 'font-size:0.8em; display:block;';

    $sprintf = str_replace('</a>', '</a><span class="remove-text" style="'.$styles.'">'.$add_text.'</span>', $sprintf);

    return $sprintf;
};

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

You will may be need to add some CSS styles for the red cross icon using
.woocommerce a.remove CSS selector in the slyle.css file of your active theme.



来源:https://stackoverflow.com/questions/42548276/adding-a-custom-text-under-the-x-button-that-removes-items-from-cart

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