add_action('pre_get_posts', 'exclude_products_from_categories'); function exclude_products_from_categories($query) { if (is_shop() && $query->is_main_query()) { $excluded_categories = array(7, 8, 9); // Categories to exclude $query->set('tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $excluded_categories, 'operator' => 'NOT IN', ), )); } }
Category: WordPress
Plug-ins(37)
Change product price based on user location
add_filter(‘woocommerce_product_get_price’, ‘adjust_price_by_user_location’, 10, 2); function adjust_price_by_user_location($price, $product) { $user_country = WC()->customer->get_billing_country(); if ($user_country == ‘US’) { $price = $price * 1.1; // 10% Aufschlag für US-Kunden } return $price; }
Apply coupon code based on product category
add_action('woocommerce_before_cart', 'apply_coupon_based_on_category'); function apply_coupon_based_on_category() { $coupon_code = 'CATEGORYCODE'; // Coupon code for specific category $target_category = 'sale'; // Category name $cart = WC()->cart; $coupon_applied = false; foreach ($cart->get_cart() as $cart_item) { $product = $cart_item['data']; if (has_term($target_category, 'product_cat', $product->get_id())) { $coupon_applied = true; break; } } if (!$coupon_applied) { $cart->apply_coupon($coupon_code); } }
Change shipping costs based on the weight of the products
add_filter('woocommerce_package_rates', 'adjust_shipping_costs_by_weight', 10, 2); function adjust_shipping_costs_by_weight($rates, $package) { $total_weight = WC()->cart->get_cart_contents_weight(); if ($total_weight > 10) { foreach ($rates as $rate_id => $rate) { $rates[$rate_id]->cost += 7.00; // Additional shipping costs for heavy packages } } return $rates; }
Hide products in the “Sale” category
add_action('pre_get_posts', 'exclude_sale_category_products'); function exclude_sale_category_products($query) { if (is_shop() && $query->is_main_query()) { $query->set('tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'sale', 'operator' => 'NOT IN', ), )); } }
Free shipping for orders over a certain amount
add_filter('woocommerce_shipping_free_shipping_is_available', 'free_shipping_above_threshold', 10, 2); function free_shipping_above_threshold($is_available, $package) { $free_shipping_threshold = 75; // Amount from which shipping is free $cart_total = WC()->cart->get_cart_contents_total(); if ($cart_total >= $free_shipping_threshold) { $is_available = true; } return $is_available; }
Embed custom CSS file:
add_action('wp_enqueue_scripts', 'custom_styles'); function custom_styles() { wp_enqueue_style('custom-style', get_stylesheet_directory_uri() . '/custom.css'); }
Disable customer reviews on the product page
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
Set minimum order quantity for free delivery
The set_free_shipping_minimum() function in WooCommerce checks if the customer's cart value has reached the minimum order value for free shipping. If the cart value is below the minimum order value, an error message is displayed informing the customer that they must reach the minimum order value to receive free shipping. The minimum order value can be changed in the function by setting the […]
Show order status in order emails
add_filter('woocommerce_email_order_meta_keys', 'show_order_status_in_emails'); function show_order_status_in_emails($keys) { $keys[] = '_order_status'; return $keys; }