woocommerce添加清除购物车按钮

参考这里

http://www.remicorson.com/create-a-empty-cart-button-for-woocommerce/


To do so, there are two steps. The first one is to add this little snippet in your functions.php file (this file is located in your theme folder):

// 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();    	
}    }

And the second step is to create a custom template for the cart. Basically a custom template allow you to override WooCommerce default files and use your own custom files instead. Here is a quick tutorial that will explain you how to create your custom templates: http://docs.woothemes.com/document/template-structure/

Finally, in “templates/cart/cart.php”, add at line 134 the HTML code to output the button:

这里补充一行代码,在cart.php开头要初始化$woocommerce这个变量,在添加按钮的代码;

global $woocommerce;
<a class="button" href="<?php echo $woocommerce->cart->get_cart_url(); ?>?empty-cart"><?php _e( 'Empty Cart', 'woocommerce' ); ?></a>

原理很简单,其实函数已经在class里面定义好,只需要添加一个呼唤执行的函数,还有在前端添加相应的按钮即可。

你可能感兴趣的:(wordpress,woocommerce)