Below, you’ll find a detailed guide on how to add the Empty Cart Button for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Empty Cart Button for WooCommerce Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Empty Cart Button for WooCommerce Plugin and the shortcodes it provides:
"Empty Cart Button for WooCommerce is a user-friendly plugin that allows you to effortlessly add an 'Empty Cart' button in your WooCommerce store, enhancing customers' shopping experience."
- [wec_button]
Empty Cart Button for WooCommerce [wec_button] Shortcode
The ‘wec_button’ shortcode is part of the Woo-Empty-Cart-Button plugin. It creates an “Empty Cart” button on your WooCommerce site. The shortcode checks the WooCommerce version and gets the appropriate cart URL. It retrieves the custom ‘Empty Cart’ text from the plugin options. If no text is defined, it defaults to ‘Empty Cart’. The button links to the cart URL with an ’empty-cart’ parameter, which triggers the cart clearance.
Shortcode: [wec_button]
Examples and Usage
Basic example – A simple usage of the ‘wec_button’ shortcode to create an empty cart button.
[wec_button /]
For more advanced examples, you can use the ‘wec_button’ shortcode with different parameters. Although the shortcode doesn’t directly support additional parameters, you can modify the text displayed on the button by changing the ‘wecb_text’ option in your WordPress settings.
Advanced example – Changing the text displayed on the empty cart button by modifying the ‘wecb_text’ option in your WordPress settings.
update_option( 'wecb_text', 'Clear Shopping Cart' );
Then, you can use the ‘wec_button’ shortcode as usual, and it will display the new text:
[wec_button /]
Please note that this is a PHP code, and it should be added in your theme’s functions.php file or a site-specific plugin. Also, remember to replace ‘Clear Shopping Cart’ with the text you want to display on the button.
PHP Function Code
In case you have difficulties debugging what causing issues with [wec_button]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wec_button', array( $this, 'wec_create_button_shortcode' ) );
Shortcode PHP function:
function wec_create_button_shortcode() {
global $woocommerce;
// Check for deprecation notices
if ( version_compare( $woocommerce->version, '2.5', "<=" ) ) {
$cart_url = $woocommerce->cart->get_cart_url();
} else {
$cart_url = wc_get_cart_url();
}
$getEmptytext = get_option( 'wecb_text' );
$emptyTxt = ! empty( $getEmptytext ) ? $getEmptytext : 'Empty Cart';
if ( empty( $_GET ) ) {
return '<a class="button wecb_emptycart" href="' . $cart_url . '?empty-cart=clearcart">' . sprintf( __( '%s', 'woo-empty-cart-button' ), $emptyTxt ) . '</a>';
} else {
return '<a class="button wecb_emptycart" href="' . $cart_url . '?empty-cart=clearcart">' . sprintf( __( '%s', 'woo-empty-cart-button' ), $emptyTxt ) . '</a>';
}
}
Code file location:
woo-empty-cart-button/woo-empty-cart-button/woo-empty-cart-button.php
Conclusion
Now that you’ve learned how to embed the Empty Cart Button for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply