I've currently got some custom theme functions running (custom single product page and java script) although want to exclude a specific category from them. How do I go about this?
I currently have implemented a calculator into my single product using the following script;
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method(){
wp_enqueue_script( 'calc_fun', '/wp-content/themes/flatsome-child/assets/js/calc.js', array(), '1.6');
wp_enqueue_style( 'calc_css', '/wp-content/themes/flatsome-child/assets/css/calc.css', true , 9.1 );
}
This calculator features on the single product page and works out quantity and m2 of products. This calculator is tied in with a custom single product page using the following scripts;
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_addtocart_button_func' );
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_after_addtocart_button_func' );
add_action('woocommerce_after_add_to_cart_button', 'add_content_after_true_addtocart_button_func');
add_action('woocommerce_shop_loop_item_title', 'add_woo_shop_loop_item_title');
function add_content_before_addtocart_button_func() {
// Echo content.
get_template_part( 'woo-singlre-product-card', 'template' );
}
function add_content_after_addtocart_button_func() {
// Echo content.
get_template_part( 'woo-singlre-product-card-after', 'template' );
}
function add_content_after_true_addtocart_button_func(){
get_template_part( 'woo-after-single-product', 'template');
}
function add_woo_shop_loop_item_title() {
get_template_part( 'woo-shop-loop-item-title', 'template');
}
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_add_product_description');
function custom_add_product_description ($category) {
get_template_part( 'woo-shop-loop-card-after', 'template' );
}
I want to exclude these scripts from the specific category 'example'.
I've currently got some custom theme functions running (custom single product page and java script) although want to exclude a specific category from them. How do I go about this?
I currently have implemented a calculator into my single product using the following script;
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method(){
wp_enqueue_script( 'calc_fun', '/wp-content/themes/flatsome-child/assets/js/calc.js', array(), '1.6');
wp_enqueue_style( 'calc_css', '/wp-content/themes/flatsome-child/assets/css/calc.css', true , 9.1 );
}
This calculator features on the single product page and works out quantity and m2 of products. This calculator is tied in with a custom single product page using the following scripts;
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_before_addtocart_button_func' );
add_action( 'woocommerce_before_add_to_cart_button', 'add_content_after_addtocart_button_func' );
add_action('woocommerce_after_add_to_cart_button', 'add_content_after_true_addtocart_button_func');
add_action('woocommerce_shop_loop_item_title', 'add_woo_shop_loop_item_title');
function add_content_before_addtocart_button_func() {
// Echo content.
get_template_part( 'woo-singlre-product-card', 'template' );
}
function add_content_after_addtocart_button_func() {
// Echo content.
get_template_part( 'woo-singlre-product-card-after', 'template' );
}
function add_content_after_true_addtocart_button_func(){
get_template_part( 'woo-after-single-product', 'template');
}
function add_woo_shop_loop_item_title() {
get_template_part( 'woo-shop-loop-item-title', 'template');
}
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_add_product_description');
function custom_add_product_description ($category) {
get_template_part( 'woo-shop-loop-card-after', 'template' );
}
I want to exclude these scripts from the specific category 'example'.
Share Improve this question edited May 13, 2019 at 14:17 nmr 4,5672 gold badges17 silver badges25 bronze badges asked May 13, 2019 at 13:30 JayJay 11 bronze badge 2- Do you want the function not to be performed if the product belongs to specific categories? – nmr Commented May 13, 2019 at 13:38
- @nmr And that is correct - I want the function not to be performed on products belonging to a specific category. – Jay Commented May 13, 2019 at 14:03
2 Answers
Reset to default 0WP provides a number of conditionals. So, instead of this, which adds your JS and CSS to every URL on the site
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method(){
wp_enqueue_script( 'calc_fun', '/wp-content/themes/flatsome-child/assets/js/calc.js', array(), '1.6');
wp_enqueue_style( 'calc_css', '/wp-content/themes/flatsome-child/assets/css/calc.css', true , 9.1 );
}
You will want something like this, which only adds your JS and CSS to certain URLs
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
function my_scripts_method(){
// Only enqueue if this is a single product page that's not in product category "example"
// has_term will accept either the name, term ID, or slug of the product category
if(is_singular('product') && !has_term('example', 'product_cat')) {
wp_enqueue_script( 'calc_fun', '/wp-content/themes/flatsome-child/assets/js/calc.js', array(), '1.6');
wp_enqueue_style( 'calc_css', '/wp-content/themes/flatsome-child/assets/css/calc.css', true , 9.1 );
}
}
You can do the same thing inside your other functions - instead of always doing get_template_part()
, wrap that inside of a conditional that checks whether this is the specific product you want to add your code to.
If you have more than one category to exclude you can use something like:
function se337775_omit_calculator()
{
global $post;
// calculator can be added only on single product page
if ( ! is_singular('product') ) {
return true;
}
$taxonomy = 'product_cat';
//
// slugs of categories without calculator
$excluded = ['first_category', 'another_category'];
//
// get current product category
$product_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'id=>slug' ) );
if ( ! is_array($product_terms) ) {
$product_terms = [];
}
else {
$product_terms = array_values( $product_terms );
}
//
// if product has any of category from "$excluded" - return TRUE
$in_excluded_cat = array_intersect($product_terms, $excluded);
if ( is_array($in_excluded_cat) && count($in_excluded_cat) ) {
return true;
}
return false;
}
And then use this function where it is needed:
function my_scripts_method() {
if ( se337775_omit_calculator() )
return;
wp_enqueue_script( 'calc_fun', get_stylesheet_directory_uri() . '/assets/js/calc.js', array(), '1.6');
wp_enqueue_style( 'calc_css', get_stylesheet_directory_uri() . '/assets/css/calc.css', true , 9.1 );
}