Empty Cart button in Mini Cart. woocommerce

2019-06-14 06:17发布

Is it possible to add an empty cart button inside a mini cart? I managed to do that on a cart page but having issue with a mini cart.

add_action( 'woocommerce_widget_shopping_cart_buttons', 'add_clear_cart_button', 20 );
function add_clear_cart_button() {
    echo "<a class='button' href='?empty-cart=true'>" . __( 'Empty Cart', 'woocommerce' ) . "</a>";
}

Am I using a wrong action?

1条回答
冷血范
2楼-- · 2019-06-14 06:30

add below function in function.php for empty cart button

<?php
// check for empty-cart get param to clear the cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
  global $woocommerce;

    if ( isset( $_GET['empty-cart'] ) ) {
        $woocommerce->cart->empty_cart(); 
    }
}
add_action( 'woocommerce_widget_shopping_cart_buttons', 'add_clear_cart_button', 10, 2 );
function add_clear_cart_button() {
    ?>
    <a class="button" href="<?php echo $woocommerce->cart->get_cart_url(); ?>?empty-cart"><?php _e( 'Empty Cart', 'woocommerce' ); ?></a>
    <?php
} 
?>
查看更多
登录 后发表回答