问题
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 theslyle.css
file of your active theme.
来源:https://stackoverflow.com/questions/42548276/adding-a-custom-text-under-the-x-button-that-removes-items-from-cart