Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 12, 2018 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/2dd33fffd736fb6dd3cbe77176c59eb8 to your computer and use it in GitHub Desktop.
Save damiencarbery/2dd33fffd736fb6dd3cbe77176c59eb8 to your computer and use it in GitHub Desktop.
Limit sale flash by category or ID - Limit the categories or products where the WooCommerce sale flash is displayed. https://www.damiencarbery.com/2018/10/limit-sale-flash-by-category-or-id/
<?php
/*
Plugin Name: Limit sale flash by category or ID
Plugin URI: https://www.damiencarbery.com/2018/10/limit-sale-flash-by-category-or-id/
Description: Limit the categories or products where the WooCommerce sale flash is displayed.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'woocommerce_sale_flash', 'dcwd_limit_wc_sale_flash', 10, 3 );
function dcwd_limit_wc_sale_flash( $markup, $post, $product ) {
$exclude_by_product = array( 33 ); // List of product IDs where sale flash will not be shown.
$exclude_by_category = array( 'hoodies' ); // List of category slugs where sales flash will not be shown.
if ( in_array( $product->get_id(), $exclude_by_product ) ) {
//return 'Product Excluded'; // Use this during testing to ensure code works.
return '';
}
// Get product's categories.
$product_categories = get_the_terms( $post->ID, 'product_cat' );
if ( $product_categories && ! is_wp_error( $product_categories ) ) {
foreach ( $product_categories as $category ) {
if ( in_array( $category->slug, $exclude_by_category ) ) {
//return 'Category Excluded'; // Use this during testing to ensure code works.
return '';
}
}
}
return $markup;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment