Skip to content

Instantly share code, notes, and snippets.

@Willem-Siebe
Last active September 7, 2020 10:56
Show Gist options
  • Save Willem-Siebe/dc34719917e77fcecbc6 to your computer and use it in GitHub Desktop.
Save Willem-Siebe/dc34719917e77fcecbc6 to your computer and use it in GitHub Desktop.
Replace the 'x' in WooCommerce cart.php with text because 'x' is not helpfull at all for screenreader users. I give a extra class to the text so that it can be hidden with CSS for visual browsers, because I replace that link with Font Awesome icon using :before. See also: https://github.com/woothemes/woocommerce/issues/5725.
// Replace the 'x' in WooCommerce cart.php with text because 'x' is not helpfull at all for screenreader users, see https://gist.github.com/Willem-Siebe/dc34719917e77fcecbc6.
function wsis_woocommerce_remove_item( $wsis_html, $cart_item_key ) {
$cart_item_key = $cart_item_key;
$wsis_html = sprintf( '<a href="%s" class="remove" title="%s"><span class="wsis-remove-item">%s</span></a>', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'woocommerce' ), __( 'Remove this item', 'woocommerce' ));
return $wsis_html;
}
add_filter ( 'woocommerce_cart_item_remove_link', 'wsis_woocommerce_remove_item', 10, 2 );
@coffeeneed
Copy link

Thanks, man.

@StefsterNYC
Copy link

Did that even work @coffeeneed? I've never seen that work.

@teledirigido
Copy link

This is really helpful!. In later versions you'll need to replace WC()->cart->get_remove_url for wc_get_cart_remove_url

@Oxibug
Copy link

Oxibug commented Sep 7, 2020

Unfortunately This code will remove two data attributes in the output data-product_id and data-product_sku.

You can do it using the action [woocommerce_before_cart_contents] to get the cart list in a global variable once before foreach begins and of course the main filter [woocommerce_cart_item_remove_link].

class THEME_SLUG_WooCommerce {

    private static $_instance = null;

    /**
     * The Cart items
     * @var array|null
     */
    public $cart_list = null;

    public static function instance() {

        if( is_null( self::$_instance ) ) {

            self::$_instance = new self;

            // Start Page Actions
            self::$_instance->init_actions();

        }

        return self::$_instance;

    }


    public function init_actions() {

        add_action( 'woocommerce_before_cart_contents', array( $this, '__before_cart' ) );
        add_action( 'woocommerce_before_mini_cart_contents', array( $this, '__before_cart' ) );

        add_filter( 'woocommerce_cart_item_remove_link', array( $this, '__table_cart_item_remove_link' ), 10, 2 );

    }


    /**
     * Get cart list in action -- woocommerce_before_cart_contents --
     * to do it once 
     * 
     * @since 1.0.0
     */
    public function __before_cart() {

        self::$_instance->cart_list = WC()->cart->get_cart();

    }


    /**
     * Cart Tables - Remove Link
     * 
     * @since 1.0.0
     */
    public function __table_cart_item_remove_link( $output, $cart_item_key ) {

        $cart = self::$_instance->cart_list;

        if( ! $cart || empty( $cart ) || ! array_key_exists( $cart_item_key, $cart ) ) {
            return wp_kses_post( $output );
        }

        $cart_item = $cart[ $cart_item_key ];

        $_product = $cart_item['data'];
        $product_id = $cart_item['product_id'];

        $output = sprintf( '<a href="%s" class="remove remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s"><span class="far fa-trash-alt"></span></a>',
                    esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
                    esc_html__( 'Remove this item', 'zbest' ),
                    esc_attr( $product_id ),
                    esc_attr( $cart_item_key ),
                    esc_attr( $_product->get_sku() ) ); 

        return wp_kses_post( $output );

    }


} // Class End


/* == Fire up == */
if( ! function_exists( '__THEME_SLUG_CLS_WCFunctionsInstance' ) ) {

    function __THEME_SLUG_CLS_WCFunctionsInstance() {

        return THEME_SLUG_WooCommerce::instance();

    }

}

__THEME_SLUG_CLS_WCFunctionsInstance();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment