What we’re up to in WooCommerce land right now.
Searching for woocommerce
WooCommerce 3.3 – Hide uncategorized category from the shop page on the frontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' ); | |
function custom_woocommerce_product_subcategories_args( $args ) { | |
$args['exclude'] = get_option( 'default_product_cat' ); | |
return $args; | |
} |
WooCommerce 3.0 – Disable deferred email sending
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_filter( 'woocommerce_defer_transactional_emails', '__return_false' ); |
WooCommerce – Notify admin when a new customer account is created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_action( 'woocommerce_created_customer', 'woocommerce_created_customer_admin_notification' ); | |
function woocommerce_created_customer_admin_notification( $customer_id ) { | |
wp_send_new_user_notifications( $customer_id, 'admin' ); | |
} |
WooCommerce – Hide price suffix when product is not taxable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 ); | |
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) { | |
if ( ! $product->is_taxable() ) { | |
return ''; | |
} | |
return $price_display_suffix; | |
} |
WooCommerce – Redirect external products offsite (disable single listings)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_action( 'template_redirect', 'redirect_external_products' ); | |
function redirect_external_products() { | |
global $post; | |
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) { | |
wp_redirect( $product->get_product_url() ); | |
exit; | |
} | |
} |
WooCommerce – remove payment method from emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
*/ | |
add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' ); | |
function custom_woocommerce_get_order_item_totals( $totals ) { | |
unset( $totals['payment_method'] ); | |
return $totals; | |
} |
WooCommerce – Hide shipping rates when free shipping is available.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hide shipping rates when free shipping is available. | |
* Updated to support WooCommerce 2.6 Shipping Zones. | |
* | |
* @param array $rates Array of rates found for the package. | |
* @return array | |
*/ | |
function my_hide_shipping_when_free_is_available( $rates ) { | |
$free = array(); | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'free_shipping' === $rate->method_id ) { | |
$free[ $rate_id ] = $rate; | |
break; | |
} | |
} | |
return ! empty( $free ) ? $free : $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); |
WooCommerce – Add all upsells of a product to the cart via custom link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. | |
* Product must have upsells, and this works with simple products only. | |
* Example link: yoursite.com?add-upsells-to-cart=X, X being your product ID. | |
*/ | |
add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 ); | |
function bulk_upsell_add_to_cart_action() { | |
if ( ! empty( $_GET['add-upsells-to-cart'] ) ) { | |
$product_id = absint( $_GET['add-upsells-to-cart'] ); | |
$product = wc_get_product( $product_id ); | |
if ( $product ) { | |
$upsell_ids = $product->get_upsells(); | |
if ( $upsell_ids ) { | |
$count = 0; | |
foreach ( $upsell_ids as $upsell_id ) { | |
if ( WC()->cart->add_to_cart( $upsell_id ) ) { | |
$count ++; | |
} | |
} | |
wc_add_notice( sprintf( _n( 'Added %d item to the cart', 'Added %d items to the cart', $count ), $count ) ); | |
} | |
} | |
} | |
} |
WooCommerce – enable free shipping only if product class is found in cart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this if already open! | |
/** | |
* Code goes in theme functions.php. Free Shipping Method must be enabled. | |
*/ | |
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_based_on_cart_shipping_class' ); | |
function free_shipping_based_on_cart_shipping_class( $is_available ) { | |
/** | |
* This example enables free shipping only when an item is found in the cart with a class named 'free_shipping' | |
*/ | |
$cart_items = WC()->cart->get_cart(); | |
$found = false; | |
foreach ( $cart_items as $cart_item ) { | |
$product = $cart_item['data']; | |
$class = $product->get_shipping_class(); | |
if ( 'free_shipping' === $class ) { | |
$found = true; | |
break; | |
} | |
} | |
return $is_available && $found; | |
} |